Tilde Friends
HTTP Server

Classes

struct  _tf_http_request_t
 

Typedefs

typedef struct _tf_http_connection_t tf_http_connection_t
 
typedef struct _tf_http_request_t tf_http_request_t
 
typedef struct _tf_http_t tf_http_t
 
typedef struct _tf_tls_context_t tf_tls_context_t
 
typedef struct _tf_trace_t tf_trace_t
 
typedef struct uv_loop_s uv_loop_t
 
typedef void() tf_http_message_callback(tf_http_request_t *request, int op_code, const void *data, size_t size)
 
typedef void() tf_http_close_callback(tf_http_request_t *request)
 
typedef void() tf_http_callback_t(tf_http_request_t *request)
 
typedef void() tf_http_cleanup_t(void *user_data)
 

Functions

tf_http_ttf_http_create (uv_loop_t *loop)
 
void tf_http_set_trace (tf_http_t *http, tf_trace_t *trace)
 
int tf_http_listen (tf_http_t *http, int port, bool local_only, tf_tls_context_t *tls, tf_http_cleanup_t *cleanup, void *user_data)
 
void tf_http_add_handler (tf_http_t *http, const char *pattern, tf_http_callback_t *callback, tf_http_cleanup_t *cleanup, void *user_data)
 
void tf_http_respond (tf_http_request_t *request, int status, const char **headers, int headers_count, const void *body, size_t content_length)
 
size_t tf_http_get_body (const tf_http_request_t *request, const void **out_data)
 
void tf_http_destroy (tf_http_t *http)
 
void tf_http_set_user_data (tf_http_t *http, void *user_data, tf_http_cleanup_t *cleanup)
 
void * tf_http_get_user_data (tf_http_t *http)
 
void tf_http_request_ref (tf_http_request_t *request)
 
void tf_http_request_unref (tf_http_request_t *request)
 
const char * tf_http_request_get_header (tf_http_request_t *request, const char *name)
 
const char * tf_http_get_cookie (const char *cookie_header, const char *name)
 
void tf_http_request_websocket_send (tf_http_request_t *request, int op_code, const void *data, size_t size)
 
void tf_http_request_websocket_upgrade (tf_http_request_t *request)
 
const char * tf_http_status_text (int status)
 
bool tf_http_pattern_matches (const char *pattern, const char *path)
 

Detailed Description

This is a HTTP server.

It can listen on multiple ports. It supports IPv4 and IPv6. It handles websocket connections. Requests can be handled immediately or at a later time. It is very bare bones, and that is a feature.

Typedef Documentation

◆ tf_http_callback_t

typedef void() tf_http_callback_t(tf_http_request_t *request)

A callback called when an HTTP request is received.

Parameters
requestThe HTTP request.

◆ tf_http_cleanup_t

typedef void() tf_http_cleanup_t(void *user_data)

A callback called when the HTTP instance is destroyed.

Parameters
user_dataUser data provided with the callback.

◆ tf_http_close_callback

typedef void() tf_http_close_callback(tf_http_request_t *request)

A callback called when a request closes.

Parameters
requestThe HTTP request.

◆ tf_http_connection_t

typedef struct _tf_http_connection_t tf_http_connection_t

An HTTP connection.

◆ tf_http_message_callback

typedef void() tf_http_message_callback(tf_http_request_t *request, int op_code, const void *data, size_t size)

A callback called when receiving a websocket message.

Parameters
requestThe HTTP request.
op_codeThe type of websocket message.
dataThe payload.
sizeThe size of the payload in bytes.

◆ tf_http_request_t

An HTTP request.

◆ tf_http_t

typedef struct _tf_http_t tf_http_t

An HTTP instance.

◆ tf_tls_context_t

typedef struct _tf_tls_context_t tf_tls_context_t

A TLS context.

◆ tf_trace_t

typedef struct _tf_trace_t tf_trace_t

A trace instance.

◆ uv_loop_t

typedef struct uv_loop_s uv_loop_t

An event loop.

Function Documentation

◆ tf_http_add_handler()

void tf_http_add_handler ( tf_http_t http,
const char *  pattern,
tf_http_callback_t callback,
tf_http_cleanup_t cleanup,
void *  user_data 
)

Add an HTTP request handler.

Parameters
httpThe HTTP instance.
patternThe prefix that the path of incoming requests must match to use this handler.
callbackThe function to be called to handle the request.
cleanupA function to be called when the request is complete.
user_dataUser data to pass to the callbacks.

◆ tf_http_create()

tf_http_t * tf_http_create ( uv_loop_t loop)

Create an HTTP server using the given libuv loop.

Parameters
loopA libuv loop to use.
Returns
An HTTP server instance.

◆ tf_http_destroy()

void tf_http_destroy ( tf_http_t http)

Destroy an HTTP instance.

Parameters
httpThe HTTP instance.

◆ tf_http_get_body()

size_t tf_http_get_body ( const tf_http_request_t request,
const void **  out_data 
)

Get the request body content.

Parameters
requestAn incoming request.
out_dataThe request body content. Valid until the request is completed.
Returns
The size of the request body content.

◆ tf_http_get_cookie()

const char * tf_http_get_cookie ( const char *  cookie_header,
const char *  name 
)

Get a cookie value from request headers.

Parameters
cookie_headerThe value of the "Cookie" header of the form "name1=value1; name2=value2".
nameThe cookie name.
Returns
The cookie value, if found, or NULL. Must be freed with tf_free().

◆ tf_http_get_user_data()

void * tf_http_get_user_data ( tf_http_t http)

Get HTTP instance user data previous set with tf_http_set_user_data().

Parameters
httpThe HTTP instance.
Returns
The user data.

◆ tf_http_listen()

int tf_http_listen ( tf_http_t http,
int  port,
bool  local_only,
tf_tls_context_t tls,
tf_http_cleanup_t cleanup,
void *  user_data 
)

Begin listening for HTTP requests on a new port. May be called multiple times to listen on multiple ports.

Parameters
httpThe HTTP instance.
portThe port on which to listen, or 0 to assign a free port.
local_onlyOnly access connections on localhost, otherwise any address.
tlsAn optional TLS context to use for HTTPS requests.
cleanupA function called when the HTTP instance is being cleaned up.
user_dataUser data passed to the cleanup callback.
Returns
The port number on which the HTTP instance is now listening.

◆ tf_http_pattern_matches()

bool tf_http_pattern_matches ( const char *  pattern,
const char *  path 
)

Match URL patterns. "*" matches anything, and "{word}" matches [a-zA-Z][a-zA-Z0-9]*".

Parameters
patternThe pattern to match.
pathThe path to test.
Returns
true if the path matches the pattern.

◆ tf_http_request_get_header()

const char * tf_http_request_get_header ( tf_http_request_t request,
const char *  name 
)

Get the value of a header from an HTTP request.

Parameters
requestThe request.
nameThe header key. Matched case insensitively.
Returns
The value or NULL.

◆ tf_http_request_ref()

void tf_http_request_ref ( tf_http_request_t request)

Increment a requests refcount to keep it around after its callback. tf_http_respond() may be called at a later time, and tf_http_request_unref() must eventually be called in order to not leak the request.

Parameters
requestThe request to retain.

◆ tf_http_request_unref()

void tf_http_request_unref ( tf_http_request_t request)

Decrement a requests refcount. tf_http_request_ref() must have been previously called.

Parameters
requestThe request.

◆ tf_http_request_websocket_send()

void tf_http_request_websocket_send ( tf_http_request_t request,
int  op_code,
const void *  data,
size_t  size 
)

Send a websocket message.

Parameters
requestThe HTTP request which was previously updated to a websocket session with tf_http_request_websocket_upgrade().
op_codeWebsocket op code.
dataThe message data.
sizeThe size of data.

◆ tf_http_request_websocket_upgrade()

void tf_http_request_websocket_upgrade ( tf_http_request_t request)

Upgrade an HTTP request to a websocket session.

Parameters
requestThe HTTP request.

◆ tf_http_respond()

void tf_http_respond ( tf_http_request_t request,
int  status,
const char **  headers,
int  headers_count,
const void *  body,
size_t  content_length 
)

Respond to an HTTP request.

Parameters
requestThe request.
statusThe HTTP status with which to respond.
headersHeaders to include in the response. Content-Length will be added internally.
headers_countThe number of headers. The headers array must have twice as many entries as this value, since it is both keys and values.
bodyThe response body or NULL.
content_lengthThe length of the response body.

◆ tf_http_set_trace()

void tf_http_set_trace ( tf_http_t http,
tf_trace_t trace 
)

Register a trace instance with the HTTP instance to record the begin and end time of request handlers.

Parameters
httpThe HTTP instance to trace.
traceThe trace instance to use, or NULL to disable.

◆ tf_http_set_user_data()

void tf_http_set_user_data ( tf_http_t http,
void *  user_data,
tf_http_cleanup_t cleanup 
)

Set instance-wide HTTP user data and a callback to clean it up.

Parameters
httpThe HTTP instance.
user_dataThe user data.
cleanupThe cleanup callback.

◆ tf_http_status_text()

const char * tf_http_status_text ( int  status)

Get standard HTTP status text for common HTTP statuses. 200 = "OK", 404 = "File not found", etc.

Parameters
statusThe HTTP status.
Returns
The status text or NULL.