From ca7c7e9557b2c17dc0628cb06e2a9e0ce1541cae Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 9 Sep 2018 19:46:12 -0400 Subject: [PATCH] Fix buffering issues in send_headers --- server/socket_server.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/socket_server.c b/server/socket_server.c index a923b58..26f0e56 100644 --- a/server/socket_server.c +++ b/server/socket_server.c @@ -1,8 +1,10 @@ #include "socket_server.h" +#include "request_handling.h" #include "../common/http_types.h" #include "../common/socket_helper.h" #include "../common/buffer_helper.h" #include "status_codes.h" +#include #include #include #include @@ -99,7 +101,8 @@ static int send_headers(int status_code, int client_socket, int content_length) // RFC2616 states that if we don't intend to persist the connection, we must send "Connection: close" const char *connection_header_components[] = {HEADER_CONNECTION, ": close\r\n"}; char *content_length_header_buffer = ""; - int header_buffer_size = get_buffer_size(connection_header_components, 2); + // Include space for the CRLF and null term + int header_buffer_size = get_buffer_size(connection_header_components, 2) + 3; if (content_length > 0) { // Get the number of chars in the port int content_length_length = floor(log10(content_length)) + 1; @@ -113,8 +116,7 @@ static int send_headers(int status_code, int client_socket, int content_length) free(content_length_string); } - // Include space for the null term and the CRLF - char *header_buffer = malloc((header_buffer_size + 3) * sizeof(char)); + char *header_buffer = malloc(header_buffer_size * sizeof(char)); sprintf(header_buffer, "%s: close\r\n%s\r\n", HEADER_CONNECTION, content_length_header_buffer); if (content_length > 0) { free(content_length_header_buffer);