refactor: modularize the http header files

This commit is contained in:
Jef Roosens 2023-05-30 16:00:08 +02:00
parent 89fb77db7f
commit f07042e798
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
13 changed files with 190 additions and 162 deletions

View file

@ -1,6 +1,6 @@
#include <regex.h>
#include "http.h"
#include "http/types.h"
#include "http_loop.h"
#include "log.h"

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include "http.h"
#include "http/types.h"
#include "http_loop.h"
http_loop_gctx *http_loop_gctx_init() {

View file

@ -34,8 +34,8 @@ http_parse_error http_loop_parse_request(event_loop_conn *conn) {
bool match = false;
size_t i = 0;
for (i = 0; i < request_method_names_len; i++) {
if (strncmp(method, request_method_names[i], method_len) == 0) {
for (i = 0; i < http_method_names_len; i++) {
if (strncmp(method, http_method_names[i], method_len) == 0) {
req->method = i;
match = true;
}
@ -88,7 +88,7 @@ void http_loop_route_request(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
http_loop_gctx *gctx = ctx->g;
info("%s %.*s", request_method_names[ctx->req.method], ctx->req.path_len,
info("%s %.*s", http_method_names[ctx->req.method], ctx->req.path_len,
ctx->req.path);
http_route *route;

View file

@ -17,7 +17,7 @@ void http_loop_init_header(http_response *res) {
}
const char *response_type_name =
http_response_type_names[res->status / 100 - 1][res->status % 100];
http_status_names[res->status / 100 - 1][res->status % 100];
// First we calculate the size of the start of the header
int buf_size = snprintf(NULL, 0, http_response_format, res->status,

View file

@ -1,37 +0,0 @@
#include <sys/stat.h>
#include "http_loop.h"
void http_loop_res_set_body_buf(http_loop_ctx *ctx, const char *body,
size_t body_len, bool owned) {
ctx->res.body_type = http_body_buf;
ctx->res.body.buf = (char *)body;
ctx->res.body_len = body_len;
ctx->res.owns_body = owned;
}
void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename) {
struct stat st;
stat(filename, &st);
// TODO error handling
FILE *f = fopen(filename, "r");
ctx->res.body_type = http_body_file;
ctx->res.body.file = f;
ctx->res.body_len = st.st_size;
}
void http_loop_res_add_header(http_loop_ctx *ctx, http_header type,
const char *value, bool owned) {
ctx->res.headers[ctx->res.header_count].type = type;
ctx->res.headers[ctx->res.header_count].value = value;
ctx->res.headers[ctx->res.header_count].owned = owned;
ctx->res.header_count++;
}
void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type) {
http_loop_res_add_header(ctx, http_header_content_type,
http_mime_type_names[mime_type][1], false);
}