diff --git a/nodec/test/hexdump.c b/nodec/test/hexdump.c index 952b542..8f7e4d6 100644 --- a/nodec/test/hexdump.c +++ b/nodec/test/hexdump.c @@ -1,7 +1,20 @@ #include "stdlib.h" #include "stdio.h" #include -#include "hexdump.h" + +//---------------------------------------------------------------------------// +// get_print +// +// If the character is printable then return it otherwise return a period +//---------------------------------------------------------------------------// + +static unsigned char get_print(unsigned char uc) +{ + if (isprint(uc)) + return uc; + else + return '.'; +} void hexDump(const void *addr, size_t len) { @@ -9,16 +22,14 @@ void hexDump(const void *addr, size_t len) unsigned char buff[17]; unsigned char *pc = (unsigned char*)addr; for (i = 0; i < len; i++) { + const unsigned char uc = pc[i]; if ((i % 16) == 0) { if (i != 0) printf(" %s\n", buff); printf("%p ", pc + i); } - printf(" %02x", pc[i]); - if (!isprint(pc[i])) - buff[i % 16] = '.'; - else - buff[i % 16] = pc[i]; + printf(" %02x", uc); + buff[i % 16] = get_print(uc); buff[(i % 16) + 1] = '\0'; } while ((i % 16) != 0) { diff --git a/nodec/test/http_parser_wrapper.h b/nodec/test/http_parser_wrapper.h index ddb51ff..b91b914 100644 --- a/nodec/test/http_parser_wrapper.h +++ b/nodec/test/http_parser_wrapper.h @@ -11,7 +11,7 @@ extern "C" { void debug_http_parser_pause(http_parser* _parser, int _paused); int debug_http_body_is_final(const http_parser* _parser); -#ifdef VERBOSE +#if 1 #define HTTP_PARSER_EXECUTE debug_http_parser_execute #define HTTP_SHOULD_KEEP_ALIVE debug_http_should_keep_alive #define HTTP_PARSER_PAUSE debug_http_parser_pause diff --git a/nodec/test/main.c b/nodec/test/main.c index d21fbbb..d8817e0 100644 --- a/nodec/test/main.c +++ b/nodec/test/main.c @@ -214,74 +214,74 @@ void init_settings(struct http_parser_settings* settings); static const char* http_type_str(enum http_parser_type type) { - switch (type) { - case HTTP_REQUEST: return "HTTP_REQUEST"; - case HTTP_RESPONSE: return "HTTP_RESPONSE"; - case HTTP_BOTH: return "HTTP_BOTH"; - default: return "UNKNOWN"; - } + switch (type) { + case HTTP_REQUEST: return "HTTP_REQUEST"; + case HTTP_RESPONSE: return "HTTP_RESPONSE"; + case HTTP_BOTH: return "HTTP_BOTH"; + default: return "UNKNOWN"; + } } static int test_chunk(struct http_parser *parser, const struct http_parser_settings *settings, const char* chunk) { - size_t const len0 = strlen(chunk); - size_t const len1 = HTTP_PARSER_EXECUTE(parser, settings, chunk, len0); - if (parser->http_errno != HPE_OK || len0 != len1) { - printf("Parsing Error\n"); - return 1; - } - else - return 0; + size_t const len0 = strlen(chunk); + size_t const len1 = HTTP_PARSER_EXECUTE(parser, settings, chunk, len0); + if (parser->http_errno != HPE_OK || len0 != len1) { + printf("Parsing Error\n"); + return 1; + } + else + return 0; } static void test_chunks(const char* chunks[], size_t nchunks, enum http_parser_type type) { - size_t i; - struct http_parser_settings settings; - struct http_parser parser; - - init_settings(&settings); - HTTP_PARSER_INIT(&parser, type); - for (i = 0; i < nchunks; i++) { - if (test_chunk(&parser, &settings, chunks[i]) != 0) - break; - } - printf(" type: %s\n", http_type_str(parser.type)); - if (parser.type == HTTP_REQUEST) - printf(" method: %s\n", http_method_str(parser.method)); + size_t i; + struct http_parser_settings settings; + struct http_parser parser; + + init_settings(&settings); + HTTP_PARSER_INIT(&parser, type); + for (i = 0; i < nchunks; i++) { + if (test_chunk(&parser, &settings, chunks[i]) != 0) + break; + } + printf(" type: %s\n", http_type_str(parser.type)); + if (parser.type == HTTP_REQUEST) + printf(" method: %s\n", http_method_str(parser.method)); } static void test_httpx_serve(int strand_id, uv_stream_t* client) { - fprintf(stderr, "strand %i entered\n", strand_id); - // input - read_stream_t* rs = async_read_start(client, 0, 0, 0); - uv_buf_t buf = nodec_buf_null(); - size_t nread = async_read_buf(rs, &buf); - //const char* input = async_read_str(rs); - {with_free(buf.base) { - buf.base[buf.len] = 0; - printf("nread: %zu\n", nread); - printf("strand %i received:%u bytes\n%s\n", strand_id, buf.len, buf.base); - const char* chunks[] = { buf.base }; - test_chunks(chunks, 1, HTTP_REQUEST); - }} - // work - printf("waiting %i secs...\n", 2 + strand_id); - async_wait(1000 + strand_id * 1000); - //check_uverr(UV_EADDRINUSE); - - // response - {with_alloc_n(128, char, content_len) { - snprintf(content_len, 128, "Content-Length: %zi\r\n\r\n", strlen(response_body)); - printf("strand %i: response body is %zi bytes\n", strand_id, strlen(response_body)); - const char* response[3] = { response_headers, content_len, response_body }; - async_write_strs(client, response, 3); - }} - printf("request handled\n\n\n"); + fprintf(stderr, "strand %i entered\n", strand_id); + // input + read_stream_t* rs = async_read_start(client, 0, 0, 0); + uv_buf_t buf = nodec_buf_null(); + size_t nread = async_read_buf(rs, &buf); + //const char* input = async_read_str(rs); + {with_free(buf.base) { + buf.base[buf.len] = 0; + printf("nread: %zu\n", nread); + printf("strand %i received:%u bytes\n%s\n", strand_id, buf.len, buf.base); + const char* chunks[] = { buf.base }; + test_chunks(chunks, 1, HTTP_REQUEST); + }} + // work + printf("waiting %i secs...\n", 2 + strand_id); + async_wait(1000 + strand_id * 1000); + //check_uverr(UV_EADDRINUSE); + + // response + {with_alloc_n(128, char, content_len) { + snprintf(content_len, 128, "Content-Length: %zi\r\n\r\n", strlen(response_body)); + printf("strand %i: response body is %zi bytes\n", strand_id, strlen(response_body)); + const char* response[3] = { response_headers, content_len, response_body }; + async_write_strs(client, response, 3); + }} + printf("request handled\n\n\n"); } static void test_http() { - define_ip4_addr("127.0.0.1", 8080, addr); - async_http_server_at(addr, 0, 3, 0, &test_httpx_serve); + define_ip4_addr("127.0.0.1", 8080, addr); + async_http_server_at(addr, 0, 3, 0, &test_httpx_serve); } /*-----------------------------------------------------------------