From 06f1636f2a5469eb61418b9fcb9a743c2594462c Mon Sep 17 00:00:00 2001 From: Rob van der Leek <5324924+robvanderleek@users.noreply.github.com> Date: Tue, 11 Jun 2024 09:11:38 +0200 Subject: [PATCH] Fix body extraction --- c/snake.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/c/snake.c b/c/snake.c index 371dc21..d43267a 100644 --- a/c/snake.c +++ b/c/snake.c @@ -90,8 +90,19 @@ void handle_end(int client_socket) { send(client_socket, header, strlen(header), 0); } -const char *get_body(const char *request) { - char *result = strstr(request, "\r\n\r\n") + 4; +int get_content_length(char *request) { + char *ptr = strstr(request, "Content-Length: ") + 16; + return (int)strtol(ptr, NULL, 10); +} + +char *get_body(int client_socket, char *buffer, int buffer_size, int bytes) { + int content_length = get_content_length(buffer); + char *result = strstr(buffer, "\r\n\r\n") + 4; + while (strlen(result) < content_length) { + char *ptr = buffer + bytes; + bytes += (int)recv(client_socket, ptr, buffer_size - bytes, 0); + result = strstr(buffer, "\r\n\r\n") + 4; + } return result; } @@ -113,7 +124,7 @@ int main(int argc, char *argv[]) { &client_address_len); int buffer_size = 32768; char *buffer = (char *)malloc(buffer_size * sizeof(char)); - size_t bytes = recv(client_socket, buffer, buffer_size, MSG_WAITALL); + int bytes = (int)recv(client_socket, buffer, buffer_size, 0); const char *get_meta_data = "GET /"; const char *post_start = "POST /start"; const char *post_move = "POST /move"; @@ -121,9 +132,11 @@ int main(int argc, char *argv[]) { if (strncmp(buffer, get_meta_data, strlen(get_meta_data)) == 0) { handle_get_meta_data(client_socket); } else if (strncmp(buffer, post_start, strlen(post_start)) == 0) { - handle_start(client_socket, get_body(buffer)); + char *body = get_body(client_socket, buffer, buffer_size, bytes); + handle_start(client_socket, body); } else if (strncmp(buffer, post_move, strlen(post_move)) == 0) { - handle_move(client_socket, get_body(buffer)); + char *body = get_body(client_socket, buffer, buffer_size, bytes); + handle_move(client_socket, body); } else if (strncmp(buffer, post_end, strlen(post_end)) == 0) { handle_end(client_socket); }