#ifndef LANDER_HTTP_LOOP #define LANDER_HTTP_LOOP #include #include "event_loop.h" #include "http.h" #include "trie.h" typedef enum http_route_type { http_route_literal = 0, http_route_regex = 1, } http_route_type; typedef struct http_route { http_route_type type; char *path; regex_t *regex; bool (*steps[5])(event_loop_conn *); } http_route; /* * Global context passed to every connection using the same pointer */ typedef struct http_loop_gctx { http_route *routes; size_t route_count; Trie *trie; } http_loop_gctx; /* * Initialize a new global context */ 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; } http_loop_ctx; /* * Initialize a context struct */ http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g); void http_loop_ctx_reset(http_loop_ctx *ctx); /* * Free a context struct */ void http_loop_ctx_free(http_loop_ctx *ctx); /** * Process incoming data as an HTTP request */ bool http_loop_handle_request(event_loop_conn *conn); void http_loop_write_response(event_loop_conn *conn); http_parse_error http_loop_parse_request(event_loop_conn *conn); void http_loop_route_request(event_loop_conn *conn); void http_loop_process_request(event_loop_conn *conn); void http_loop_res_set_body(const char *body, size_t body_len); void http_loop_res_set_type(http_response_type type); void http_loop_write_standard_response(event_loop_conn *conn, http_response_type type); /** * Initialize a new http loop */ event_loop *http_loop_init(http_loop_gctx *gctx); #endif