Change content length to use strtol

This commit is contained in:
Nick Krichevsky 2018-09-03 16:41:00 -04:00
parent 17d9767394
commit 74d23ebebe
2 changed files with 16 additions and 6 deletions

View file

@ -1,5 +1,6 @@
#include "http_types.h"
#include "socket_helper.h"
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@ -107,7 +108,12 @@ static struct socket_read_info get_read_info(const struct http_header *headers)
} else if (headercmp(header_cursor, HEADER_CONTENT_LENGTH) == 0
&& info.strategy != STRATEGY_CHUNKED) {
info.strategy = STRATEGY_CONTENT_LENGTH;
info.length = atoi(header_cursor->value);
info.length = strtol(header_cursor->value, NULL, 10);
if (errno == EINVAL || errno == ERANGE) {
info.strategy = STRATEGY_UNDEFINED;
info.length = -1;
return info;
}
}
header_cursor = header_cursor->next_header;
}
@ -123,7 +129,7 @@ static struct socket_read_info get_read_info(const struct http_header *headers)
*
* @return The content in the socket. Must be freed by the caller.
*/
static char *read_body_by_content_length(int socket_fd, int content_length) {
static char *read_body_by_content_length(int socket_fd, long content_length) {
char *result = malloc((content_length + 1) * sizeof(char));
char *buffer = malloc(content_length * sizeof(char));
int total_bytes_read = 0;
@ -234,10 +240,14 @@ enum socket_read_result get_all_remote_parts(int socket_fd, enum socket_type typ
}
struct socket_read_info read_info = get_read_info(message->headers);
char *body_result;
if (read_info.strategy == STRATEGY_CHUNKED) {
if (read_info.strategy == STRATEGY_UNDEFINED) {
free(buffer);
free(result);
return RESULT_MALFORMED;
} else if (read_info.strategy == STRATEGY_CHUNKED) {
// TODO: Implement
} else if (read_info.strategy == STRATEGY_CONTENT_LENGTH) {
int net_content_length = read_info.length - (current_result_size - read_offset);
long net_content_length = read_info.length - (current_result_size - read_offset);
// Include space for null term
current_result_size += net_content_length + 1;
result = realloc(result, current_result_size);

View file

@ -10,7 +10,7 @@
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, RESULT_PROCESSING_ERROR};
enum socket_read_strategy {STRATEGY_CHUNKED, STRATEGY_CONTENT_LENGTH, STRATEGY_EOF};
enum socket_read_strategy {STRATEGY_CHUNKED, STRATEGY_CONTENT_LENGTH, STRATEGY_EOF, STRATEGY_UNDEFINED};
enum socket_type {TYPE_CLIENT, TYPE_SERVER};
struct line_read_result {
enum line_type line_type;
@ -20,7 +20,7 @@ struct line_read_result {
};
struct socket_read_info {
enum socket_read_strategy strategy;
int length;
long length;
};
enum socket_read_result get_all_remote_parts(int socket_fd, enum socket_type type, struct http_message *message);