diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/kit/http1.h | 30 | ||||
-rw-r--r-- | source/kit/secure_random.c | 12 | ||||
-rw-r--r-- | source/kit/secure_random.h | 4 | ||||
-rw-r--r-- | source/kit/status.h | 1 | ||||
-rw-r--r-- | source/tests/http1.test.c | 5 | ||||
-rw-r--r-- | source/tests/secure_random.test.c | 4 |
6 files changed, 39 insertions, 17 deletions
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 <unistd.h> #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; |