mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 00:34:06 +02:00
move query params to http
This commit is contained in:
parent
4f84dc4842
commit
eb52be1d8c
@ -4,54 +4,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
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)
|
void route_get_receipts_one(HttpCtx* ctx)
|
||||||
{
|
{
|
||||||
Cx* cx = http_ctx_user_ctx(ctx);
|
Cx* cx = http_ctx_user_ctx(ctx);
|
||||||
@ -60,9 +12,9 @@ void route_get_receipts_one(HttpCtx* ctx)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
const char* query = http_ctx_req_query(ctx);
|
const char* query = http_ctx_req_query(ctx);
|
||||||
QueryParams params = parse_query_params(query);
|
HttpQueryParams* params = http_parse_query_params(query);
|
||||||
char* receipt_id_str = query_params_get(¶ms, "receipt_id");
|
char* receipt_id_str = http_query_params_get(params, "receipt_id");
|
||||||
query_params_destroy(¶ms);
|
http_query_params_free(params);
|
||||||
if (!receipt_id_str) {
|
if (!receipt_id_str) {
|
||||||
RESPOND_BAD_REQUEST(ctx, "no receipt_id parameter");
|
RESPOND_BAD_REQUEST(ctx, "no receipt_id parameter");
|
||||||
return;
|
return;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "../util/str.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -36,3 +37,10 @@ const char* http_ctx_req_query(HttpCtx* ctx);
|
|||||||
const char* http_ctx_req_body(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_res_headers_set(HttpCtx* ctx, const char* key, const char* value);
|
||||||
void http_ctx_respond(HttpCtx* ctx, int status, const char* body);
|
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);
|
||||||
|
55
backend/src/http/query_params.c
Normal file
55
backend/src/http/query_params.c
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include "./http.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user