From 735c399405e3905fde9acfee4d9fcea2013272ee Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 2 Sep 2018 20:37:34 -0400 Subject: [PATCH] Add support for reading until EOF --- common/socket_helper.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/common/socket_helper.c b/common/socket_helper.c index 59cb4f4..557b588 100644 --- a/common/socket_helper.c +++ b/common/socket_helper.c @@ -137,6 +137,29 @@ static char *read_body_by_content_length(int socket_fd, int content_length) { return result; } +static char *read_body_until_eof(int socket_fd) { + ssize_t current_result_size = 0; + int write_offset = 0; + char *buffer = malloc(BUFFER_SIZE * sizeof(char)); + char *result = NULL; + ssize_t bytes_read; + while ((bytes_read = read(socket_fd, buffer, BUFFER_SIZE)) > 0) { + current_result_size += bytes_read; + if (result == NULL) { + result = malloc(bytes_read); + } else { + result = realloc(result, current_result_size); + } + memcpy(result + write_offset, buffer, bytes_read); + write_offset += bytes_read; + } + free(buffer); + result = realloc(result, current_result_size + 1); + result[current_result_size] = '\0'; + + return result; +} + //TODO: specify request vs response for eof differences enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message *message) { ssize_t current_result_size = 0; @@ -199,7 +222,9 @@ enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message result = realloc(result, current_result_size); body_result = read_body_by_content_length(socket_fd, net_content_length); } else { - // TODO: Implement + body_result = read_body_until_eof(socket_fd); + current_result_size += strlen(body_result); + result = realloc(result, current_result_size); } strcpy(result + write_offset, body_result); free(body_result);