5.1. http_server — HTTP server

A HTTP server serves HTTP client requests, typically from a web browser.

A HTTP server can be wrapped in SSL, a secutiry layer, to create a HTTPS server.

Source code: src/inet/http_server.h, src/inet/http_server.c

Test code: tst/inet/http_server/main.c

Test coverage: src/inet/http_server.c

Example code: examples/http_server/main.c, examples/https_server/main.c


typedef int (*http_server_route_callback_t)(struct http_server_connection_t *connection_p, struct http_server_request_t *request_p)


enum http_server_request_action_t

Request action types.


http_server_request_action_get_t = 0
http_server_request_action_post_t = 1
enum http_server_content_type_t

Content type.


http_server_content_type_text_plain_t = 0
http_server_content_type_text_html_t = 1
enum http_server_response_code_t

Response codes.


http_server_response_code_200_ok_t = 200
http_server_response_code_400_bad_request_t = 400
http_server_response_code_401_unauthorized_t = 401
http_server_response_code_404_not_found_t = 404
enum http_server_connection_state_t

Connection state.


http_server_connection_state_free_t = 0


int http_server_init(struct http_server_t *self_p, struct http_server_listener_t *listener_p, struct http_server_connection_t *connections_p, const char *root_path_p, const struct http_server_route_t *routes_p, http_server_route_callback_t on_no_route)

Initialize given http server with given root path and maximum number of clients.

zero(0) or negative error code.
  • self_p: Http server to initialize.
  • listener_p: Listener.
  • connections_p: A NULL terminated list of connections.
  • root_path_p: Working directory for the connection threads.
  • routes_p: An array of routes.
  • on_no_route: Callback called for all requests without a matching route in route_p.

int http_server_wrap_ssl(struct http_server_t *self_p, struct ssl_context_t *context_p)

Wrap given HTTP server in SSL, to make it secure.

This function must be called after http_server_init() and before http_server_start().

zero(0) or negative error code.
  • self_p: Http server to wrap in SSL.
  • context_p: SSL context to wrap the server in.

int http_server_start(struct http_server_t *self_p)

Start given HTTP server.

Spawn the threads and start listening for connections.

zero(0) or negative error code.
  • self_p: Http server.

int http_server_stop(struct http_server_t *self_p)

Stop given HTTP server.

Closes the listener and all open connections, and then kills the threads.

zero(0) or negative error code.
  • self_p: Http server.

int http_server_response_write(struct http_server_connection_t *connection_p, struct http_server_request_t *request_p, struct http_server_response_t *response_p)

Write given HTTP response to given connected client. This function should only be called from the route callbacks to respond to given request.

zero(0) or negative error code.
  • connection_p: Current connection.
  • request_p: Current request.
  • response_p: Current response. If buf_p in the response to NULL this function will only write the HTTP header, including the size, to the socket. After this function returns write the payload by calling socket_write().

struct http_server_request_t
#include <http_server.h>

HTTP request.

Public Members

http_server_request_action_t http_server_request_taction
char http_server_request_tpath[64]
int http_server_request_tpresent
char http_server_request_tvalue[20]
struct http_server_request_t::@56::@57 http_server_request_t::sec_websocket_key
struct http_server_request_t::@56::@58 http_server_request_t::content_type
long http_server_request_tvalue
struct http_server_request_t::@56::@59 http_server_request_t::content_length
struct http_server_request_t::@56::@60 http_server_request_t::authorization
struct http_server_request_t::@56::@61 http_server_request_t::expect
struct http_server_request_t::@56 http_server_request_t::headers
struct http_server_response_t
#include <http_server.h>

HTTP response.

Public Members

int http_server_response_ttype
http_server_response_code_t http_server_response_tcode
const char *http_server_response_tbuf_p
size_t http_server_response_tsize
struct http_server_response_t::@62 http_server_response_t::content
struct http_server_listener_t

Public Members

const char *http_server_listener_taddress_p
int http_server_listener_tport
const char *http_server_listener_tname_p
void *http_server_listener_tbuf_p
size_t http_server_listener_tsize
struct http_server_listener_t::@63::@64 http_server_listener_t::stack
struct thrd_t *http_server_listener_tid_p
struct http_server_listener_t::@63 http_server_listener_t::thrd
struct socket_t http_server_listener_tsocket
struct http_server_connection_t

Public Members

http_server_connection_state_t http_server_connection_tstate
const char *http_server_connection_tname_p
void *http_server_connection_tbuf_p
size_t http_server_connection_tsize
struct http_server_connection_t::@65::@66 http_server_connection_t::stack
struct thrd_t *http_server_connection_tid_p
struct http_server_connection_t::@65 http_server_connection_t::thrd
struct http_server_t *http_server_connection_tself_p
struct socket_t http_server_connection_tsocket
void *http_server_connection_tchan_p
struct event_t http_server_connection_tevents
struct http_server_route_t
#include <http_server.h>

Call given callback for given path.

struct http_server_t

Public Members

const char *http_server_troot_path_p
const struct http_server_route_t *http_server_troutes_p
http_server_route_callback_t http_server_ton_no_route
struct http_server_listener_t *http_server_tlistener_p
struct http_server_connection_t *http_server_tconnections_p
struct ssl_context_t *http_server_tssl_context_p
struct event_t http_server_tevents