Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_server.h File Reference

HTTP/1.1 server: TLS or plain, deadlines, limits, keep-alive. More...

#include "nilorea/n_http_req.h"
#include "nilorea/n_http_response.h"
#include "nilorea/n_str.h"
#include <stddef.h>
Include dependency graph for n_http_server.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  N_HTTP_SERVER_CONFIG
 server settings; start from n_http_server_config_defaults() More...
struct  N_HTTP_SERVER_REQUEST
 a request as the handler sees it (fields are read-only) More...
struct  N_HTTP_SERVER_STATS
 counters, see n_http_server_stats() More...

Typedefs

typedef N_HTTP_RESP *(* n_http_server_handler) (struct N_HTTP_SERVER_REQUEST *req, void *user)
 the application: build the response for a request; NULL means 500

Functions

void n_http_server_config_defaults (N_HTTP_SERVER_CONFIG *cfg)
 fill a config with the defaults documented on each field
void n_http_server_free (N_HTTP_SERVER **srv)
 free a stopped server and set *srv to NULL
const char * n_http_server_header (const N_HTTP_SERVER_REQUEST *req, const char *name, size_t *len)
 a request header, see n_http_request_header()
N_HTTP_SERVER * n_http_server_new (const N_HTTP_SERVER_CONFIG *cfg)
 bind the listening socket and load the TLS context
int n_http_server_read_body (N_HTTP_SERVER_REQUEST *req, long long max, N_STR **out)
 read the whole body into memory
int n_http_server_read_body_to_fd (N_HTTP_SERVER_REQUEST *req, long long max, int fd)
 read the whole body into an open file descriptor (for uploads)
int n_http_server_run (N_HTTP_SERVER *srv)
 serve until n_http_server_stop(): start the workers, accept, then stop accepting, let the workers finish their current request, close idle connections and join them
void n_http_server_stats (N_HTTP_SERVER *srv, N_HTTP_SERVER_STATS *out)
 copy of the counters
void n_http_server_stop (N_HTTP_SERVER *srv)
 ask the server to stop; async-signal-safe, any thread

Detailed Description

HTTP/1.1 server: TLS or plain, deadlines, limits, keep-alive.

One accept loop hands sockets to a fixed pool of worker threads through a bounded queue; a connection keeps its worker for its whole life, so the pool size is the connection cap, and a full queue refuses new connections. Every socket operation is non-blocking under a deadline: the TLS handshake, the request head (counted from its first byte), the body, each response and the keep-alive wait each have a TOTAL time limit, so a peer that trickles one byte at a time gets nowhere. Heads are parsed by n_http_request_parse_ex() (no chunked bodies, no smuggling). Bodies are read only when the handler asks for them, with its own size cap, into memory or a file; a body left unread closes the connection after the response. Keep-alive and pipelining are supported. The server answers 400, 408, 413, 417, 431, 500, 501 (a well-formed method other than GET, HEAD and POST) and 503 itself.

SIGPIPE is blocked in the worker threads only (a peer closing mid-write is an error return, not a dead process); the application's own signal setup is untouched. n_http_server_stop() is async-signal-safe.

Author
Castagnier Mickael
Version
1.0
Date
2026

Definition in file n_http_server.h.