diff --git a/common/socket_helper.c b/common/socket_helper.c index 2e41f86..928260f 100644 --- a/common/socket_helper.c +++ b/common/socket_helper.c @@ -85,6 +85,24 @@ static struct line_read_result read_line(const char *buffer, int buffer_size, st return result; } +static struct socket_read_info get_read_info(const struct http_header *headers) { + const struct http_header *header_cursor = headers; + struct socket_read_info info = {STRATEGY_EOF, -1}; + while (header_cursor != NULL) { + header_cursor = header_cursor->next_header; + if (headercmp(header_cursor, HEADER_TRANSFER_ENCODING) == 0 && + case_insensitive_strcmp(header_cursor->value, ENCODING_TYPE_CHUNKED)) { + info.strategy = STRATEGY_CHUNKED; + } else if (headercmp(header_cursor, HEADER_CONTENT_LENGTH) == 0 + && info.strategy != STRATEGY_CHUNKED) { + info.strategy = STRATEGY_CONTENT_LENGTH; + info.length = atoi(header_cursor->value); + } + } + + return info; +} + enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message *message) { ssize_t current_result_size = 0; char *result = NULL; diff --git a/common/socket_helper.h b/common/socket_helper.h index 0246084..b2c162c 100644 --- a/common/socket_helper.h +++ b/common/socket_helper.h @@ -4,15 +4,23 @@ #include "http_types.h" #define BUFFER_SIZE 1024 +#define HEADER_CONTENT_LENGTH "Content-Length" +#define HEADER_TRANSFER_ENCODING "Transfer-Encoding" +#define ENCODING_TYPE_CHUNKED "chunked" enum line_type {RESULT_START_LINE, RESULT_HEADER, RESULT_BLANK_LINE, RESULT_NO_LINE}; enum socket_read_result {RESULT_OK, RESULT_MALFORMED, RESULT_READ_ERROR}; +enum socket_read_strategy {STRATEGY_CHUNKED, STRATEGY_CONTENT_LENGTH, STRATEGY_EOF}; struct line_read_result { enum line_type line_type; // TODO: this just gets freed... do we need it? char *line; int bytes_read; }; +struct socket_read_info { + enum socket_read_strategy strategy; + int length; +}; enum socket_read_result get_all_remote_parts(int socket_fd, struct http_message *message);