tildefriends/src/http.h

75 lines
2.5 KiB
C

#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>
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 struct _tf_http_request_t
{
tf_http_t* http;
tf_http_connection_t* connection;
bool is_tls;
const char* method;
const char* path;
const char* query;
void* body;
size_t content_length;
struct phr_header* headers;
int headers_count;
tf_http_message_callback* on_message;
tf_http_close_callback* on_close;
void* context;
void* user_data;
int ref_count;
} tf_http_request_t;
typedef void(tf_http_callback_t)(tf_http_request_t* request);
typedef void(tf_http_cleanup_t)(void* user_data);
/**
** 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);
void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace);
int tf_http_listen(tf_http_t* http, int port, 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);
void tf_http_request_send(tf_http_request_t* request, 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);
/** @} */