diff options
Diffstat (limited to 'source/kit')
-rw-r--r-- | source/kit/defer.h | 39 | ||||
-rw-r--r-- | source/kit/http1.h | 35 | ||||
-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/kit/test.h (renamed from source/kit/kit_test.h) | 14 |
6 files changed, 81 insertions, 24 deletions
diff --git a/source/kit/defer.h b/source/kit/defer.h new file mode 100644 index 0000000..bb78b69 --- /dev/null +++ b/source/kit/defer.h @@ -0,0 +1,39 @@ +// Requres GCC or Clang with `-fblocks` option available. +// + +#ifndef KIT_DEFER_H +#define KIT_DEFER_H + +#if !defined(__clang__) && !defined(__APPLE__) +# error C blocks support required +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-function" +# pragma GCC diagnostic ignored "-Wunknown-pragmas" +#endif + +static void kit_defer_cleanup_(void (^*b)(void)) { (*b)(); } + +#define kit_defer_merge_(a,b) a##b +#define kit_defer_varname_(a) \ + kit_defer_merge_(kit_defer_scopevar_,a) +#define defer \ + __attribute__((unused, cleanup(kit_defer_cleanup_))) \ + void (^kit_defer_varname_(__COUNTER__))(void) = ^ +#define defer_ref __block + +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/kit/http1.h b/source/kit/http1.h index f4e5fc6..98190ea 100644 --- a/source/kit/http1.h +++ b/source/kit/http1.h @@ -16,8 +16,6 @@ extern "C" { #endif -#define KIT_HTTP1_CONTENT_TYPE \ - "Content-Type: text/plain; version=0.0.4; charset=utf-8"; #define KIT_HTTP1_NEWLINE "\r\n" #define KIT_HTTP1_SPACE " " #define KIT_HTTP1_HEADER_SEPARATOR ": " @@ -62,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; @@ -120,7 +119,7 @@ static kit_str_t kit_http1_tok_next(kit_http1_tok_t *tok, // - Return error status static void kit_http1_uri_init(kit_http1_uri_t *uri, kit_str_t input, kit_allocator_t *alloc) { - static const char port_80[] = "80"; + static const kit_str_t port_80 = { .size = 2, .values = "80" }; assert(uri != NULL); if (uri == NULL) @@ -155,7 +154,7 @@ static void kit_http1_uri_init(kit_http1_uri_t *uri, kit_str_t input, // - HTTPS default port 443 uri->port = kit_http1_tok_tail(&host_port_tok); if (uri->port.size == 0) - uri->port = SZ(port_80); + uri->port = port_80; uri->address = kit_http1_tok_next(&input_tok, SZ("?"), 1); uri->query_string = kit_http1_tok_next(&input_tok, SZ("#"), 1); @@ -193,7 +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) { @@ -436,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); @@ -444,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 }; @@ -481,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/kit/kit_test.h b/source/kit/test.h index 176d40c..b6be132 100644 --- a/source/kit/kit_test.h +++ b/source/kit/test.h @@ -1,6 +1,6 @@ // ================================================================ // -// kit_test.h +// test.h // https://guattari.tech/git/kit // // Header-only unit-testing and microbenchmarks framework for C. @@ -26,16 +26,16 @@ // Usage example // // // foo.test.c -// #define KIT_TEST_FILE foo // This is required if you want to use -// // multiple test files. -// #include "kit_test.h" +// #define KIT_TEST_FILE foo // This is required if you want to +// // use multiple test files. +// #include "test.h" // TEST("foo") { // REQUIRE(1); // REQUIRE_EQ(2 + 2, 4); // } // // // main.c -// #include "kit_test.h" +// #include "test.h" // int main(int argc, char **argv) { // return run_tests(argc, argv); // } @@ -263,8 +263,8 @@ void kit_bench_register(char const *name, char const *file, kit_bench_end_(kit_bench_index_); \ } -/* FIXME - */ +// FIXME +// #define KIT_DO_NOT_OPTIMIZE(x) \ do { \ volatile void *bench_ptr_ = &(x); \ |