Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_server.c File Reference
#include "nilorea/n_common.h"
#include "nilorea/n_http_response.h"
#include "nilorea/n_http_server.h"
#include "nilorea/n_log.h"
#include "nilorea/n_network.h"
#include "nilorea/n_str.h"
#include "nilorea/n_x509.h"
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for ex_http_server.c:

Go to the source code of this file.

Data Structures

struct  client
 a test client connection, plain or TLS More...
struct  reply
 a parsed response as the client saw it More...
struct  runner
 server thread state More...

Functions

static void cl_close (client *c)
 close a client connection
static int cl_connect (const char *port, client *c)
 connect to the server
static long cl_recv (client *c, char *b, size_t n)
 receive some bytes (3 s socket timeout)
static void cl_send (client *c, const char *s, size_t n)
 send all bytes
static void cl_sends (client *c, const char *s)
 send a NUL-terminated string
static int cl_tls (client *c, SSL_CTX *ctx)
 upgrade a client connection to TLS
static int closed_by_server (client *c, int ms)
 has the server closed the connection (within ms)?
static void expect_true (const char *what, int ok)
 count a failed check
static N_HTTP_RESP * handler (N_HTTP_SERVER_REQUEST *req, void *user)
 the application under test
int main (int argc, char **argv)
 run the checks
static void process_args (int argc, char **argv)
 read -V LOG_LEVEL
static int read_reply (client *c, int head_only, reply *r)
 read one response
static void * run_server (void *p)
 run the server
static N_HTTP_SERVER * start (N_HTTP_SERVER_CONFIG *cfg, char *port, int base)
 start a server on the first free port from base
static void test_capacity (void)
 the connection cap, on a server whose deadlines are long enough for the held connections to stay open however slow the machine is
static void test_plain (void)
 the plain HTTP server
static void test_tls (void)
 the TLS server
static N_HTTP_RESP * text (int status, const char *body)
 text response
static int write_pem (const N_STR *pem, char *path)
 write a PEM string to a temp file

Variables

static int failures = 0
 failed checks
static size_t npending = 0
 number of leftover bytes
static char pending [200000]
 leftover bytes between responses on one connection

Data Structure Documentation

◆ client

struct client

a test client connection, plain or TLS

Examples
ex_accept_pool_server.c, ex_http_server.c, ex_network_reactor.c, and ex_network_ws_limits.c.

Definition at line 148 of file ex_http_server.c.

Collaboration diagram for client:
Data Fields
NETWORK * netw the socket
SSL * ssl TLS, or NULL.

◆ reply

struct reply

a parsed response as the client saw it

Examples
ex_http2_server.c, ex_http_server.c, ex_network_sni.c, and ex_network_ssl_nonblock.c.

Definition at line 243 of file ex_http_server.c.

Collaboration diagram for reply:
Data Fields
char body[70000] the body
long body_len body length
char head[4096] the head
int status status code, 0 when none

◆ runner

struct runner

server thread state

Examples
ex_http_server.c.

Definition at line 321 of file ex_http_server.c.

Collaboration diagram for runner:
Data Fields
int ok run() result
N_HTTP_SERVER * srv the server

Function Documentation

◆ cl_close()

void cl_close ( client * c)
static

close a client connection

Parameters
cthe connection
Examples
ex_http_server.c.

Definition at line 228 of file ex_http_server.c.

References client::netw, netw_close(), and client::ssl.

Referenced by test_capacity(), test_plain(), and test_tls().

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

◆ cl_connect()

int cl_connect ( const char * port,
client * c )
static

connect to the server

Parameters
portthe port
creceives the connection
Returns
1 on success
Examples
ex_http_server.c.

Definition at line 159 of file ex_http_server.c.

References client::netw, netw_connect(), netw_setsockopt(), NETWORK_IPALL, and port.

Referenced by test_capacity(), test_plain(), and test_tls().

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

◆ cl_recv()

long cl_recv ( client * c,
char * b,
size_t n )
static

receive some bytes (3 s socket timeout)

Parameters
cthe connection
bbuffer
nits size
Returns
bytes, 0 on close, -1 on timeout/error
Examples
ex_http_server.c.

Definition at line 212 of file ex_http_server.c.

References NETWORK::link, client::netw, N_SOCKET::sock, and client::ssl.

Referenced by closed_by_server(), and read_reply().

Here is the caller graph for this function:

◆ cl_send()

void cl_send ( client * c,
const char * s,
size_t n )
static

send all bytes

Parameters
cthe connection
sbytes
nhow many
Examples
ex_http_server.c.

Definition at line 184 of file ex_http_server.c.

References NETWORK::link, client::netw, N_SOCKET::sock, and client::ssl.

Referenced by cl_sends(), and test_plain().

Here is the caller graph for this function:

◆ cl_sends()

void cl_sends ( client * c,
const char * s )
static

send a NUL-terminated string

Parameters
cthe connection
sthe string
Examples
ex_http_server.c.

Definition at line 203 of file ex_http_server.c.

References cl_send().

Referenced by test_plain(), and test_tls().

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

◆ cl_tls()

int cl_tls ( client * c,
SSL_CTX * ctx )
static

upgrade a client connection to TLS

Parameters
cthe connection
ctxclient context
Returns
1 on a completed handshake
Examples
ex_http_server.c.

Definition at line 172 of file ex_http_server.c.

References NETWORK::link, client::netw, N_SOCKET::sock, and client::ssl.

Referenced by test_tls().

Here is the caller graph for this function:

◆ closed_by_server()

int closed_by_server ( client * c,
int ms )
static

has the server closed the connection (within ms)?

Parameters
cthe connection
mshow long to wait
Returns
1 when closed
Examples
ex_http_server.c.

Definition at line 304 of file ex_http_server.c.

References cl_recv(), NETWORK::link, client::netw, npending, N_SOCKET::sock, and client::ssl.

Referenced by test_plain(), and test_tls().

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

◆ expect_true()

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

count a failed check

Parameters
whatthe check
okits result

Definition at line 59 of file ex_http_server.c.

References failures, LOG_ERR, LOG_INFO, and n_log.

◆ handler()

N_HTTP_RESP * handler ( N_HTTP_SERVER_REQUEST * req,
void * user )
static

the application under test

Parameters
reqthe request
userunused
Returns
the response, NULL for /null
Examples
ex_http_server.c.

Definition at line 104 of file ex_http_server.c.

References N_HTTP_SERVER_REQUEST::body_status, N_STR::data, free_nstr, n_http_response_append(), n_http_response_new(), n_http_server_read_body(), n_http_server_read_body_to_fd(), N_HTTP_SERVER_REQUEST::path, N_HTTP_SERVER_REQUEST::peer_ip, N_HTTP_SERVER_REQUEST::query, text(), N_HTTP_SERVER_REQUEST::tls, and N_STR::written.

Referenced by main(), test_capacity(), test_plain(), and test_tls().

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 714 of file ex_http_server.c.

References expect_true(), failures, handler(), N_HTTP_SERVER_CONFIG::handler, LOG_ERR, LOG_NOTICE, n_http_server_config_defaults(), n_http_server_free(), n_http_server_new(), n_http_server_stop(), n_log, netw_init_openssl(), netw_unload_openssl(), process_args(), set_log_level(), test_capacity(), test_plain(), test_tls(), and N_HTTP_SERVER_CONFIG::workers.

Here is the call graph for this function:

◆ process_args()

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

read -V LOG_LEVEL

Parameters
argcargument count
argvarguments

Definition at line 71 of file ex_http_server.c.

References LOG_DEBUG, LOG_ERR, LOG_INFO, LOG_NOTICE, LOG_NULL, and set_log_level().

Here is the call graph for this function:

◆ read_reply()

int read_reply ( client * c,
int head_only,
reply * r )
static

read one response

Parameters
cthe connection
head_onlyresponse to a HEAD request (no body)
rreceives it
Returns
the status, 0 when none arrived
Examples
ex_http_server.c.

Definition at line 260 of file ex_http_server.c.

References reply::body, reply::body_len, cl_recv(), reply::head, npending, pending, and reply::status.

Referenced by test_capacity(), test_plain(), and test_tls().

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

◆ run_server()

void * run_server ( void * p)
static

run the server

Parameters
pthe runner
Returns
NULL

Definition at line 329 of file ex_http_server.c.

References n_http_server_run(), runner::ok, and runner::srv.

Here is the call graph for this function:

◆ start()

◆ test_capacity()

void test_capacity ( void )
static

the connection cap, on a server whose deadlines are long enough for the held connections to stay open however slow the machine is

Examples
ex_http_server.c.

Definition at line 580 of file ex_http_server.c.

References cl_close(), cl_connect(), expect_true(), handler(), N_HTTP_SERVER_CONFIG::handler, N_HTTP_SERVER_CONFIG::header_timeout_ms, n_http_server_config_defaults(), n_http_server_free(), n_http_server_stop(), npending, port, read_reply(), run, run_server(), start(), and N_HTTP_SERVER_CONFIG::workers.

Referenced by main().

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

◆ test_plain()

◆ test_tls()

◆ text()

N_HTTP_RESP * text ( int status,
const char * body )
static

text response

Parameters
statusstatus code
textbody
Returns
the response
Examples
ex_gui.c, ex_gui_combobox_items.c, ex_gui_dictionary.c, ex_gui_network.c, ex_http_server.c, ex_json.c, and ex_tiff.c.

Definition at line 93 of file ex_http_server.c.

References n_http_response_append(), n_http_response_new(), and n_http_response_set_header().

Referenced by _draw_justified_selection(), _draw_label_overflow(), _draw_text_justified(), _draw_text_truncated(), _emit(), _justified_char_at_pos(), _label_content_height(), _label_line_at(), _label_line_count(), _n_gui_color_set_label(), _ngui_clip_set(), _ngui_meas_slot(), _popup_append(), _shorten_start(), _split_words(), _text_dims(), _text_load(), _text_w(), _textarea_step(), _word_text(), add_once(), build_chat_msg(), chat_log_add(), handler(), inl_push(), main(), md_build_seg(), md_flow(), md_run(), n_clipboard_set(), n_gui_add_label(), n_gui_add_label_link(), n_gui_combobox_add_item(), n_gui_dropmenu_add_dynamic_entry(), n_gui_dropmenu_add_entry(), n_gui_dropmenu_set_entry_text(), n_gui_get_text_dims(), n_gui_label_set_text(), n_gui_listbox_add_item(), n_gui_make_theme(), n_gui_markdownview_set_text(), n_gui_md_draw(), n_gui_md_height(), n_gui_md_link_at(), n_gui_md_style_default(), n_gui_popup_add_item(), n_gui_popup_add_submenu(), n_gui_popup_set_title(), n_gui_progressbar_set_text(), n_gui_radiolist_add_item(), n_gui_set_widget_tooltip(), n_gui_suggest_add(), n_gui_syntaxview_set_text(), n_gui_text_shorten(), n_gui_textarea_set_placeholder(), n_gui_textarea_set_text(), n_json_parse(), n_markdown_has_markup(), n_markdown_has_markup_ex(), n_markdown_parse(), n_markdown_parse_ex(), n_pretty_js(), n_pretty_json(), n_pretty_json_lenient(), n_pretty_xml(), n_url_safe_find(), on_search_change(), on_send_click(), on_text_change(), poll_network_msgs(), test_order(), test_sub_ifd(), update_completion(), wildmat(), and wildmatcase().

Here is the call graph for this function:

◆ write_pem()

int write_pem ( const N_STR * pem,
char * path )
static

write a PEM string to a temp file

Parameters
pemthe PEM
pathtemplate, replaced by the file name
Returns
1 on success
Examples
ex_http_server.c.

Definition at line 630 of file ex_http_server.c.

References N_STR::data, and N_STR::written.

Referenced by test_tls().

Here is the caller graph for this function:

Variable Documentation

◆ failures

int failures = 0
static

failed checks

Definition at line 54 of file ex_http_server.c.

◆ npending

size_t npending = 0
static

number of leftover bytes

Examples
ex_http_server.c.

Definition at line 253 of file ex_http_server.c.

Referenced by closed_by_server(), read_reply(), test_capacity(), test_plain(), and test_tls().

◆ pending

char pending[200000]
static

leftover bytes between responses on one connection

Examples
ex_http_server.c.

Definition at line 251 of file ex_http_server.c.

Referenced by n_gui_draw_detached(), and read_reply().