![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
#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>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 | |
| struct client |
a test client connection, plain or TLS
Definition at line 148 of file ex_http_server.c.
| Data Fields | ||
|---|---|---|
| NETWORK * | netw | the socket |
| SSL * | ssl | TLS, or NULL. |
| struct reply |
a parsed response as the client saw it
Definition at line 243 of file ex_http_server.c.
| Data Fields | ||
|---|---|---|
| char | body[70000] | the body |
| long | body_len | body length |
| char | head[4096] | the head |
| int | status | status code, 0 when none |
| struct runner |
|
static |
close a client connection
| c | the connection |
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().
|
static |
connect to the server
| port | the port |
| c | receives the connection |
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().
|
static |
receive some bytes (3 s socket timeout)
| c | the connection |
| b | buffer |
| n | its size |
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().
|
static |
send all bytes
| c | the connection |
| s | bytes |
| n | how many |
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().
|
static |
send a NUL-terminated string
| c | the connection |
| s | the string |
Definition at line 203 of file ex_http_server.c.
References cl_send().
Referenced by test_plain(), and test_tls().
|
static |
upgrade a client connection to TLS
| c | the connection |
| ctx | client context |
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().
|
static |
has the server closed the connection (within ms)?
| c | the connection |
| ms | how long to wait |
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().
|
static |
|
static |
the application under test
| req | the request |
| user | unused |
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().
| int main | ( | int | argc, |
| char ** | argv ) |
run the checks
| argc | argument count |
| argv | arguments |
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.
|
static |
read -V LOG_LEVEL
| argc | argument count |
| argv | arguments |
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().
read one response
| c | the connection |
| head_only | response to a HEAD request (no body) |
| r | receives it |
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().
|
static |
run the server
| p | the runner |
Definition at line 329 of file ex_http_server.c.
References n_http_server_run(), runner::ok, and runner::srv.
|
static |
start a server on the first free port from base
| cfg | settings (port is filled) |
| port | buffer for the port |
| base | first port to try |
Definition at line 340 of file ex_http_server.c.
References n_http_server_new(), N_HTTP_SERVER_CONFIG::port, and port.
Referenced by _draw_label_lines(), _label_line_at(), _label_lines_char_at(), _n_html_skip_rawtext(), _slider_travel(), _suggest_accept(), _suggest_refresh(), _suggest_word(), _text_load(), _word_text(), lex_next(), main(), n_diff_to_unified(), n_gui_slider_handle_pos(), n_gui_syntaxview_set_selection(), n_gui_textarea_set_selection(), n_html_extract_js_urls(), n_html_to_text(), n_http_form_parse(), n_markdown_parse_ex(), netw_extract_http_info(), str_to_int_ex(), str_to_int_nolog(), str_to_long_ex(), str_to_long_long_ex(), test_capacity(), test_plain(), test_tls(), trim_nocopy(), and zip_scan().
|
static |
the connection cap, on a server whose deadlines are long enough for the held connections to stay open however slow the machine is
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().
|
static |
the plain HTTP server
Definition at line 354 of file ex_http_server.c.
References N_HTTP_SERVER_STATS::accepted, N_HTTP_SERVER_STATS::active, N_HTTP_SERVER_STATS::bad_requests, reply::body, reply::body_len, N_HTTP_SERVER_CONFIG::body_timeout_ms, cl_close(), cl_connect(), cl_send(), cl_sends(), closed_by_server(), expect_true(), handler(), N_HTTP_SERVER_CONFIG::handler, reply::head, N_HTTP_SERVER_CONFIG::header_timeout_ms, N_HTTP_SERVER_CONFIG::idle_timeout_ms, NETWORK::link, LOG_INFO, N_HTTP_SERVER_CONFIG::max_body, n_http_server_config_defaults(), n_http_server_free(), n_http_server_stats(), n_http_server_stop(), n_log, client::netw, npending, port, read_reply(), N_HTTP_SERVER_STATS::refused, N_HTTP_SERVER_STATS::requests, run, run_server(), N_SOCKET::sock, start(), N_HTTP_SERVER_STATS::timeouts, N_HTTP_SERVER_CONFIG::workers, and N_HTTP_SERVER_CONFIG::write_timeout_ms.
Referenced by main().
|
static |
the TLS server
Definition at line 639 of file ex_http_server.c.
References reply::body, cert, cl_close(), cl_connect(), cl_sends(), cl_tls(), closed_by_server(), expect_true(), free_nstr, handler(), N_HTTP_SERVER_CONFIG::handler, N_HTTP_SERVER_CONFIG::handshake_timeout_ms, key, n_http_server_config_defaults(), n_http_server_free(), n_http_server_new(), n_http_server_stats(), n_http_server_stop(), n_x509_generate_ca(), n_x509_mint_host_cert(), npending, port, read_reply(), run, run_server(), client::ssl, start(), N_HTTP_SERVER_CONFIG::tls_cert_file, N_HTTP_SERVER_STATS::tls_failures, N_HTTP_SERVER_CONFIG::tls_key_file, N_HTTP_SERVER_CONFIG::workers, and write_pem().
Referenced by main().
|
static |
text response
| status | status code |
| text | body |
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().
|
static |
write a PEM string to a temp file
| pem | the PEM |
| path | template, replaced by the file name |
Definition at line 630 of file ex_http_server.c.
References N_STR::data, and N_STR::written.
Referenced by test_tls().
|
static |
failed checks
Definition at line 54 of file ex_http_server.c.
|
static |
number of leftover bytes
Definition at line 253 of file ex_http_server.c.
Referenced by closed_by_server(), read_reply(), test_capacity(), test_plain(), and test_tls().
|
static |
leftover bytes between responses on one connection
Definition at line 251 of file ex_http_server.c.
Referenced by n_gui_draw_detached(), and read_reply().