refactor: move some stuff, add some docs

c-web-server
Jef Roosens 2023-05-29 12:03:40 +02:00
parent 2fb3e2bb00
commit 9b66223a57
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 47 additions and 23 deletions

View File

@ -49,6 +49,10 @@ typedef struct http_loop_ctx {
*/
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);
/*
@ -56,25 +60,40 @@ void http_loop_ctx_reset(http_loop_ctx *ctx);
*/
void http_loop_ctx_free(http_loop_ctx *ctx);
/**
* Process incoming data as an HTTP request
/*
* Process incoming data as an HTTP request. This is the "handle_data" function
* for the event 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.
*/
void http_loop_write_response(event_loop_conn *conn);
/*
* Try to parse the incoming data as an HTTP request.
*/
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);
/*
* Advance the processing of the routed request's processing by cycling through
* the request's various steps.
*/
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);
/*
* 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.
*/
void http_loop_res_set_body(const char *body, size_t body_len, bool owned);
/**
* Initialize a new http loop

View File

@ -5,26 +5,31 @@
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
static const char *http_response_format = "HTTP/1.1 %i %s\n"
"Content-Length: %lu\n\n";
void http_loop_init_header(http_response *res) {
const char *response_type_name =
http_response_type_names[res->type / 100 - 1][res->type % 100];
// Running snprintf with size 0 prevents it from writing any bytes, while
// still letting it calculate how many bytes it would have written
int buf_size = snprintf(NULL, 0, http_response_format, res->type,
response_type_name, res->body_len);
char *buf = malloc(buf_size + 1);
sprintf(buf, http_response_format, res->type, response_type_name,
res->body_len);
res->head = buf;
res->head_len = buf_size;
}
void http_loop_write_response(event_loop_conn *conn) {
http_response *res = &((http_loop_ctx *)conn->ctx)->res;
// Create head response
if (res->head == NULL) {
const char *response_type_name =
http_response_type_names[res->type / 100 - 1][res->type % 100];
char *format = "HTTP/1.1 %i %s\n"
"Content-Length: %lu\n\n";
// Running snprintf with size 0 prevents it from writing any bytes, while
// still letting it calculate how many bytes it would have written
int buf_size =
snprintf(NULL, 0, format, res->type, response_type_name, res->body_len);
char *buf = malloc(buf_size + 1);
sprintf(buf, format, res->type, response_type_name, res->body_len);
res->head = buf;
res->head_len = buf_size;
http_loop_init_header(res);
}
// The final iteration marks the end of the response, after which we reset the