Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_response.c File Reference
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_common.h"
#include "nilorea/n_http_response.h"
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
Include dependency graph for ex_http_response.c:

Go to the source code of this file.

Data Structures

struct  sink
 an in-memory sink for n_http_response_write() More...

Macros

#define RANGE(s, total)

Functions

static void expect_true (const char *what, int ok)
 count a failed check
static int head_is (const N_HTTP_RESP *r, const char *want)
 text of the head of r, up to and excluding the Date line's value
int main (int argc, char **argv)
 run the checks
static int process_args (int argc, char **argv)
 read -V
static ssize_t sink_write (void *ctx, const void *buf, size_t len)
 write function: accept at most s->chunk bytes
static void test_headers (void)
 headers: validation, set/remove, framing
static void test_range (void)
 Range header parsing.
static void test_status (void)
 status codes and bodiless responses
static void test_write (void)
 writing memory and file bodies, HEAD, partial and failing writes

Variables

static int failures = 0
 failed checks
static int log_level = 3
 log level from -V

Data Structure Documentation

◆ sink

struct sink

an in-memory sink for n_http_response_write()

Examples
ex_gui_suggest.c, and ex_http_response.c.

Definition at line 77 of file ex_http_response.c.

Collaboration diagram for sink:
Data Fields
size_t chunk most bytes accepted per call (partial writes)
N_STR * data everything written
long fail_at fail once this many bytes were written, -1 never

Macro Definition Documentation

◆ RANGE

#define RANGE ( s,
total )
Value:
n_http_parse_range((s), strlen(s), (total), &a, &b)
N_HTTP_RANGE n_http_parse_range(const char *value, size_t len, long long total, long long *first, long long *last)
interpret a single-range Range header for a body of total bytes
Examples
ex_http_response.c.

Referenced by test_range().

Function Documentation

◆ expect_true()

void expect_true ( const char * what,
int ok )
static

count a failed check

Parameters
whatthe check
okits result

Definition at line 50 of file ex_http_response.c.

References failures, LOG_ERR, LOG_INFO, and n_log.

◆ head_is()

int head_is ( const N_HTTP_RESP * r,
const char * want )
static

text of the head of r, up to and excluding the Date line's value

Parameters
rthe response
wantexpected head, with the Date line removed
Returns
non-zero when equal
Examples
ex_http_response.c.

Definition at line 102 of file ex_http_response.c.

References free_nstr, h(), LOG_ERR, n_http_response_head(), and n_log.

Referenced by test_headers(), and test_status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char ** argv )

run the checks

Parameters
argcargument count
argvarguments
Returns
0 when every check passes

Definition at line 315 of file ex_http_response.c.

References failures, LOG_ERR, log_level, LOG_NOTICE, n_log, process_args(), set_log_level(), test_headers(), test_range(), test_status(), and test_write().

Here is the call graph for this function:

◆ process_args()

int process_args ( int argc,
char ** argv )
static

read -V

Parameters
argcargument count
argvarguments
Returns
0, or -1 on a bad argument

Definition at line 63 of file ex_http_response.c.

References log_level.

◆ sink_write()

ssize_t sink_write ( void * ctx,
const void * buf,
size_t len )
static

write function: accept at most s->chunk bytes

Parameters
ctxthe sink
bufbytes
lenhow many
Returns
bytes taken, -1 to simulate a broken connection
Examples
ex_http_response.c.

Definition at line 88 of file ex_http_response.c.

References sink::chunk, sink::data, sink::fail_at, nstrcat_ex(), and N_STR::written.

Referenced by test_status(), and test_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_headers()

void test_headers ( void )
static

◆ test_range()

void test_range ( void )
static

Range header parsing.

Examples
ex_http_response.c.

Definition at line 290 of file ex_http_response.c.

References expect_true(), n_http_parse_range(), N_HTTP_RANGE_NONE, N_HTTP_RANGE_OK, N_HTTP_RANGE_UNSATISFIABLE, and RANGE.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_status()

void test_status ( void )
static

◆ test_write()

void test_write ( void )
static

writing memory and file bodies, HEAD, partial and failing writes

Examples
ex_http_response.c.

Definition at line 217 of file ex_http_response.c.

References N_STR::data, sink::data, expect_true(), free_nstr, n_http_response_append(), n_http_response_body_length(), n_http_response_file(), n_http_response_free(), n_http_response_head(), n_http_response_new(), n_http_response_set_connection(), n_http_response_write(), sink_write(), and N_STR::written.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ failures

int failures = 0
static

failed checks

Definition at line 45 of file ex_http_response.c.

◆ log_level

int log_level = 3
static

log level from -V

Definition at line 43 of file ex_http_response.c.