lander/include/http_loop.h

131 lines
3.0 KiB
C
Raw Normal View History

2023-05-27 11:47:39 +02:00
#ifndef LANDER_HTTP_LOOP
#define LANDER_HTTP_LOOP
2023-05-27 15:38:06 +02:00
#include <regex.h>
2023-05-27 11:47:39 +02:00
#include "event_loop.h"
2023-05-27 15:38:06 +02:00
#include "http.h"
2023-05-27 11:47:39 +02:00
#include "trie.h"
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
2023-05-27 15:38:06 +02:00
typedef enum http_route_type {
http_route_literal = 0,
http_route_regex = 1,
} http_route_type;
typedef struct http_route {
http_route_type type;
2023-05-29 12:43:50 +02:00
http_request_method method;
2023-05-27 15:38:06 +02:00
char *path;
regex_t *regex;
2023-05-27 16:42:15 +02:00
bool (*steps[5])(event_loop_conn *);
2023-05-27 15:38:06 +02:00
} http_route;
2023-05-27 11:47:39 +02:00
/*
* Global context passed to every connection using the same pointer
*/
typedef struct http_loop_gctx {
2023-05-27 16:42:15 +02:00
http_route *routes;
size_t route_count;
2023-05-27 11:47:39 +02:00
Trie *trie;
2023-05-29 23:37:23 +02:00
const char *api_key;
2023-05-27 11:47:39 +02:00
} http_loop_gctx;
2023-05-27 15:38:06 +02:00
/*
* Initialize a new global context
*/
2023-05-27 11:47:39 +02:00
http_loop_gctx *http_loop_gctx_init();
/*
* Invidivual context initialized for every connection
*/
typedef struct http_loop_ctx {
http_request req;
http_response res;
2023-05-27 15:38:06 +02:00
http_route *route;
size_t current_step;
2023-05-27 11:47:39 +02:00
http_loop_gctx *g;
} http_loop_ctx;
2023-05-27 15:38:06 +02:00
/*
* Initialize a context struct
*/
2023-05-27 11:47:39 +02:00
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.
*/
void http_loop_ctx_reset(http_loop_ctx *ctx);
2023-05-27 15:38:06 +02:00
/*
* Free a context struct
*/
2023-05-27 11:47:39 +02:00
void http_loop_ctx_free(http_loop_ctx *ctx);
/*
* Process incoming data as an HTTP request. This is the "handle_data" function
* for the event loop.
2023-05-27 15:38:06 +02:00
*/
2023-05-27 11:47:39 +02:00
bool http_loop_handle_request(event_loop_conn *conn);
/*
* Write the HTTP response to the file descriptor. This is the "write_data"
* function for the event loop.
*/
void http_loop_write_response(event_loop_conn *conn);
/*
* Try to parse the incoming data as an HTTP request.
*/
2023-05-27 17:14:49 +02:00
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.
*/
void http_loop_route_request(event_loop_conn *conn);
2023-05-27 16:42:15 +02:00
/*
* Advance the processing of the routed request's processing by cycling through
* the request's various steps.
*/
2023-05-27 15:38:06 +02:00
void http_loop_process_request(event_loop_conn *conn);
/*
* Set the request body to the given buffer. If owned is set to true, the body
* buffer will be free'd after the request has finished.
*/
2023-05-29 17:53:57 +02:00
void http_loop_res_set_body_buf(http_loop_ctx *ctx, const char *body,
size_t body_len, bool owned);
void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename);
2023-05-28 10:23:32 +02:00
2023-05-29 15:34:46 +02:00
void http_loop_res_add_header(http_loop_ctx *ctx, http_header type,
const char *value, bool owned);
/*
* Request step that consumes the request body and stores it in a buffer
*/
bool http_loop_step_body_to_buf(event_loop_conn *conn);
2023-05-30 10:17:46 +02:00
bool http_loop_step_body_to_file(event_loop_conn *conn);
2023-05-29 23:37:23 +02:00
/*
* Authenticate the request using the X-Api-Key header
*/
bool http_loop_step_auth(event_loop_conn *conn);
2023-05-30 10:17:46 +02:00
bool http_loop_step_switch_res(event_loop_conn *conn);
2023-05-27 15:38:06 +02:00
/**
* Initialize a new http loop
*/
2023-05-27 11:47:39 +02:00
event_loop *http_loop_init(http_loop_gctx *gctx);
2023-05-29 16:55:16 +02:00
void http_loop_run(event_loop *el, int port);
2023-05-27 11:47:39 +02:00
#endif