feat(http): add custom processing to responses using response steps

This commit is contained in:
Jef Roosens 2023-11-02 10:27:34 +01:00
parent 6d74c8c550
commit afd18d3a37
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 128 additions and 66 deletions

View file

@ -27,9 +27,10 @@ typedef enum http_route_type {
* Function describing a step in a route's processing.
*
* @param conn connection to process
* @return whether the processing can immediately advance to the next step. A
* step should return false if it's e.g. waiting for I/O, and can therefore not
* finish its task in the current cycle of the event loop.
* @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 (*step)(event_loop_conn *conn);
@ -44,6 +45,7 @@ typedef struct http_route {
// starting the http loop
regex_t *regex;
step steps[HTTP_LOOP_MAX_STEPS];
step steps_res[HTTP_LOOP_MAX_STEPS];
} http_route;
/**
@ -116,14 +118,6 @@ typedef struct event_loop http_loop;
*/
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.
*
* @param conn connection to process
*/
void http_loop_write_response(event_loop_conn *conn);
/**
* Try to parse the incoming data as an HTTP request.
*
@ -148,6 +142,14 @@ void http_loop_route_request(event_loop_conn *conn);
*/
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.
*
@ -180,6 +182,21 @@ bool http_loop_step_auth(event_loop_conn *conn);
*/
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.
*