From 076a37921f533b97413340288d56572e11e1395b Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Fri, 19 Nov 2021 19:29:05 +0100 Subject: Omg! It works! --- main.c | 123 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 64 insertions(+), 59 deletions(-) diff --git a/main.c b/main.c index da67e45..18b9900 100644 --- a/main.c +++ b/main.c @@ -112,8 +112,6 @@ void * next_customer(size_t new_socket) { char * key = output_buffer; char * saveptr = NULL; - char * content_start = NULL; - char * content_end = NULL; char * start = output_buffer; char * end = NULL; @@ -166,9 +164,6 @@ 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) { @@ -187,12 +182,65 @@ void * next_customer(size_t new_socket) { verbose("\033[32m[%ld]> '% 20s' = '%s'\033[0m\n", matchlen, name, start); if(matchlen > http_header.newline_length) { - verbose("END HEADERS, boundary='%s'[%ld]\n", http_header.boundary, http_header.boundary_size); - search = "-"; - } - break; - case '-': - { + verbose("> END HEADERS, because there were %d newlines; boundary='%s'[%ld]\n", matchlen / http_header.newline_length, http_header.boundary, http_header.boundary_size); + end += matchlen; + + // if it was the first header, we calculate the boundary size and expect more headers to come after a boundary + if(http_header.boundary && http_header.boundary_size == 0) { + verbose("================================================================================\n"); + http_header.boundary_size = strlen(http_header.boundary); + // skip the first header and boundary... + start = end; + start += strspn(start, "-"); + start += http_header.boundary_size; + start += http_header.newline_length; + continue; + } else { + char * content_start = end; + while(1) + { + size_t size_remaining = (size_t) output_buffer_length - (end - output_buffer) - 1; + verbose("%ld remaining.\n", size_remaining); fflush(stdout); + + if(size_remaining <= 0) { + verbose("> not even the boundary would fit in that what is left.\n"); + break; + } + + if(NULL == (end = memchr((void*) end, '-', size_remaining))) { + verbose("no further '-' found\n"); + break; + } + + char * content_end = end - http_header.newline_length; + + + end += strspn(end, "-"); + if(0 == strncmp(end, http_header.boundary, http_header.boundary_size)) { + size_t file_size = content_end - content_start; + verbose("> Content ends here, size of the last file is %ld: {begin}", file_size); + fwrite(content_start, file_size, 1, stdout); + verbose("{end}\n"); + + + // puts(" <[last 20 bytes]\n"); + + + end += http_header.boundary_size; + matchlen = strspn(end, "\r\n"); + // matchlen = http_header.boundary_size; + verbose("> end is at %p, matchlen is %ld\n", end, matchlen); + + + search = ":"; + break; + } else { + // printf("[%ld] kack: %c\n", size_remaining, p[0]); fflush(stdout); + end = end + 1; + } + } + } + /* char * content_start = end; content_start += strspn(content_start, "-"); // verbose("CONTENT: %s\n", content_start); @@ -205,64 +253,21 @@ void * next_customer(size_t new_socket) { } else { verbose("NO MATCH\n"); } + */ break; } } + // verbose("> end is at %p, matchlen is %ld\n", end, matchlen); start = end + matchlen; + // fwrite(start, 42, 1, stdout); + // printf(" <[first 42 bytes of new start; matchlen was %ld; searching for %s\n", matchlen, search); } 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) { - verbose("> not even the boundary would fit in that what is left.\n"); - break; - } - - p = (char *) memchr( - (void *) p, '-', size_remaining); - - - if(!p) { - verbose("> no further minus found.\n"); - break; - } - - end = p; - if(end[-1] == '\n') end--; - if(end[-1] == '\r') end--; - p += strspn(p, "-"); - - if(0 == strncmp(p, http_header.boundary, http_header.boundary_size)) { - verbose("> Content ends here, size of the last file is %ld\n", end - start); - - fwrite(start, 20, 1, stdout); - puts(" <[first 20 bytes]\n"); - fwrite(end - 20, 20, 1, stdout); - puts(" <[last 20 bytes]\n"); - 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_r); fclose(f_r); @@ -495,4 +500,4 @@ void * next_customer(void * new_socket) { return NULL; } */ -// vim: shiftwidth=2 tabstop=2 number +// vim: shiftwidth=2 tabstop=2 number foldmethod=marker -- cgit v1.2.3