Add support for reading until EOF

This commit is contained in:
Nick Krichevsky 2018-09-02 20:37:34 -04:00
parent a958916a41
commit 735c399405

View file

@ -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);