chore: properly compile picohttpparser in lnm; remove old http header
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/docker Pipeline was successful

files
This commit is contained in:
Jef Roosens 2023-12-07 11:02:20 +01:00
parent c59dd29648
commit cc9dc6aec5
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
18 changed files with 30 additions and 1484 deletions

View file

@ -1,137 +0,0 @@
#ifndef LANDER_EVENT_LOOP
#define LANDER_EVENT_LOOP
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
// Size of the read and write buffers for each connection, in bytes
#define EVENT_LOOP_BUFFER_SIZE 2048
/**
* State of a connection
*/
typedef enum {
event_loop_conn_state_req = 0,
event_loop_conn_state_res = 1,
event_loop_conn_state_end = 2,
} event_loop_conn_state;
/**
* Represents an active connection managed by the event loop
*/
typedef struct event_loop_conn {
int fd;
event_loop_conn_state state;
// Read buffer
size_t rbuf_size;
size_t rbuf_read;
uint8_t rbuf[EVENT_LOOP_BUFFER_SIZE];
// Write buffer
size_t wbuf_size;
size_t wbuf_sent;
uint8_t wbuf[EVENT_LOOP_BUFFER_SIZE];
// If true, the server will close the connection after the final write buffer
// has been written
bool close_after_write;
// Context for a request
void *ctx;
} event_loop_conn;
/*
* Main struct object representing the event loop
*/
typedef struct event_loop {
event_loop_conn **connections;
size_t connection_count;
// Global context passed to every connection
void *gctx;
/**
* Function to initialize a connection context.
*
* @param gctx global context of the event loop
* @return pointer to the allocated object.
*/
void *(*ctx_init)(void *gctx);
/**
* Function to free a connection context object.
*
* @param ctx context to free
*/
void (*ctx_free)(void *ctx);
/**
* Function to process incoming data while in the req state.
*
* @param conn connection to process
* @return whether the function can be called again immediately in the same
* event loop cycle. This allows quicly processing multiple requests without
* waiting for I/O.
*/
bool (*handle_data)(event_loop_conn *conn);
/**
* Function to process outgoing data while in the res state.
*
* @param conn connection to proces
*/
void (*write_data)(event_loop_conn *conn);
} event_loop;
/*
* Initialize a new connection struct
*
* @param el the event loop
* @return pointer to the newly allocated connection struct
*/
event_loop_conn *event_loop_conn_init(event_loop *el);
/*
* Free a connection struct
*
* @param el the event loop
* @param conn connection struct to free
*/
void event_loop_conn_free(event_loop *el, event_loop_conn *conn);
/*
* Handle I/O for a connection, be it reading input or writing output.
*
* @param el the event loop
* @param conn the connection to process
*/
void event_loop_conn_io(event_loop *el, event_loop_conn *conn);
/*
* Initialize a new event loop struct
*
* @return pointer to the newly allocated event loop struct
*/
event_loop *event_loop_init();
/*
* Place a new connection into the event loop's internal array.
*
* @param el the event loop
* @param conn connection to insert
* @return 0 on success, -1 if the internal realloc failed.
*/
int event_loop_put(event_loop *el, event_loop_conn *conn);
/**
* Accept a new connection for the given file descriptor.
*
* @param el the event loop
* @param fd file descriptor for the connection
* @return 0 if successful, negative value otherwise
*/
int event_loop_accept(event_loop *el, int fd);
/*
* Run the event loop. This function never returns.
*
* @param el the event loop
* @param port on what port to listen
*/
void event_loop_run(event_loop *el, int port);
#endif

View file

@ -1,42 +0,0 @@
#ifndef LANDER_HTTP_REQ
#define LANDER_HTTP_REQ
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "http/types.h"
#include "picohttpparser.h"
#define HTTP_MAX_ALLOWED_HEADERS 32
#define HTTP_MAX_REGEX_GROUPS 4
/**
* Struct representing the specific type of request
*/
typedef struct http_request {
size_t len;
int minor_version;
http_method method;
const char *path;
size_t path_len;
const char *query;
size_t query_len;
http_body body;
regmatch_t regex_groups[HTTP_MAX_REGEX_GROUPS];
struct phr_header headers[HTTP_MAX_ALLOWED_HEADERS];
size_t num_headers;
} http_request;
/**
* Result of the HTTP parse function
*/
typedef enum http_parse_error {
http_parse_error_ok = 0,
http_parse_error_incomplete = 1,
http_parse_error_invalid = 2,
http_parse_error_unknown_method = 3
} http_parse_error;
#endif

View file

@ -1,72 +0,0 @@
#ifndef LANDER_HTTP_RES
#define LANDER_HTTP_RES
#include <stdbool.h>
#include <stdio.h>
#include "http/types.h"
/**
* Struct describing a header for the response.
*/
typedef struct http_response_header {
http_header type;
const char *value;
bool owned;
} http_response_header;
/**
* Struct representing an HTTP response.
*/
typedef struct http_response {
http_status status;
const char *head;
size_t head_len;
size_t head_written;
http_body body;
struct {
http_response_header *arr;
size_t len;
size_t cap;
} headers;
} http_response;
/**
* Set the request body to the given buffer.
*
* @param res response to modify
* @param body pointer to the buf containing the body
* @param body_len length of the body
* @owned whether the body should be freed after processing the request
*/
void http_res_set_body_buf(http_response *res, const char *body,
size_t body_len, bool owned);
/**
* Set the request body to the given filename.
*
* @param res response to modify
* @param filename path to the file to return
*/
void http_res_set_body_file(http_response *res, const char *filename);
/**
* Add a header to the response.
*
* @param res response to modify
* @param type type of the header
* @param value value of the header
* @param owned whether the value should be freed after processing the request
*/
void http_res_add_header(http_response *res, http_header type,
const char *value, bool owned);
/**
* Add a Content-Type header corresponding to the mime type.
*
* @param res response to modiy
* @param mime_type mime type of the response
*/
void http_res_set_mime_type(http_response *res, http_mime_type mime_type);
#endif

View file

@ -1,175 +0,0 @@
#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_server,
http_header_content_length
} 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

View file

@ -1,241 +0,0 @@
#ifndef LANDER_HTTP_LOOP
#define LANDER_HTTP_LOOP
#include <regex.h>
#include "event_loop.h"
#include "http/req.h"
#include "http/res.h"
#include "http/types.h"
// Max amount of steps a route can use
#define HTTP_LOOP_MAX_STEPS 17
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/**
* Type of a route
*/
typedef enum http_route_type {
http_route_literal = 0,
http_route_regex = 1,
} http_route_type;
/**
* Function describing a step in a route's processing.
*
* @param conn connection to process
* @return whether processing can proceed to the next step without performing
* I/O first. For a request step, `false` means more data needs to be read
* before the step can finish its processing. For response steps, `false` means
* there's new data in the write buffer that needs to be written.
*/
typedef bool (*http_step)(event_loop_conn *conn);
extern const http_step http_default_res_steps[HTTP_LOOP_MAX_STEPS];
/**
* Struct describing a route a request can take.
*/
typedef struct http_route {
http_route_type type;
http_method method;
char *path;
// Compiled regex for a regex route. This value gets set at runtime when
// starting the http loop
regex_t *regex;
const http_step steps[HTTP_LOOP_MAX_STEPS];
const http_step steps_res[HTTP_LOOP_MAX_STEPS];
} http_route;
/**
* Global context passed to every connection using the same pointer
*/
typedef struct http_loop_gctx {
http_route *routes;
size_t route_count;
void *(*custom_ctx_init)();
void (*custom_ctx_reset)(void *);
void (*custom_ctx_free)(void *);
const char *api_key;
// Custom global context
void *c;
} http_loop_gctx;
/**
* Initialize a new global context
*
* @return pointer to the newly allocated object.
*/
http_loop_gctx *http_loop_gctx_init();
/**
* Invidivual context initialized for every connection
*/
typedef struct http_loop_ctx {
http_request req;
http_response res;
http_route *route;
size_t current_step;
http_loop_gctx *g;
void *c;
} http_loop_ctx;
/**
* Initialize a context struct
*
* @param g global context
* @return pointer to the newly allocated context
*/
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g);
/**
* Resets an already allocated context so that it can be reused for a new
* request.
*
* @param ctx context to reset
*/
void http_loop_ctx_reset(http_loop_ctx *ctx);
/**
* Free a context struct. Internally this first calls http_loop_ctx_reset.
*
* @param ctx context to free
*/
void http_loop_ctx_free(http_loop_ctx *ctx);
/**
* Represents an HTTP loop
*/
typedef struct event_loop http_loop;
/**
* Process incoming data as an HTTP request. This is the "handle_data" function
* for the event loop.
*
* @param conn connection to process
* @return whether another request can be processed immediately.
*/
bool http_loop_handle_request(event_loop_conn *conn);
/**
* Try to parse the incoming data as an HTTP request.
*
* @param conn connection to process
* @return result of the parse
*/
http_parse_error http_loop_parse_request(event_loop_conn *conn);
/**
* Try to match the parsed request with one of the defined routes, aka route the
* request.
*
* @param conn connection to process
*/
void http_loop_route_request(event_loop_conn *conn);
/**
* Advance the processing of the routed request's processing by cycling through
* the request's various steps.
*
* @param conn connection to process
*/
void http_loop_process_request(event_loop_conn *conn);
/**
* Handles the response processing. This is the `write_data` function for the
* event loop.
*
* @param conn connection to process
*/
void http_loop_handle_response(event_loop_conn *conn);
/**
* Request step that consumes the request body and stores it in a buffer.
*
* @param conn connection to process
* @return true if the body has been fully received, false otherwise
*/
bool http_loop_step_body_to_buf(event_loop_conn *conn);
/**
* Request step that consumes the request body and stores it in a file.
*
* @param conn connection to process
* @return true if the body has been fully received, false otherwise
*/
bool http_loop_step_body_to_file(event_loop_conn *conn);
/**
* Try to parse the Content-Length header.
*
* @param conn connection to process
*/
bool http_loop_step_parse_content_length(event_loop_conn *conn);
/**
* Authenticate the request using the X-Api-Key header.
*
* @param conn connection to check
* @return always true
*/
bool http_loop_step_auth(event_loop_conn *conn);
/**
* A step that simply sets the connection's state to res.
*
* @param conn connection to process
* @return always true
*/
bool http_loop_step_switch_res(event_loop_conn *conn);
/**
* Write the HTTP header back to the connection. If `res->head` is not set, a
* header will be generated for you.
*
* @param conn connection to process
*/
bool http_loop_step_write_header(event_loop_conn *conn);
/**
* Write the HTTP body back to the connection.
*
* @param conn connection to process
*/
bool http_loop_step_write_body(event_loop_conn *conn);
/**
* Initialize a new http loop.
*
* @param routes array of routes that should be served
* @param route_count how many elements are in `routes`
* @param custom_gctx the application's custom global context; can be NULL
* @param custom_ctx_init function to initialize a new custom context
* @param custom_ctx_reset function to reset a custom context
* @param custom_ctx_free function to free a custom context; will always be run
* after a reset
* @return pointer to the newly allocated object
*/
http_loop *http_loop_init(http_route *routes, size_t route_count,
void *custom_gctx, void *(*custom_ctx_init)(),
void(custom_ctx_reset)(void *),
void(custom_ctx_free)(void *));
/**
* Set the API key the authentication steps should use.
*
* @param hl HTTP loop to set key in
* @param api_key API key to use
*/
void http_loop_set_api_key(http_loop *hl, const char *api_key);
/**
* Run the HTTP loop. This function never returns.
*
* @param el the event loop
* @param port on what port to listen
*/
void http_loop_run(http_loop *hl, int port);
#endif

View file

@ -5,9 +5,6 @@
#include "lnm/http/loop.h"
#include "lsm/store.h"
#include "http_loop.h"
extern http_route lander_routes[6];
extern const char lander_key_charset[];
typedef struct lander_gctx {
@ -50,8 +47,6 @@ lnm_http_step_err lander_post_paste(lnm_http_conn *conn);
lnm_http_step_err lander_stream_body_to_entry(lnm_http_conn *conn);
bool lander_stream_body_to_client(event_loop_conn *conn);
lnm_http_step_err lander_post_redirect_body_to_attr(lnm_http_conn *conn);
lnm_http_step_err lander_remove_entry(lnm_http_conn *conn);