Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_server.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
46
47#ifndef __NILOREA_HTTP_SERVER_HEADER
48#define __NILOREA_HTTP_SERVER_HEADER
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
58
59#include "nilorea/n_http_req.h"
61#include "nilorea/n_str.h"
62
63#include <stddef.h>
64
66
68typedef N_HTTP_RESP* (*n_http_server_handler)(struct N_HTTP_SERVER_REQUEST* req, void* user);
69
107
109typedef struct N_HTTP_SERVER_REQUEST {
113 const char* raw;
115 const char* path;
117 size_t path_len;
119 const char* query;
121 size_t query_len;
123 const char* peer_ip;
125 int tls;
129 void* conn;
133
135typedef struct N_HTTP_SERVER_STATS {
136 unsigned long long accepted;
137 unsigned long long refused;
138 unsigned long long active;
139 unsigned long long requests;
140 unsigned long long timeouts;
141 unsigned long long bad_requests;
142 unsigned long long tls_failures;
144
146typedef struct N_HTTP_SERVER N_HTTP_SERVER;
147
151
156
163
167
171
176
182const char* n_http_server_header(const N_HTTP_SERVER_REQUEST* req, const char* name, size_t* len);
183
194int n_http_server_read_body(N_HTTP_SERVER_REQUEST* req, long long max, N_STR** out);
195
201int n_http_server_read_body_to_fd(N_HTTP_SERVER_REQUEST* req, long long max, int fd);
202
206
207#ifdef __cplusplus
208}
209#endif
210
211#endif /* __NILOREA_HTTP_SERVER_HEADER */
#define N_HTTP_REQ_PATH_MAX
Longest origin-form request target ('/'-prefixed, query included).
Definition n_http_req.h:65
A parsed request head.
Definition n_http_req.h:117
a response under construction (fields are private, use the functions)
int body_timeout_ms
body deadline, ms (default 60000)
int header_timeout_ms
request head deadline from its first byte, ms (default 10000)
unsigned long long requests
requests answered
int tls
non-zero over TLS
unsigned long long timeouts
connections ended by a deadline
int body_status
after a failed body read: the status to answer (408, 413...)
char target[2048u+2]
private: storage for path and query
const char * raw
the raw head, for n_http_server_header()
size_t max_requests_per_conn
requests served on one connection before it is closed (default 1000)
unsigned long long active
connections being served now
size_t workers
worker threads = most concurrent connections (default 32)
const char * port
port to bind ("7430")
int write_timeout_ms
deadline to write one response, ms (default 60000)
unsigned long long accepted
connections accepted
size_t query_len
length of query
long long max_body
largest Content-Length accepted at all (default 32 MiB); handlers pass their own, lower cap to the bo...
const char * tls_key_file
PEM private key, NULL for plain HTTP.
const char * tls_cert_file
PEM certificate chain, NULL for plain HTTP.
const char * path
the path, without the query string, still percent-encoded
n_http_server_handler handler
the application
const char * addr
address to bind ("127.0.0.1")
size_t max_per_ip
most connections from one peer address, 0 for no limit (default 0: behind a reverse proxy every conne...
n_http_request head
the parsed head (method, version_minor, content_length, headers)
size_t path_len
length of path
void * conn
private: the connection
int require_host
refuse HTTP/1.1 requests without exactly one Host (default 1)
unsigned long long refused
connections refused (queue full, per-IP cap)
const char * peer_ip
peer address as text
const char * query
the query string without '?
void * user
passed to the handler
int idle_timeout_ms
wait for the next request on a kept-alive connection, ms (default 15000)
unsigned long long tls_failures
failed TLS handshakes
unsigned long long bad_requests
400 / 413 / 417 / 431 answered by the server
int handshake_timeout_ms
TLS handshake deadline, ms (default 10000).
void n_http_server_stats(N_HTTP_SERVER *srv, N_HTTP_SERVER_STATS *out)
copy of the counters
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 fin...
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
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
void n_http_server_stop(N_HTTP_SERVER *srv)
ask the server to stop; async-signal-safe, any thread
int n_http_server_read_body(N_HTTP_SERVER_REQUEST *req, long long max, N_STR **out)
read the whole body into memory
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
server settings; start from n_http_server_config_defaults()
a request as the handler sees it (fields are read-only)
counters, see n_http_server_stats()
A box including a string and his lenght.
Definition n_str.h:61
Pure, strict, bounded parser for an HTTP/1.1 request head.
HTTP/1.1 response: validated headers, memory or file body, framing.
N_HTTP_SERVER_CONFIG cfg
a server (opaque)
N_STR and string function declaration.