2 Module N_HTTP_SERVER
gullradriel edited this page 2026-10-29 20:54:40 +01:00

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

Structures

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

Types

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

Functions

Name
void n_http_server_stop(N_HTTP_SERVER * srv)
ask the server to stop; async-signal-safe, any thread
void n_http_server_stats(N_HTTP_SERVER * srv, N_HTTP_SERVER_STATS * out)
copy of the counters
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
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_read_body(N_HTTP_SERVER_REQUEST * req, long long max, N_STR ** out)
read the whole body into memory
N_HTTP_SERVER * n_http_server_new(const N_HTTP_SERVER_CONFIG * cfg)
bind the listening socket and load the TLS context
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()
void n_http_server_free(N_HTTP_SERVER ** srv)
free a stopped server and set *srv to NULL
void n_http_server_config_defaults(N_HTTP_SERVER_CONFIG * cfg)
fill a config with the defaults documented on each field

Types Documentation

typedef n_http_server_handler

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 Documentation

function n_http_server_stop

void n_http_server_stop(
    N_HTTP_SERVER * srv
)

ask the server to stop; async-signal-safe, any thread

Parameters:

  • srv the server
  • srv the server

ask the server to stop (async-signal-safe)

ask the server to stop; async-signal-safe, any thread

function n_http_server_stats

void n_http_server_stats(
    N_HTTP_SERVER * srv,
    N_HTTP_SERVER_STATS * out
)

copy of the counters

Parameters:

  • srv the server
  • out receives the counters

function n_http_server_run

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

Parameters:

  • srv the server
  • srv the server

Return:

  • TRUE after a clean stop, FALSE when the server could not start
  • TRUE after a clean stop, FALSE when the server could not start

serve until n_http_server_stop()

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

function n_http_server_read_body_to_fd

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)

Parameters:

  • req the request
  • max largest body accepted for this request
  • fd where the body is written
  • req the request
  • max largest body accepted for this request
  • fd where the body is written

Return:

  • TRUE, FALSE with req->body_status set (500 on a write error)
  • TRUE, FALSE with req->body_status set

read the whole body into an open file descriptor, refusing more than max

read the whole body into an open file descriptor (for uploads)

function n_http_server_read_body

int n_http_server_read_body(
    N_HTTP_SERVER_REQUEST * req,
    long long max,
    N_STR ** out
)

read the whole body into memory

Parameters:

  • req the request
  • max largest body accepted for this request
  • out receives the body (created, or appended to)
  • req the request
  • max largest body accepted for this request
  • out receives the body (created, or appended to)

Return:

  • TRUE, FALSE with req->body_status set
  • TRUE, FALSE with req->body_status set

read the whole body into memory, refusing more than max

Refused with req->body_status 413 when Content-Length is above max (the connection then closes after the response), 408 when the body deadline passes, 400 when the peer goes away. A request without a body gives an empty string. Call it at most once. read the whole body into memory

function n_http_server_new

N_HTTP_SERVER * n_http_server_new(
    const N_HTTP_SERVER_CONFIG * cfg
)

bind the listening socket and load the TLS context

Parameters:

  • cfg the settings (copied; strings must stay valid until free)

Return: the server, NULL on a bad setting, a bind or a TLS error

function n_http_server_header

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()

Parameters:

  • req the request
  • name header name, any case
  • len receives the value length (may be NULL)

Return: the value (not NUL-terminated) or NULL

function n_http_server_free

void n_http_server_free(
    N_HTTP_SERVER ** srv
)

free a stopped server and set *srv to NULL

Parameters:

  • srv address of the server pointer (may point to NULL)

function n_http_server_config_defaults

void n_http_server_config_defaults(
    N_HTTP_SERVER_CONFIG * cfg
)

fill a config with the defaults documented on each field

Parameters:

  • cfg the config to fill
  • cfg the config to fill

fill a config with the documented defaults

fill a config with the defaults documented on each field