Fix buffering issues in send_headers

master
Nick Krichevsky 2018-09-09 19:46:12 -04:00
parent e9e8bb8175
commit ca7c7e9557
1 changed files with 5 additions and 3 deletions

View File

@ -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 <errno.h>
#include <math.h>
#include <netdb.h>
#include <stdio.h>
@ -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);