From 01c5f33c8a1c163a4e02e486ceff3b1556594514 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Fri, 5 Jan 2024 18:51:41 +0100 Subject: Update HTTP1; Error handling for secure random --- build_and_test.sh | 42 +++++++++++++++++++-------------------- source/kit/http1.h | 30 +++++++++++++++++++++------- source/kit/secure_random.c | 12 ++++++----- source/kit/secure_random.h | 4 ++-- source/kit/status.h | 1 + source/tests/http1.test.c | 5 ++++- source/tests/secure_random.test.c | 4 ++-- 7 files changed, 60 insertions(+), 38 deletions(-) diff --git a/build_and_test.sh b/build_and_test.sh index bcb10c6..f677961 100644 --- a/build_and_test.sh +++ b/build_and_test.sh @@ -144,7 +144,7 @@ echo "" echo "Build kit" $COMPILE ${FLAGS} \ - ${FLAG_OBJ}"build/kit${OBJ_POSTFIX}" \ + ${FLAG_OBJ}"${FOLDER}/kit${OBJ_POSTFIX}" \ "source/kit/_lib.c" if [ $? -ne 0 ]; then exit 1 @@ -152,8 +152,8 @@ fi echo "Build kit_test_suite" $COMPILE ${FLAGS} \ - ${FLAG_EXE}"build/kit_test_suite${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/kit_test_suite${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/_exe.c" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -161,8 +161,8 @@ if [ $? -ne 0 ]; then fi $COMPILE ${FLAGS} \ - ${FLAG_EXE}"build/test_too_many_assertions${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/test_too_many_assertions${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/test_too_many_assertions.c" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -170,8 +170,8 @@ if [ $? -ne 0 ]; then fi $COMPILE ${FLAGS} \ - ${FLAG_EXE}"build/test_too_many_tests${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/test_too_many_tests${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/test_too_many_tests.c" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -179,8 +179,8 @@ if [ $? -ne 0 ]; then fi $COMPILEPP ${FLAGS} \ - ${FLAG_EXE}"build/test_cpp${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/test_cpp${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/test_cpp.cpp" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -188,8 +188,8 @@ if [ $? -ne 0 ]; then fi $COMPILEPP ${FLAGS} \ - ${FLAG_EXE}"build/test_signals${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/test_signals${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/test_signals.cpp" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -197,8 +197,8 @@ if [ $? -ne 0 ]; then fi $COMPILE ${FLAGS} \ - ${FLAG_EXE}"build/test_interprocess${EXE_POSTFIX}" \ - "build/kit${OBJ_POSTFIX}" \ + ${FLAG_EXE}"${FOLDER}/test_interprocess${EXE_POSTFIX}" \ + "${FOLDER}/kit${OBJ_POSTFIX}" \ "source/tests/test_interprocess.c" \ ${LINK_FLAGS} if [ $? -ne 0 ]; then @@ -210,12 +210,12 @@ echo "" STATUS=0 -./build/kit_test_suite +${FOLDER}/kit_test_suite if [ $? -ne 0 ]; then STATUS=1 fi -./build/test_too_many_assertions --quiet +${FOLDER}/test_too_many_assertions --quiet if [ $? -eq 0 ]; then echo "too many assertions - OK" else @@ -223,7 +223,7 @@ else STATUS=1 fi -./build/test_too_many_tests --quiet +${FOLDER}/test_too_many_tests --quiet if [ $? -eq 0 ]; then echo "too many tests - OK" else @@ -231,7 +231,7 @@ else STATUS=1 fi -./build/test_cpp --quiet +${FOLDER}/test_cpp --quiet if [ $? -eq 0 ]; then echo "cpp - OK" else @@ -239,7 +239,7 @@ else STATUS=1 fi -./build/test_signals --quiet +${FOLDER}/test_signals --quiet if [ $? -eq 0 ]; then echo "signals - OK" else @@ -247,9 +247,9 @@ else STATUS=1 fi -./build/test_interprocess clean -./build/test_interprocess reader & -./build/test_interprocess writer +${FOLDER}/test_interprocess clean +${FOLDER}/test_interprocess reader & +${FOLDER}/test_interprocess writer if [ $? -eq 0 ]; then echo "interprocess - OK" else diff --git a/source/kit/http1.h b/source/kit/http1.h index 043dcf6..98190ea 100644 --- a/source/kit/http1.h +++ b/source/kit/http1.h @@ -60,6 +60,7 @@ typedef struct { kit_str_builder_t response; kit_str_builder_t response_str; kit_http1_str_map_t header; + kit_str_builder_t header_str; kit_str_builder_t body; } kit_http1_response_t; @@ -191,8 +192,13 @@ static void kit_http1_uri_init(kit_http1_uri_t *uri, kit_str_t input, } static void kit_http1_uri_destroy(kit_http1_uri_t *uri) { - // TODO - // + assert(uri != NULL); + if (uri == NULL) + return; + + KIT_DA_DESTROY(uri->parameters); + + memset(uri, 0, sizeof *uri); } static kit_str_t kit_http1_method_to_str(i32 method) { @@ -435,7 +441,7 @@ static kit_http1_response_t kit_http1_request( res.response_str = kit_str_build( kit_http1_tok_next(&buf_tok, SZ(KIT_HTTP1_NEWLINE), 0), alloc); - kit_str_builder_t header = kit_str_build( + res.header_str = kit_str_build( kit_http1_tok_next(&buf_tok, SZ(KIT_HTTP1_NEWLINE KIT_HTTP1_NEWLINE), 0), alloc); @@ -443,8 +449,8 @@ static kit_http1_response_t kit_http1_request( res.body = kit_str_build(kit_http1_tok_tail(&buf_tok), alloc); kit_http1_tok_t header_tok = { - .str = (kit_str_t) { .size = header.size, - .values = header.values }, + .str = (kit_str_t) { .size = res.header_str.size, + .values = res.header_str.values }, .position = 0 }; @@ -480,8 +486,18 @@ static kit_http1_response_t kit_http1_request( static void kit_http1_response_destroy( kit_http1_response_t *response) { - // TODO - // + assert(response != NULL); + if (response == NULL) + return; + + KIT_DA_DESTROY(response->protocol); + KIT_DA_DESTROY(response->response); + KIT_DA_DESTROY(response->response_str); + KIT_DA_DESTROY(response->header); + KIT_DA_DESTROY(response->header_str); + KIT_DA_DESTROY(response->body); + + memset(response, 0, sizeof *response); } #ifdef __GNUC__ diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c index 8f7a302..02b168b 100644 --- a/source/kit/secure_random.c +++ b/source/kit/secure_random.c @@ -14,12 +14,12 @@ # include #endif -void kit_secure_random(i64 size, void *data) { +kit_status_t kit_secure_random(i64 size, void *data) { assert(size >= 0); assert(data != NULL); if (size <= 0 || data == NULL) - return; + return KIT_ERROR_INVALID_ARGUMENT; #if defined(_WIN32) && !defined(__CYGWIN__) HCRYPTPROV prov = 0; @@ -27,17 +27,19 @@ void kit_secure_random(i64 size, void *data) { CRYPT_VERIFYCONTEXT | CRYPT_SILENT) || !CryptGenRandom(prov, (DWORD) size, (BYTE *) data) || !CryptReleaseContext(prov, 0)) - abort(); + return KIT_ERROR_RESOURCE_UNAVAILABLE; #else FILE *f = fopen("/dev/urandom", "rb"); if (f == NULL) - abort(); + return KIT_ERROR_RESOURCE_UNAVAILABLE; i64 n = (i64) fread(data, 1, size, f); fclose(f); if (n != size) - abort(); + return KIT_ERROR_RESOURCE_UNAVAILABLE; #endif + + return KIT_OK; } diff --git a/source/kit/secure_random.h b/source/kit/secure_random.h index c894bd1..54a40e0 100644 --- a/source/kit/secure_random.h +++ b/source/kit/secure_random.h @@ -1,13 +1,13 @@ #ifndef KIT_SECURE_RANDOM_H #define KIT_SECURE_RANDOM_H -#include "types.h" +#include "status.h" #ifdef __cplusplus extern "C" { #endif -void kit_secure_random(i64 size, void *data); +kit_status_t kit_secure_random(i64 size, void *data); #ifdef __cplusplus } diff --git a/source/kit/status.h b/source/kit/status.h index 86b34b1..be54aad 100644 --- a/source/kit/status.h +++ b/source/kit/status.h @@ -26,6 +26,7 @@ enum { KIT_ERROR_UNMAP_FAILED = (1 << 18), KIT_ERROR_SYNC_FAILED = (1 << 19), KIT_ERROR_CLOSE_FAILED = (1 << 20), + KIT_ERROR_RESOURCE_UNAVAILABLE = (1 << 21), KIT_ERROR_NOT_IMPLEMENTED = (1 << 30), }; diff --git a/source/tests/http1.test.c b/source/tests/http1.test.c index 1808785..09da5f4 100644 --- a/source/tests/http1.test.c +++ b/source/tests/http1.test.c @@ -3,6 +3,9 @@ #define KIT_TEST_FILE http1 #include "../kit/kit_test.h" -TEST("http1") { } +TEST("http1") { + // TODO + // +} #undef KIT_TEST_FILE diff --git a/source/tests/secure_random.test.c b/source/tests/secure_random.test.c index efcf327..62021b1 100644 --- a/source/tests/secure_random.test.c +++ b/source/tests/secure_random.test.c @@ -8,8 +8,8 @@ TEST("secure random") { int v[20]; memset(v, 0, sizeof v); - secure_random(40, v); - secure_random(40, v + 10); + REQUIRE_EQ(secure_random(40, v), KIT_OK); + REQUIRE_EQ(secure_random(40, v + 10), KIT_OK); int repeats = 0; -- cgit v1.2.3