From e1d928c67e1cfcd522cd1f28a5feacec4803679c Mon Sep 17 00:00:00 2001 From: Francesco Date: Wed, 25 Mar 2026 01:56:34 +0100 Subject: [PATCH] refactor(request): change func namespace --- include/http/request.h | 10 +++------- src/http/request.c | 36 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/include/http/request.h b/include/http/request.h index f3a22a8..9cae650 100644 --- a/include/http/request.h +++ b/include/http/request.h @@ -6,10 +6,6 @@ #include #include -#define CWS_HTTP_CONTENT_TYPE 64 -#define CWS_HTTP_HEADER_MAX 512 -#define CWS_HTTP_HEADER_CONTENT_MAX 1024 - typedef struct cws_request { cws_http_method_e method; string_s *host; @@ -20,10 +16,10 @@ typedef struct cws_request { string_s *body; } cws_request_s; -cws_request_s *cws_http_parse(string_s *request_str); +cws_request_s *cws_request_parse(string_s *request_str); -char *cws_http_get_host(cws_request_s *request); +char *cws_request_get_header(cws_request_s *request, const char *header); -void cws_http_free(cws_request_s *request); +void cws_request_free(cws_request_s *request); #endif diff --git a/src/http/request.c b/src/http/request.c index 52a0e08..9d2add2 100644 --- a/src/http/request.c +++ b/src/http/request.c @@ -9,6 +9,8 @@ #include "utils/debug.h" #include "utils/hash.h" +#include "internal/common.h" + static cws_request_s *http_request_new(void) { cws_request_s *request = malloc(sizeof(*request)); if (!request) { @@ -91,7 +93,7 @@ static bool parse_version(cws_request_s *req, char **cursor) { static bool parse_headers(cws_request_s *req, char **cursor) { req->headers = hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, - sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX); + sizeof(char) * HEADER_KEY_MAX, sizeof(char) * HEADER_VALUE_MAX); char *s = *cursor + strspn(*cursor, "\r\n"); while (*s != '\0' && *s != '\r') { @@ -113,8 +115,8 @@ static bool parse_headers(cws_request_s *req, char **cursor) { char *header_value = colon + 1; header_value += strspn(header_value, " \t"); - char hk[CWS_HTTP_HEADER_MAX]; - char hv[CWS_HTTP_HEADER_CONTENT_MAX]; + char hk[HEADER_KEY_MAX]; + char hv[HEADER_VALUE_MAX]; strncpy(hk, header_key, sizeof(hk) - 1); hk[sizeof(hk) - 1] = '\0'; @@ -133,7 +135,7 @@ static bool parse_headers(cws_request_s *req, char **cursor) { return true; } -cws_request_s *cws_http_parse(string_s *request_str) { +cws_request_s *cws_request_parse(string_s *request_str) { if (!request_str || !string_cstr(request_str)) { return NULL; } @@ -145,7 +147,7 @@ cws_request_s *cws_http_parse(string_s *request_str) { char *str = string_copy(request_str); if (!str) { - cws_http_free(request); + cws_request_free(request); return NULL; } char *orig = str; @@ -153,28 +155,28 @@ cws_request_s *cws_http_parse(string_s *request_str) { /* Parse HTTP method */ if (!parse_method(request, &str)) { free(orig); - cws_http_free(request); + cws_request_free(request); return NULL; } /* Parse location (URL path) */ if (!parse_location(request, &str)) { free(orig); - cws_http_free(request); + cws_request_free(request); return NULL; } /* Parse HTTP version */ if (!parse_version(request, &str)) { free(orig); - cws_http_free(request); + cws_request_free(request); return NULL; } /* Parse headers */ if (!parse_headers(request, &str)) { free(orig); - cws_http_free(request); + cws_request_free(request); return NULL; } @@ -183,16 +185,20 @@ cws_request_s *cws_http_parse(string_s *request_str) { return request; } -char *cws_http_get_host(cws_request_s *request) { - bucket_s *host = hm_get(request->headers, "Host"); - if (!host) { - return "default"; +char *cws_request_get_header(cws_request_s *request, const char *header) { + if (!request || !header || !request->headers) { + return ""; } - return (char *)host->value; + bucket_s *bucket = hm_get(request->headers, (void *)header); + if (!bucket) { + return ""; + } + + return (char *)bucket->value; } -void cws_http_free(cws_request_s *request) { +void cws_request_free(cws_request_s *request) { if (!request) { return; }