tildefriends/src/http.h

240 lines
7.8 KiB
C
Raw Normal View History

#pragma once
/**
** \defgroup http HTTP Server
** 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.
** @{
*/
#include <stdbool.h>
#include <stddef.h>
2024-03-03 12:35:10 -05:00
/** An HTTP connection. */
typedef struct _tf_http_connection_t tf_http_connection_t;
2024-03-03 12:35:10 -05:00
/** An HTTP request. */
typedef struct _tf_http_request_t tf_http_request_t;
2024-03-03 12:35:10 -05:00
/** An HTTP instance. */
typedef struct _tf_http_t tf_http_t;
2024-03-03 12:35:10 -05:00
/** A TLS context. */
typedef struct _tf_tls_context_t tf_tls_context_t;
2024-03-03 12:35:10 -05:00
/** A trace instance. */
typedef struct _tf_trace_t tf_trace_t;
2024-03-03 12:35:10 -05:00
/** An event loop. */
typedef struct uv_loop_s uv_loop_t;
2024-03-03 12:35:10 -05:00
/**
** A callback called when receiving a websocket message.
** @param request The HTTP request.
** @param op_code The type of websocket message.
** @param data The payload.
** @param size The size of the payload in bytes.
*/
typedef void(tf_http_message_callback)(tf_http_request_t* request, int op_code, const void* data, size_t size);
2024-03-03 12:35:10 -05:00
/**
** A callback called when a request closes.
** @param request The HTTP request.
*/
typedef void(tf_http_close_callback)(tf_http_request_t* request);
2024-03-03 12:35:10 -05:00
/**
** A callback called when an HTTP request is received.
** @param request The HTTP request.
*/
typedef void(tf_http_callback_t)(tf_http_request_t* request);
/**
** A callback called when the HTTP instance is destroyed.
** @param user_data User data provided with the callback.
*/
typedef void(tf_http_cleanup_t)(void* user_data);
/**
** An HTTP request.
*/
typedef struct _tf_http_request_t
{
2024-03-03 12:35:10 -05:00
/** The HTTP instance this request belongs to. */
tf_http_t* http;
2024-03-03 12:35:10 -05:00
/** The HTTP connection associated with this request. */
tf_http_connection_t* connection;
2024-03-03 12:35:10 -05:00
/** True if this is an HTTPS session. */
bool is_tls;
2024-03-03 12:35:10 -05:00
/** The HTTP method of the request (GET/POST/...). */
const char* method;
2024-03-03 12:35:10 -05:00
/** The HTTP request path. */
const char* path;
2024-03-03 12:35:10 -05:00
/** The raw HTTP query string. */
const char* query;
2024-03-03 12:35:10 -05:00
/** The HTTP request body. */
void* body;
2024-03-03 12:35:10 -05:00
/** The length of the HTTP request body. */
size_t content_length;
2024-03-03 12:35:10 -05:00
/** Header storage. Can also be accessed with tf_http_request_get_header(). */
struct phr_header* headers;
2024-03-03 12:35:10 -05:00
/** The number of headers stored. */
int headers_count;
2024-03-03 12:35:10 -05:00
/** A callback to be called when receiving a websocket message. */
tf_http_message_callback* on_message;
2024-03-03 12:35:10 -05:00
/** A callback to be called when the connection is closed. */
tf_http_close_callback* on_close;
2024-03-03 12:35:10 -05:00
/** Extra storage for user data. */
void* context;
2024-03-03 12:35:10 -05:00
/** User data available to callbacks. */
void* user_data;
2024-03-03 12:35:10 -05:00
/** The number of times tf_http_request_ref() has been called without tf_http_request_unref(). */
int ref_count;
} tf_http_request_t;
/**
** Create an HTTP server using the given libuv loop.
** @param loop A libuv loop to use.
** @return An HTTP server instance.
*/
tf_http_t* tf_http_create(uv_loop_t* loop);
2024-03-03 12:35:10 -05:00
/**
** Register a trace instance with the HTTP instance to record the begin and end
** time of request handlers.
** @param http The HTTP instance to trace.
** @param trace The trace instance to use, or NULL to disable.
*/
void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace);
2024-03-03 12:35:10 -05:00
/**
** Begin listening for HTTP requests on a new port. May be called multiple
** times to listen on multiple ports.
** @param http The HTTP instance.
** @param port The port on which to listen, or 0 to assign a free port.
** @param tls An optional TLS context to use for HTTPS requests.
** @param cleanup A function called when the HTTP instance is being cleaned up.
** @param user_data User data passed to the cleanup callback.
** @return The port number on which the HTTP instance is now listening.
*/
int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls, tf_http_cleanup_t* cleanup, void* user_data);
2024-03-03 12:35:10 -05:00
/**
** Add an HTTP request handler.
** @param http The HTTP instance.
** @param pattern The prefix that the path of incoming requests must match to use this handler.
** @param callback The function to be called to handle the request.
** @param cleanup A function to be called when the request is complete.
** @param user_data User data to pass to the callbacks.
*/
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);
2024-03-03 12:35:10 -05:00
/**
** Respond to an HTTP request.
** @param request The request.
** @param status The HTTP status with which to respond.
** @param headers Headers to include in the response. Content-Length will be added internally.
** @param headers_count The number of headers. The headers array must have
** twice as many entries as this value, since it is both keys and values.
** @param body The response body or NULL.
** @param content_length The length of the response body.
*/
void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length);
2024-03-03 12:35:10 -05:00
/**
** Get the request body content.
** @param request An incoming request.
** @param out_data The request body content. Valid until the request is completed.
** @return The size of the request body content.
*/
size_t tf_http_get_body(const tf_http_request_t* request, const void** out_data);
2024-03-03 12:35:10 -05:00
/**
** Destroy an HTTP instance.
** @param http The HTTP instance.
*/
void tf_http_destroy(tf_http_t* http);
2024-03-03 12:35:10 -05:00
/**
** Set instance-wide HTTP user data and a callback to clean it up.
** @param http The HTTP instance.
** @param user_data The user data.
** @param cleanup The cleanup callback.
*/
void tf_http_set_user_data(tf_http_t* http, void* user_data, tf_http_cleanup_t* cleanup);
2024-03-03 12:35:10 -05:00
/**
** Get HTTP instance user data previous set with tf_http_set_user_data().
** @param http The HTTP instance.
** @return The user data.
*/
void* tf_http_get_user_data(tf_http_t* http);
2024-03-03 12:35:10 -05:00
/**
** 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.
** @param request The request to retain.
*/
void tf_http_request_ref(tf_http_request_t* request);
2024-03-03 12:35:10 -05:00
/**
** Decrement a requests refcount. tf_http_request_ref() must have been previously called.
** @param request The request.
*/
void tf_http_request_unref(tf_http_request_t* request);
2024-03-03 12:35:10 -05:00
/**
** Get the value of a header from an HTTP request.
** @param request The request.
** @param name The header key. Matched case insensitively.
** @return The value or NULL.
*/
const char* tf_http_request_get_header(tf_http_request_t* request, const char* name);
2024-03-03 12:35:10 -05:00
2024-03-31 16:15:50 -04:00
/**
** Get a cookie value from request headers.
** @param cookie_header The value of the "Cookie" header of the form
** "name1=value1; name2=value2".
** @param name The cookie name.
** @return The cookie value, if found, or NULL. Must be freed with tf_free().
*/
const char* tf_http_get_cookie(const char* cookie_header, const char* name);
2024-03-03 12:35:10 -05:00
/**
** Send a websocket message.
** @param request The HTTP request which was previously updated to a websocket
** session with tf_http_request_websocket_upgrade().
** @param data The message data.
** @param size The size of data.
*/
void tf_http_request_send(tf_http_request_t* request, const void* data, size_t size);
2024-03-03 12:35:10 -05:00
/**
** Upgrade an HTTP request to a websocket session.
** @param request The HTTP request.
*/
void tf_http_request_websocket_upgrade(tf_http_request_t* request);
2024-03-03 12:35:10 -05:00
/**
** Get standard HTTP status text for common HTTP statuses. 200 = "OK", 404 =
** "File not found", etc.
** @param status The HTTP status.
** @return The status text or NULL.
*/
const char* tf_http_status_text(int status);
/**
** Match URL patterns. "*" matches anything, and "{word}" matches [a-zA-Z][a-zA-Z0-9]*".
** @param pattern The pattern to match.
** @param path The path to test.
** @return true if the path matches the pattern.
*/
bool tf_http_pattern_matches(const char* pattern, const char* path);
/** @} */