From eb52be1d8c6858eee86a74e7b0654a1c67439364 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Mon, 17 Mar 2025 16:51:45 +0100 Subject: [PATCH] move query params to http --- backend/src/controllers/receipts.c | 54 ++--------------------------- backend/src/http/http.h | 8 +++++ backend/src/http/query_params.c | 55 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 backend/src/http/query_params.c diff --git a/backend/src/controllers/receipts.c b/backend/src/controllers/receipts.c index 28321ab..bcb834b 100644 --- a/backend/src/controllers/receipts.c +++ b/backend/src/controllers/receipts.c @@ -4,54 +4,6 @@ #include #include -typedef struct { - StrSlice key; - StrSlice value; -} QueryParamEntry; - -DEFINE_VEC(QueryParamEntry, QueryParamVec, query_param_vec) - -typedef struct { - QueryParamVec vec; -} QueryParams; - -QueryParams parse_query_params(const char* query) -{ - QueryParams result = { - .vec = (QueryParamVec) { 0 }, - }; - query_param_vec_construct(&result.vec); - - StrSplitter params = str_splitter(query, strlen(query), "&"); - StrSlice param; - while ((param = str_split_next(¶ms)).len != 0) { - StrSplitter left_right = str_splitter(param.ptr, param.len, "="); - StrSlice key = str_split_next(&left_right); - StrSlice value = str_split_next(&left_right); - - query_param_vec_push(&result.vec, (QueryParamEntry) { key, value }); - } - - return result; -} - -void query_params_destroy(QueryParams* query_params) -{ - query_param_vec_destroy(&query_params->vec); -} - -char* query_params_get(const QueryParams* query_params, const char* key) -{ - size_t key_len = strlen(key); - for (size_t i = 0; i < query_params->vec.size; ++i) { - const QueryParamEntry* entry = &query_params->vec.data[i]; - if (key_len == entry->key.len && strcmp(key, entry->key.ptr)) { - return str_slice_copy(&entry->value); - } - } - return NULL; -} - void route_get_receipts_one(HttpCtx* ctx) { Cx* cx = http_ctx_user_ctx(ctx); @@ -60,9 +12,9 @@ void route_get_receipts_one(HttpCtx* ctx) return; const char* query = http_ctx_req_query(ctx); - QueryParams params = parse_query_params(query); - char* receipt_id_str = query_params_get(¶ms, "receipt_id"); - query_params_destroy(¶ms); + HttpQueryParams* params = http_parse_query_params(query); + char* receipt_id_str = http_query_params_get(params, "receipt_id"); + http_query_params_free(params); if (!receipt_id_str) { RESPOND_BAD_REQUEST(ctx, "no receipt_id parameter"); return; diff --git a/backend/src/http/http.h b/backend/src/http/http.h index 00ca27a..b7a42c5 100644 --- a/backend/src/http/http.h +++ b/backend/src/http/http.h @@ -1,5 +1,6 @@ #pragma once +#include "../util/str.h" #include #include #include @@ -36,3 +37,10 @@ const char* http_ctx_req_query(HttpCtx* ctx); const char* http_ctx_req_body(HttpCtx* ctx); void http_ctx_res_headers_set(HttpCtx* ctx, const char* key, const char* value); void http_ctx_respond(HttpCtx* ctx, int status, const char* body); + +typedef struct HttpQueryParams HttpQueryParams; + +HttpQueryParams* http_parse_query_params(const char* query); +void http_query_params_free(HttpQueryParams* query_params); +char* http_query_params_get( + const HttpQueryParams* query_params, const char* key); diff --git a/backend/src/http/query_params.c b/backend/src/http/query_params.c new file mode 100644 index 0000000..e79776a --- /dev/null +++ b/backend/src/http/query_params.c @@ -0,0 +1,55 @@ +#include "./http.h" +#include +#include + +typedef struct { + StrSlice key; + StrSlice value; +} Param; + +DEFINE_VEC(Param, ParamVec, query_param_vec) + +struct HttpQueryParams { + ParamVec vec; +}; + +HttpQueryParams* http_parse_query_params(const char* query) +{ + HttpQueryParams* result = malloc(sizeof(HttpQueryParams)); + + *result = (HttpQueryParams) { + .vec = (ParamVec) { 0 }, + }; + query_param_vec_construct(&result->vec); + + StrSplitter params = str_splitter(query, strlen(query), "&"); + StrSlice param; + while ((param = str_split_next(¶ms)).len != 0) { + StrSplitter left_right = str_splitter(param.ptr, param.len, "="); + StrSlice key = str_split_next(&left_right); + StrSlice value = str_split_next(&left_right); + + query_param_vec_push(&result->vec, (Param) { key, value }); + } + + return result; +} + +void http_query_params_free(HttpQueryParams* query_params) +{ + query_param_vec_destroy(&query_params->vec); + free(query_params); +} + +char* http_query_params_get( + const HttpQueryParams* query_params, const char* key) +{ + size_t key_len = strlen(key); + for (size_t i = 0; i < query_params->vec.size; ++i) { + const Param* entry = &query_params->vec.data[i]; + if (key_len == entry->key.len && strcmp(key, entry->key.ptr)) { + return str_slice_copy(&entry->value); + } + } + return NULL; +}