int c_simple_http_required_iter_fn(void *data, void *ud) {
C_SIMPLE_HTTP_INTERNAL_RequiredIter *req_iter_struct = ud;
+ unsigned int data_str_length = (unsigned int)strlen(data) + 1;
if (simple_archiver_hash_map_get(
req_iter_struct->hash_map,
data,
- strlen(data) + 1) == NULL) {
+ data_str_length) == NULL) {
if (req_iter_struct->path) {
fprintf(
stderr,
int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) {
C_SIMPLE_HTTP_INTERNAL_RequiredCheck *req = ud;
+ unsigned int path_void_str_len = (unsigned int)strlen(path_void_str) + 1;
C_SIMPLE_HTTP_HashMapWrapper *wrapper =
simple_archiver_hash_map_get(
req->map_of_paths_and_their_vars,
path_void_str,
- strlen(path_void_str) + 1);
+ path_void_str_len);
if (!wrapper) {
fprintf(stderr,
"WARNING: Map of paths does not have path \"%s\"!\n",
fprintf(stderr, "ERROR: separating_key argument is NULL!\n");
return config;
}
- const unsigned int separating_key_size = strlen(separating_key) + 1;
+ const unsigned int separating_key_size =
+ (unsigned int)strlen(separating_key) + 1;
config.hash_map = simple_archiver_hash_map_init();
}
if ((state & 1) == 0) {
if (c != '=') {
- key_buf[key_idx++] = c;
+ key_buf[key_idx++] = (unsigned char)c;
if (key_idx >= C_SIMPLE_HTTP_CONFIG_BUF_SIZE) {
fprintf(stderr,
"ERROR: config file \"key\" is larger than %u bytes!\n",
}
} else if ((state & 1) == 1) {
if ((c != '\n' && c != '\r') || (state & 0xC) != 0) {
- value_buf[value_idx++] = c;
+ value_buf[value_idx++] = (unsigned char)c;
if (value_idx >= C_SIMPLE_HTTP_CONFIG_BUF_SIZE) {
fprintf(stderr,
"ERROR: config file \"value\" is larger than %u bytes!\n",
if (output_buf_size) {
*output_buf_size = 0;
}
+ size_t path_len_size_t = strlen(path) + 1;
+ if (path_len_size_t > 0xFFFFFFFF) {
+ fprintf(stderr, "ERROR: Path string is too large!\n");
+ return NULL;
+ }
+ unsigned int path_len = (unsigned int)path_len_size_t;
C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map =
- simple_archiver_hash_map_get(templates->hash_map, path, strlen(path) + 1);
+ simple_archiver_hash_map_get(templates->hash_map, path, path_len);
if (!wrapped_hash_map) {
return NULL;
}
if (fseek(f, 0, SEEK_SET) != 0) {
return NULL;
}
- html_buf = malloc(html_file_size + 1);
- size_t ret = fread(html_buf, 1, html_file_size, f);
+ html_buf = malloc((size_t)html_file_size + 1);
+ size_t ret = fread(html_buf, 1, (size_t)html_file_size, f);
if (ret != (size_t)html_file_size) {
return NULL;
}
html_buf[html_file_size] = 0;
- html_buf_size = html_file_size;
+ html_buf_size = (size_t)html_file_size;
} else {
char *stored_html =
simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5);
simple_archiver_hash_map_get(
wrapped_hash_map->hash_map,
var,
- var_size + 1);
+ (unsigned int)var_size + 1);
if (value_c_str) {
if (c_simple_http_internal_ends_with_FILE(var) == 0) {
// Load from file specified by "value_c_str".
#include "constants.h"
#include "http.h"
+#define CHECK_ERROR_WRITE(write_expr) \
+ if (write_expr < 0) { \
+ close(connection_fd); \
+ fprintf(stderr, "ERROR Failed to write to connected peer, closing...\n"); \
+ continue; \
+ }
+
int main(int argc, char **argv) {
Args args = parse_args(argc, argv);
puts("");
int connection_fd = ret;
read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE);
+ if (read_ret < 0) {
+ close(connection_fd);
+ fprintf(
+ stderr,
+ "WARNING Failed to read from new connection, closing...\n");
+ continue;
+ }
// DEBUG print received buf.
for (unsigned int idx = 0;
idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret;
size_t response_size = 0;
const char *response = c_simple_http_request_response(
- (const char*)recv_buf, read_ret, &parsed_config, &response_size);
+ (const char*)recv_buf, (unsigned int)read_ret, &parsed_config, &response_size);
if (response) {
- write(connection_fd, "HTTP/1.1 200 OK\n", 16);
- write(connection_fd, "Allow: GET\n", 11);
- write(connection_fd, "Content-Type: text/html\n", 24);
+ CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 200 OK\n", 16));
+ CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));
+ CHECK_ERROR_WRITE(write(connection_fd, "Content-Type: text/html\n", 24));
char content_length_buf[128];
size_t content_length_buf_size = 0;
memcpy(content_length_buf, "Content-Length: ", 16);
"%lu\n%n",
response_size,
&written);
- content_length_buf_size += written;
- write(connection_fd, content_length_buf, content_length_buf_size);
- write(connection_fd, "\n", 1);
- write(connection_fd, response, response_size);
+ if (written <= 0) {
+ close(connection_fd);
+ fprintf(
+ stderr,
+ "WARNING Failed to write in response, closing connection...\n");
+ continue;
+ }
+ content_length_buf_size += (size_t)written;
+ CHECK_ERROR_WRITE(write(connection_fd, content_length_buf, content_length_buf_size));
+ CHECK_ERROR_WRITE(write(connection_fd, "\n", 1));
+ CHECK_ERROR_WRITE(write(connection_fd, response, response_size));
free((void*)response);
} else {
// TODO handle internal errors also.
- write(connection_fd, "HTTP/1.1 404 Not Found\n", 23);
- write(connection_fd, "Allow: GET\n", 11);
- write(connection_fd, "Content-Type: text/html\n", 24);
- write(connection_fd, "Content-Length: 14\n", 19);
- write(connection_fd, "\n404 Not Found\n", 15);
+ CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 404 Not Found\n", 23));
+ CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));
+ CHECK_ERROR_WRITE(write(connection_fd, "Content-Type: text/html\n", 24));
+ CHECK_ERROR_WRITE(write(connection_fd, "Content-Length: 14\n", 19));
+ CHECK_ERROR_WRITE(write(connection_fd, "\n404 Not Found\n", 15));
}
close(connection_fd);
} else {