174 lines
4.1 KiB
C
174 lines
4.1 KiB
C
#ifndef LANDER_HTTP_TYPES
|
|
#define LANDER_HTTP_TYPES
|
|
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
|
|
// Array mapping the http_request_method enum to strings
|
|
extern const char *http_method_names[];
|
|
extern const size_t http_method_names_len;
|
|
|
|
typedef enum http_method {
|
|
http_get = 0,
|
|
http_post = 1,
|
|
http_put = 2,
|
|
http_patch = 3,
|
|
http_delete = 4
|
|
} http_method;
|
|
|
|
// Array mapping the http_response_type enum to strings
|
|
extern const char *http_status_names[][32];
|
|
|
|
typedef enum http_status {
|
|
// 1xx
|
|
http_continue = 100,
|
|
http_switching_protocols = 101,
|
|
http_processing = 102,
|
|
http_early_hints = 103,
|
|
// 2xx
|
|
http_ok = 200,
|
|
http_created = 201,
|
|
http_accepted = 202,
|
|
http_non_authoritative_information = 203,
|
|
http_no_content = 204,
|
|
http_reset_content = 205,
|
|
http_partial_content = 206,
|
|
http_multi_status = 207,
|
|
http_already_reported = 208,
|
|
// 3xx
|
|
http_multiple_choices = 300,
|
|
http_moved_permanently = 301,
|
|
http_found = 302,
|
|
http_see_other = 303,
|
|
http_not_modified = 304,
|
|
http_temporary_redirect = 307,
|
|
http_permanent_redirect = 308,
|
|
// 4xx
|
|
http_bad_request = 400,
|
|
http_unauthorized = 401,
|
|
http_payment_required = 402,
|
|
http_forbidden = 403,
|
|
http_not_found = 404,
|
|
http_method_not_allowed = 405,
|
|
http_not_acceptable = 406,
|
|
http_proxy_authentication_required = 407,
|
|
http_request_timeout = 408,
|
|
http_conflict = 409,
|
|
http_gone = 410,
|
|
http_length_required = 411,
|
|
http_precondition_failed = 412,
|
|
http_content_too_large = 413,
|
|
http_uri_too_long = 414,
|
|
http_unsupported_media_type = 415,
|
|
http_range_not_satisfiable = 416,
|
|
http_expection_failed = 417,
|
|
http_im_a_teapot = 418,
|
|
http_misdirected_request = 421,
|
|
http_unprocessable_content = 422,
|
|
http_locked = 423,
|
|
http_failed_dependency = 424,
|
|
http_too_early = 425,
|
|
http_upgrade_required = 426,
|
|
http_precondition_required = 428,
|
|
http_too_many_requests = 429,
|
|
http_request_header_fields_too_large = 431,
|
|
// 5xx
|
|
http_internal_server_error = 500,
|
|
http_method_not_implemented = 501,
|
|
http_bad_gateway = 502,
|
|
http_service_unavailable = 503,
|
|
http_gateway_timeout = 504,
|
|
http_http_version_not_supported = 505,
|
|
http_variant_also_negotiates = 506,
|
|
http_insufficient_storage = 507,
|
|
http_loop_detected = 508,
|
|
http_not_extended = 510,
|
|
http_network_authentication_required = 511
|
|
} http_status;
|
|
|
|
// Array mapping the http_mime_type enum to strings
|
|
extern const char *http_mime_type_names[][2];
|
|
|
|
typedef enum http_mime_type {
|
|
http_mime_aac = 0,
|
|
http_mime_bz,
|
|
http_mime_bz2,
|
|
http_mime_css,
|
|
http_mime_csv,
|
|
http_mime_gz,
|
|
http_mime_gif,
|
|
http_mime_htm,
|
|
http_mime_html,
|
|
http_mime_jar,
|
|
http_mime_jpeg,
|
|
http_mime_jpg,
|
|
http_mime_js,
|
|
http_mime_json,
|
|
http_mime_mp3,
|
|
http_mime_mp4,
|
|
http_mime_png,
|
|
http_mime_pdf,
|
|
http_mime_rar,
|
|
http_mime_sh,
|
|
http_mime_svg,
|
|
http_mime_tar,
|
|
http_mime_txt,
|
|
http_mime_wav,
|
|
http_mime_7z
|
|
} http_mime_type;
|
|
|
|
// Array mapping the http_header enum to strings
|
|
extern const char *http_header_names[];
|
|
|
|
typedef enum http_header {
|
|
http_header_connection = 0,
|
|
http_header_location,
|
|
http_header_content_type,
|
|
http_header_content_disposition
|
|
} http_header;
|
|
|
|
typedef enum http_body_type {
|
|
http_body_buf = 0,
|
|
http_body_file = 1
|
|
} http_body_type;
|
|
|
|
typedef struct http_body {
|
|
http_body_type type;
|
|
char *buf;
|
|
bool buf_owned;
|
|
FILE *file;
|
|
char *fname;
|
|
bool fname_owned;
|
|
// In the context of a request, expected_len is the content of the request's
|
|
// Content-Length header, and len is how many bytes have already been
|
|
// received.
|
|
// In the context of a response, expected_len is the actual length of the
|
|
// body, and len is how many have been written.
|
|
size_t expected_len;
|
|
size_t len;
|
|
} http_body;
|
|
|
|
/**
|
|
* Initialize a new body struct.
|
|
*
|
|
* @return pointer to the newly allocated object.
|
|
*/
|
|
http_body *http_body_init();
|
|
|
|
/**
|
|
* Reset a body, allowing it to be reused as if newly allocated.
|
|
*
|
|
* @param body body to reset
|
|
*/
|
|
void http_body_reset(http_body *body);
|
|
|
|
/**
|
|
* Free a body. Internally, this calls http_body_reset.
|
|
*
|
|
* @param body body to free
|
|
*/
|
|
void http_body_free(http_body *body);
|
|
|
|
#endif
|