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


Typedefs

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

Enums

enum http_server_request_action_t

Values:

http_server_request_action_get_t = 0
http_server_request_action_post_t = 1
enum http_server_content_type_t

Content type.

Values:

http_server_content_type_text_plain_t = 0
http_server_content_type_text_html_t = 1
enum http_server_response_code_t

Response codes.

Values:

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.

Values:

http_server_connection_state_free_t = 0
http_server_connection_state_allocated_t

Functions

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.

Return
zero(0) or negative error code.
Parameters
  • 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().

Return
zero(0) or negative error code.
Parameters
  • 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.

Return
zero(0) or negative error code.
Parameters
  • 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.

Return
zero(0) or negative error code.
Parameters
  • 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.

Return
zero(0) or negative error code.
Parameters
  • 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 action
char path[64]
int present
char value[20]
struct http_server_request_t::@55::@56 http_server_request_t::sec_websocket_key
struct http_server_request_t::@55::@57 http_server_request_t::content_type
long value
struct http_server_request_t::@55::@58 http_server_request_t::content_length
struct http_server_request_t::@55::@59 http_server_request_t::authorization
struct http_server_request_t::@55::@60 http_server_request_t::expect
struct http_server_request_t::@55 http_server_request_t::headers
struct http_server_response_t
#include <http_server.h>

HTTP response.

Public Members

int type
http_server_response_code_t code
const char *buf_p
size_t size
struct http_server_response_t::@61 http_server_response_t::content
struct http_server_listener_t

Public Members

const char *address_p
int port
const char *name_p
void *buf_p
size_t size
struct http_server_listener_t::@62::@63 http_server_listener_t::stack
struct thrd_t *id_p
struct http_server_listener_t::@62 http_server_listener_t::thrd
struct socket_t socket
struct http_server_connection_t

Public Members

http_server_connection_state_t state
const char *name_p
void *buf_p
size_t size
struct http_server_connection_t::@64::@65 http_server_connection_t::stack
struct thrd_t *id_p
struct http_server_connection_t::@64 http_server_connection_t::thrd
struct http_server_t *self_p
struct socket_t socket
void *chan_p
struct event_t events
struct http_server_route_t
#include <http_server.h>

Call given callback for given path.

Public Members

const char *path_p
http_server_route_callback_t callback
struct http_server_t

Public Members

const char *root_path_p
const struct http_server_route_t *routes_p
http_server_route_callback_t on_no_route
struct http_server_listener_t *listener_p
struct http_server_connection_t *connections_p
struct ssl_context_t *ssl_context_p
struct event_t events