From 8f0f398fc49cd5d0ed5d97099bd6547ea52ce44b Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Sun, 14 Nov 2021 11:52:18 +0100 Subject: changed everything * threads removed for now, because that was unrelyable and sometimes the connection could not be established. Investigation why is pending. * used openfd to open the socket, because working with a FILE structure is much more straight forward * added several parsing functions down to the level of detail, so that every chunk of multipart form data can be processed individually. The header parsing of these parts is still missing though. --- main.c | 150 ++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 103 insertions(+), 47 deletions(-) diff --git a/main.c b/main.c index b8be592..9c1d177 100644 --- a/main.c +++ b/main.c @@ -1,51 +1,25 @@ #include #include +#include #include #include #include #include +#include #include #include +#include #include #include #include -void * next_customer(void * new_socket) -{ - if (new_socket == 0) return NULL; - - - // read the input data... - char * input_buffer = {0}; - size_t input_buffer_position = 0; - FILE * f_input = open_memstream(&input_buffer, &input_buffer_position); - - - for( - int size=1024, - read_buffer[1024] = {0}; - size>0; - size=recv((size_t) new_socket, read_buffer, 1024, MSG_WAITALL)) { - fwrite(read_buffer, size, 1, f_input); - usleep(10); // TODO: implement with some sort of epoll - } - - fflush(f_input); - printf("input_buffer has %ld bytes and contains:\n", input_buffer_position); - - char * c = strstr(input_buffer, "Content-Type"); - printf("WE ARE HERE: %s\n", c); - write(STDOUT_FILENO, input_buffer, input_buffer_position); - - - // answer the call... - char * buffer = {0}; - size_t buffer_position = 0; - FILE * f = open_memstream(&buffer, &buffer_position); +void send_answer(void * new_socket) { + FILE * f = fdopen((size_t) new_socket, "w"); + puts("> sending answer..."); fputs("HTTP/1.0 200 OK\n", f); fputs("content-type: text/html\n\n", f); fputs("", f); @@ -57,19 +31,101 @@ void * next_customer(void * new_socket) fputs("
\n", f); + fputs("", f); fputs("", f); fputs("", f); - fputs("\n", f); + fputs("\n\0", f); fflush(f); - printf("OOOOO [%ld] %s\n", buffer_position, buffer); - - send((size_t) new_socket, buffer, buffer_position, MSG_EOR); - // fclose(f_socket); - close((size_t) new_socket); - fclose(f_input); + puts("> answer sent."); fclose(f); +} + + +// #define FOREACH_HEADER(HEADER) \ +// HEADER("Content-Type: multipart/form-data; boundary=") +// #define GENERATE_HEADERS(H) sizeof(H), H +// +// typedef struct { +// const int length; +// const char * string; +// } length_value; +// +// const length_value headers[] = { +// FOREACH_HEADER(GENERATE_HEADERS) +// }; + + +void * next_customer(void * new_socket) { + if (new_socket == 0) return NULL; + + const int MAX_HEADER_LINE_LENGTH = 1024; + const int MAX_BOUNDARY_LENGTH = 64; + + char boundary[64] = {0}; + int boundary_size = 0; + + usleep(100); // wait to avoid poll, epoll or evil select + + FILE * f = fdopen((size_t) new_socket, "r"); + char buffer[MAX_HEADER_LINE_LENGTH]; + while(fgets(buffer, MAX_HEADER_LINE_LENGTH, f)) { + printf("%s [%d]", buffer, errno); + + if(buffer[0] == '\n' || (buffer[0] == '\r' && buffer[1] == '\n')) { // either LF or CR/LF (windows) + puts("END HEADER\n"); + if (!boundary[0]) break; + continue; + } + + const char header_multipart[] = "Content-Type: multipart/form-data; boundary="; + if (!boundary[0] && 0 == strncasecmp(buffer, header_multipart, sizeof(header_multipart) - 1)) { + for(int i=sizeof(header_multipart); i NEXT CHUNK: %s\n", buffer); + i = 0; + } + break; + } + } + } else { + printf("%s", buffer); + } + + + // I think another if could now search for "Content-Disposition: form-data; " and store the remaining fields + // somewhere, e.g. name="okay"; filename="1.gif". Also there may also be a content-type per chunk, which we + // could also add and erase, where I have currently NEXT CHUNK. + // After that the text output (printf("%s", buffer)) will only contain content without headers, so that printf + // can then be replaced with some fwrite to a memstream and we are done :) + } + + puts("> sending answer..."); + send_answer(new_socket); + puts("> answer sent."); + - return NULL; + puts("> file closed."); + //close((size_t) new_socket); + // pthread_exit(EXIT_SUCCESS); + return NULL; } void serve(int server_fd) @@ -84,17 +140,17 @@ void serve(int server_fd) warn("next: %ld\n", new_socket); // set non blocking mode... fcntl((size_t) new_socket, F_SETFL, - fcntl((size_t) new_socket, F_GETFL) | O_NONBLOCK); + fcntl((size_t) new_socket, F_GETFL) | O_NONBLOCK); - // next_customer(new_socket); - pthread_t thread_id; - pthread_create(&thread_id, NULL, next_customer, (void*) new_socket); - pthread_join(thread_id, NULL); + next_customer((void*) new_socket); + // pthread_t thread_id; + // pthread_create(&thread_id, NULL, next_customer, (void*) new_socket); + // pthread_join(thread_id, NULL); } } #define PORT 8080 -int main(int argc, char const *argv[]) +int main(int argc, char const *argv[]) { int server_fd; int opt = 1; @@ -110,7 +166,7 @@ int main(int argc, char const *argv[]) ? err(EXIT_FAILURE, "setsockopt failed on socket with fileno %d", server_fd) : bind(server_fd, (struct sockaddr*) &address, sizeof(address)) ? err(EXIT_FAILURE, NULL) - : listen(server_fd, 3) + : listen(server_fd, SOMAXCONN) ? err(EXIT_FAILURE, NULL) : serve(server_fd); -- cgit v1.2.3