From c7b3f68c2ea6f2d5a2842d6df7b87c6c54002abf Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 22 Nov 2023 13:17:46 +0100 Subject: [PATCH] chore(lnm): copied over http constants --- lnm/include/lnm/http/consts.h | 97 ++++++++++++++++++++++++++ lnm/include/lnm/loop.h | 4 +- lnm/src/_include/lnm/loop_internal.h | 5 ++ lnm/src/http/lnm_http_consts.c | 100 +++++++++++++++++++++++++++ lnm/src/loop/lnm_loop.c | 8 ++- 5 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 lnm/include/lnm/http/consts.h create mode 100644 lnm/src/http/lnm_http_consts.c diff --git a/lnm/include/lnm/http/consts.h b/lnm/include/lnm/http/consts.h new file mode 100644 index 0000000..4b6bc92 --- /dev/null +++ b/lnm/include/lnm/http/consts.h @@ -0,0 +1,97 @@ +#ifndef LNM_HTTP_CONSTS +#define LNM_HTTP_CONSTS + +#include + +extern const char *lnm_http_method_names[]; +extern const size_t lnm_http_method_names_len; + +typedef enum lnm_http_method { + http_method_get = 0, + http_method_post, + http_method_put, + http_method_patch, + http_method_delete +} http_method; + +extern const char *lnm_http_status_names[][32]; + +typedef enum lnm_http_status { + // 1xx + lnm_http_status_continue = 100, + lnm_http_status_switching_protocols = 101, + lnm_http_status_processing = 102, + lnm_http_status_early_hints = 103, + // 2xx + lnm_http_status_ok = 200, + lnm_http_status_created = 201, + lnm_http_status_accepted = 202, + lnm_http_status_non_authoritative_information = 203, + lnm_http_status_no_content = 204, + lnm_http_status_reset_content = 205, + lnm_http_status_partial_content = 206, + lnm_http_status_multi_status = 207, + lnm_http_status_already_reported = 208, + // 3xx + lnm_http_status_multiple_choices = 300, + lnm_http_status_moved_permanently = 301, + lnm_http_status_found = 302, + lnm_http_status_see_other = 303, + lnm_http_status_not_modified = 304, + lnm_http_status_temporary_redirect = 307, + lnm_http_status_permanent_redirect = 308, + // 4xx + lnm_http_status_bad_request = 400, + lnm_http_status_unauthorized = 401, + lnm_http_status_payment_required = 402, + lnm_http_status_forbidden = 403, + lnm_http_status_not_found = 404, + lnm_http_status_method_not_allowed = 405, + lnm_http_status_not_acceptable = 406, + lnm_http_status_proxy_authentication_required = 407, + lnm_http_status_request_timeout = 408, + lnm_http_status_conflict = 409, + lnm_http_status_gone = 410, + lnm_http_status_length_required = 411, + lnm_http_status_precondition_failed = 412, + lnm_http_status_content_too_large = 413, + lnm_http_status_uri_too_long = 414, + lnm_http_status_unsupported_media_type = 415, + lnm_http_status_range_not_satisfiable = 416, + lnm_http_status_expection_failed = 417, + lnm_http_status_im_a_teapot = 418, + lnm_http_status_misdirected_request = 421, + lnm_http_status_unprocessable_content = 422, + lnm_http_status_locked = 423, + lnm_http_status_failed_dependency = 424, + lnm_http_status_too_early = 425, + lnm_http_status_upgrade_required = 426, + lnm_http_status_precondition_required = 428, + lnm_http_status_too_many_requests = 429, + lnm_http_status_request_header_fields_too_large = 431, + // 5xx + lnm_http_status_internal_server_error = 500, + lnm_http_status_method_not_implemented = 501, + lnm_http_status_bad_gateway = 502, + lnm_http_status_service_unavailable = 503, + lnm_http_status_gateway_timeout = 504, + lnm_http_status_http_status_version_not_supported = 505, + lnm_http_status_variant_also_negotiates = 506, + lnm_http_status_insufficient_storage = 507, + lnm_http_status_loop_detected = 508, + lnm_http_status_not_extended = 510, + lnm_http_status_network_authentication_required = 511 +} lnm_http_status; + +extern const char *lnm_http_header_names[]; + +typedef enum lnm_http_header { + lnm_http_header_connection = 0, + lnm_http_header_location, + lnm_http_header_content_type, + lnm_http_header_content_disposition, + lnm_http_header_server, + lnm_http_header_content_length +} lnm_http_header; + +#endif diff --git a/lnm/include/lnm/loop.h b/lnm/include/lnm/loop.h index d70057e..a3625c6 100644 --- a/lnm/include/lnm/loop.h +++ b/lnm/include/lnm/loop.h @@ -46,7 +46,9 @@ typedef struct { lnm_err lnm_loop_init(lnm_loop **out, void *gctx, lnm_err (*ctx_init)(void **out, void *gctx), - void (*ctx_free)(void *ctx)); + void (*ctx_free)(void *ctx), + void (*data_read)(lnm_loop_conn *conn), + void (*data_write)(lnm_loop_conn *conn)); lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port); diff --git a/lnm/src/_include/lnm/loop_internal.h b/lnm/src/_include/lnm/loop_internal.h index 6b723fe..a5e70a8 100644 --- a/lnm/src/_include/lnm/loop_internal.h +++ b/lnm/src/_include/lnm/loop_internal.h @@ -1,3 +1,6 @@ +#ifndef LNM_LOOP_INTERNAL +#define LNM_LOOP_INTERNAL + #include "lnm/loop.h" lnm_err lnm_loop_conn_init(lnm_loop_conn **out, lnm_loop *l); @@ -7,3 +10,5 @@ void lnm_loop_conn_free(lnm_loop *l, lnm_loop_conn *conn); lnm_err lnm_loop_accept(lnm_loop *l); void lnm_loop_conn_io(lnm_loop *l, lnm_loop_conn *conn); + +#endif diff --git a/lnm/src/http/lnm_http_consts.c b/lnm/src/http/lnm_http_consts.c new file mode 100644 index 0000000..834816a --- /dev/null +++ b/lnm/src/http/lnm_http_consts.c @@ -0,0 +1,100 @@ +#include "lnm/http/consts.h" + +const char *lnm_http_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"}; +const size_t lnm_http_method_names_len = + sizeof(lnm_http_method_names) / sizeof(lnm_http_method_names[0]); + +// clang-format off + +const char *lnm_http_status_names[][32] = { + // 1xx + { + "Continue", // 100 + "Switching Protocols", // 101, + "Processing", // 102 + "Early Hints", // 103 + }, + // 2xx + { + "OK", // 200 + "Created", // 201 + "Accepted", // 202 + "Non-Authoritative Information", // 203 + "No Content", // 204 + "Reset Content", // 205 + "Partial Content", // 206 + "Multi-Status", // 207 + "Already Reported", // 208 + }, + // 3xx + { + "Multiple Choices", // 300 + "Moved Permanently", // 301 + "Found", // 302 + "See Other", // 303 + "Not Modified", // 304 + NULL, // 305 + NULL, // 306 + "Temporary Redirect", // 307 + "Permanent Redirect", // 308 + }, + // 4xx + { + "Bad Request", // 400 + "Unauthorized", // 401 + "Payment Required", // 402 + "Forbidden", // 403 + "Not Found", // 404 + "Method Not Allowed", // 405 + "Not Acceptable", // 406 + "Proxy Authentication Required", // 407 + "Request Timeout", // 408 + "Conflict", // 409 + "Gone", // 410 + "Length Required", // 411 + "Precondition Failed", // 412 + "Content Too Large", // 413 + "URI Too Long", // 414 + "Unsupported Media Type", // 415 + "Range Not Satisfiable", // 416 + "Expectation Failed", // 417 + "I'm a teapot", // 418 + NULL, // 419 + NULL, // 420 + "Misdirected Request", // 421 + "Unprocessable Content", // 422 + "Locked", // 423 + "Failed Dependency", // 424 + "Too Early", // 425 + "Upgrade Required", // 426 + NULL, // 427 + "Precondition Required", // 428 + "Too Many Requests", // 429 + NULL, // 430 + "Request Header Fields Too Large", // 431 + }, + // 5xx + { + "Internal Server Error", // 500 + "Not Implemented", // 501 + "Bad Gateway", // 502 + "Service Unavailable", // 503 + "Gateway Timeout", // 504 + "HTTP Version Not Supported", // 505 + "Variant Also Negotiates", // 506 + "Insufficient Storage", // 507 + "Loop Detected", // 508 + NULL, // 509 + "Not Extended", // 510 + "Network Authentication Required" // 511 + }, +}; + +const char *lnm_http_header_names[] = { + "Connection", + "Location", + "Content-Type", + "Content-Disposition", + "Server", + "Content-Length" +}; diff --git a/lnm/src/loop/lnm_loop.c b/lnm/src/loop/lnm_loop.c index 63e3b34..c550009 100644 --- a/lnm/src/loop/lnm_loop.c +++ b/lnm/src/loop/lnm_loop.c @@ -8,7 +8,9 @@ lnm_err lnm_loop_init(lnm_loop **out, void *gctx, lnm_err (*ctx_init)(void **out, void *gctx), - void (*ctx_free)(void *ctx)) { + void (*ctx_free)(void *ctx), + void (*data_read)(lnm_loop_conn *conn), + void (*data_write)(lnm_loop_conn *conn)) { lnm_loop *l = calloc(1, sizeof(lnm_loop)); if (l == NULL) { @@ -18,6 +20,8 @@ lnm_err lnm_loop_init(lnm_loop **out, void *gctx, l->gctx = gctx; l->ctx_init = ctx_init; l->ctx_free = ctx_free; + l->data_read = data_read; + l->data_write = data_write; *out = l; @@ -121,7 +125,7 @@ lnm_err lnm_loop_run(lnm_loop *l) { // Add all open connections to the poll command for (size_t i = 0; i < l->conns.len && poll_args_len < l->conns.open + 1; i++) { - lnm_loop_conn *conn = l->conns.arr[i]; + const lnm_loop_conn *conn = l->conns.arr[i]; if (conn == NULL) { continue;