From e6f9bc88a029bff63df9b029d986d5961a3ce021 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Thu, 18 Nov 2021 01:23:05 +0100 Subject: Looking for a good way to parse binary data --- main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index 7fef5f3..5395ab2 100644 --- a/main.c +++ b/main.c @@ -91,13 +91,13 @@ typedef struct { int newline_length; // lenght of one newline in bytes (\n has 1, CR/LF has 2) char * method; char * boundary; + size_t boundary_size; char * content_type; char * content_disposition; } Http_Header; void * next_customer(size_t new_socket) { - FILE * f_r = fdopen((size_t) new_socket, "r"); - FILE * f_w = fdopen((size_t) new_socket, "w"); + FILE * f_r = fdopen((size_t) new_socket, "rw"); char * output_buffer = NULL; size_t output_buffer_length = 0; @@ -105,9 +105,9 @@ void * next_customer(size_t new_socket) { verbose("\n\n########################################## Content Reader [%d]\n", f_r); read_everything(f_r, output); + shutdown(new_socket, SHUT_RD); // shutdown the reading half of the connection verbose("\n\n########################################## Content Parser\n"); - // token parser... char * key = output_buffer; @@ -116,10 +116,11 @@ void * next_customer(size_t new_socket) { char * content_end = NULL; char * start = output_buffer; - char * end; + char * end = NULL; char * search = "\r\n"; Http_Header http_header; + http_header.boundary_size = 0; http_header.newline_length = 1; http_header.method = NULL; http_header.boundary = NULL; @@ -127,7 +128,9 @@ void * next_customer(size_t new_socket) { http_header.content_type = NULL; char * name = NULL; + // while(NULL != (end = strpbrk(start, search))) { while(NULL != (end = strpbrk(start, search))) { + // verbose("\033[31m[%ld|%ld|%ld]\033[0m\n", start - output_buffer, end - start, end - output_buffer); size_t matchlen = strspn(end, search); switch(end[0]) { @@ -152,7 +155,7 @@ void * next_customer(size_t new_socket) { { http_header.boundary = end + sizeof(s_multipart_form_data) + 1; http_header.boundary += strspn(http_header.boundary, "-"); - // verbose("GESCHAFFT %s QQQQ %s\n", http_header.boundary, end); + // verbose("GESCHAFFT %s [%ld] QQQQ %s\n", http_header.boundary, http_header.boundary_size, end); search = "\r\n"; continue; } @@ -163,6 +166,9 @@ void * next_customer(size_t new_socket) { end[0] = '\0'; search = ":"; // we will continue to search for headers + if(http_header.boundary && http_header.boundary_size == 0) { + http_header.boundary_size = strlen(http_header.boundary); + } if(NULL == name) { if(NULL == http_header.method) { @@ -178,11 +184,10 @@ void * next_customer(size_t new_socket) { { http_header.content_disposition = start; } } - verbose("\033[0;32m[%ld]> '% 20s' = '%s'\033[0m\n", matchlen, name, start); + verbose("\033[32m[%ld]> '% 20s' = '%s'\033[0m\n", matchlen, name, start); if(matchlen > http_header.newline_length) { - verbose("END HEADERS, boundary='%s'\n", http_header.boundary); - // start = strstr(end, http_header.boundary); + verbose("END HEADERS, boundary='%s'[%ld]\n", http_header.boundary, http_header.boundary_size); search = "-"; } break; @@ -192,11 +197,11 @@ void * next_customer(size_t new_socket) { content_start += strspn(content_start, "-"); // verbose("CONTENT: %s\n", content_start); if(0 <= strcmp(content_start, http_header.boundary)) { - verbose("MATCH\n"); + verbose("MATCH for %s // %s\n", http_header.content_type, http_header.content_disposition); + search = "\r\n"; - start = content_start + strlen(http_header.boundary); + matchlen = http_header.boundary_size; name = NULL; - continue; } else { verbose("NO MATCH\n"); } @@ -206,15 +211,43 @@ void * next_customer(size_t new_socket) { start = end + matchlen; } + printf("last known position: %p / %p with %s\n", start, end, search); + + + char * p; + if(NULL != (p = strstr(start, http_header.boundary))) { + puts("OKAY?"); + } + + p = start; + while(1) { + size_t size_remaining = (size_t) output_buffer_length - (p - output_buffer) - 1; + printf("%ld remaining.\n", size_remaining); fflush(stdout); + if(size_remaining <= 0) + break; + + p = (char *) memchr( + (void *) p, '-', size_remaining); + + p += strspn(p, "-"); + + if(0 >= strcmp(p, http_header.boundary)) { + verbose("content ends here"); + break; + } else { + printf("[%ld] kack: %c\n", size_remaining, p[0]); fflush(stdout); + p = p + 1; + } + } + start = p; + // exit(0); if(http_header.boundary) verbose("http-boundary: %s", http_header.boundary); verbose("> sending answer..."); - send_answer(f_w); - - fclose(f_w); + send_answer(f_r); fclose(f_r); verbose("> answer sent."); -- cgit v1.2.3