Fix memory issues
parent
d25c063a04
commit
42b43003aa
|
@ -249,6 +249,8 @@ enum socket_result get_all_remote_parts(int socket_fd, enum socket_type type, st
|
|||
// Servers cannot just wait for EOF with a client, so we just skip the action.
|
||||
body_result = malloc(1 * sizeof(char));
|
||||
*body_result = '\0';
|
||||
current_result_size += 1;
|
||||
result = realloc(result, current_result_size);
|
||||
} else {
|
||||
// In all other cases, such as if we have EOF strategy on a client.
|
||||
body_result = read_body_until_eof(socket_fd);
|
||||
|
|
|
@ -81,6 +81,17 @@ int parse_request_line(const char *line, struct request_line *parsed_line) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free a request_line made by parse_request_line
|
||||
*
|
||||
* @param parsed_line The request_line made by parse_request_line
|
||||
*/
|
||||
void free_request_line_items(struct request_line *parsed_line) {
|
||||
free(parsed_line->http_version);
|
||||
free(parsed_line->method);
|
||||
free(parsed_line->uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a FILE from a given uri.
|
||||
*
|
||||
|
|
|
@ -15,6 +15,7 @@ struct requested_file {
|
|||
};
|
||||
|
||||
int parse_request_line(const char *line, struct request_line *parsed_line);
|
||||
void free_request_line_items(struct request_line *parsed_line);
|
||||
int get_file_from_url(const char *uri, struct requested_file *req_file);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -46,24 +46,28 @@ struct server_info setup(int port) {
|
|||
struct addrinfo *addr_info;
|
||||
int addr_status = build_addr_info(port, &addr_info);
|
||||
if (addr_status != 0) {
|
||||
freeaddrinfo(addr_info);
|
||||
info.status = STATUS_ERROR;
|
||||
return info;
|
||||
}
|
||||
|
||||
info.sock_fd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol);
|
||||
if (info.sock_fd == -1) {
|
||||
freeaddrinfo(addr_info);
|
||||
info.status = STATUS_ERROR;
|
||||
return info;
|
||||
}
|
||||
|
||||
int bind_result = bind(info.sock_fd, addr_info->ai_addr, addr_info->ai_addrlen);
|
||||
if (bind_result == -1) {
|
||||
freeaddrinfo(addr_info);
|
||||
close(info.sock_fd);
|
||||
info.sock_fd = -1;
|
||||
info.status = STATUS_ERROR;
|
||||
return info;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr_info);
|
||||
int listen_result = listen(info.sock_fd, BACKLOG_SIZE);
|
||||
if (listen_result == -1) {
|
||||
close(info.sock_fd);
|
||||
|
@ -110,7 +114,7 @@ static int send_headers(int status_code, int client_socket, long content_length)
|
|||
char *content_length_string = malloc((content_length_length + 1) * sizeof(char));
|
||||
sprintf(content_length_string, "%ld", content_length);
|
||||
const char *content_length_header_components[] = {HEADER_CONTENT_LENGTH, ": ", content_length_string, "\r\n"};
|
||||
int content_length_header_length = get_buffer_size(content_length_header_components, 4);
|
||||
int content_length_header_length = get_buffer_size(content_length_header_components, 4) + 1;
|
||||
header_buffer_size += content_length_header_length;
|
||||
content_length_header_buffer = malloc(content_length_header_length * sizeof(char));
|
||||
sprintf(content_length_header_buffer, "%s: %s\r\n", HEADER_CONTENT_LENGTH, content_length_string);
|
||||
|
@ -175,6 +179,12 @@ enum socket_result serve_one_request(int sock_fd) {
|
|||
int parse_result = parse_request_line(message.start_line, &parsed_request_line);
|
||||
if (parse_result == -1) {
|
||||
send_headers(400, client_fd, -1);
|
||||
free_request_line_items(&parsed_request_line);
|
||||
free(message.start_line);
|
||||
free(message.contents);
|
||||
if (message.headers != NULL) {
|
||||
free_headers(message.headers);
|
||||
}
|
||||
shutdown(client_fd, SHUT_RDWR);
|
||||
close(client_fd);
|
||||
return RESULT_PROCESSING_ERROR;
|
||||
|
@ -188,6 +198,12 @@ enum socket_result serve_one_request(int sock_fd) {
|
|||
} else if (errno == EACCES) {
|
||||
send_result = send_headers(403, client_fd, -1);
|
||||
} else {
|
||||
free_request_line_items(&parsed_request_line);
|
||||
free(message.start_line);
|
||||
free(message.contents);
|
||||
if (message.headers != NULL) {
|
||||
free_headers(message.headers);
|
||||
}
|
||||
send_headers(500, client_fd, -1);
|
||||
shutdown(client_fd, SHUT_RDWR);
|
||||
close(client_fd);
|
||||
|
@ -195,6 +211,12 @@ enum socket_result serve_one_request(int sock_fd) {
|
|||
}
|
||||
|
||||
if (send_result != 0) {
|
||||
free_request_line_items(&parsed_request_line);
|
||||
free(message.start_line);
|
||||
free(message.contents);
|
||||
if (message.headers != NULL) {
|
||||
free_headers(message.headers);
|
||||
}
|
||||
shutdown(client_fd, SHUT_RDWR);
|
||||
close(client_fd);
|
||||
return RESULT_WRITE_ERROR;
|
||||
|
@ -203,6 +225,15 @@ enum socket_result serve_one_request(int sock_fd) {
|
|||
send_headers(200, client_fd, file.size);
|
||||
send_file(client_fd, file);
|
||||
}
|
||||
free_request_line_items(&parsed_request_line);
|
||||
free(message.start_line);
|
||||
free(message.contents);
|
||||
if (message.headers != NULL) {
|
||||
free_headers(message.headers);
|
||||
}
|
||||
if (file_result != -1) {
|
||||
fclose(file.file);
|
||||
}
|
||||
|
||||
shutdown(client_fd, SHUT_RDWR);
|
||||
close(client_fd);
|
||||
|
|
Loading…
Reference in New Issue