Skip to content

Commit

Permalink
Refactor URL length limits to be configurable
Browse files Browse the repository at this point in the history
Removed hardcoded maximum URL length definitions in favor of using configurable settings within `http_server_settings`. Updated `parse_http_request_line_from_packet` function to utilize these settings, and modified the test suite to set `max_url_length` accordingly.
  • Loading branch information
SeriousSamV committed Nov 1, 2024
1 parent 2bff132 commit 3b52314
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/tiny_http/tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ enum parse_http_request_status parse_http_request_headers(
}

enum parse_http_request_status parse_http_request_line_from_packet(
const http_server_settings *const settings,
const uint8_t *const http_packet,
const size_t http_packet_len,
http_request *request,
Expand Down Expand Up @@ -335,7 +336,9 @@ enum parse_http_request_status parse_http_request_line_from_packet(
printf("request method: %d", request->method);
#endif

for (int iter_cnt = 0; *ptr < http_packet_len && iter_cnt < MAX_URL_LENGTH; (*ptr)++, iter_cnt++) {
for (int iter_cnt = 0;
*ptr < http_packet_len && iter_cnt < settings->max_url_length;
(*ptr)++, iter_cnt++) {
if (http_packet[(*ptr)] == ' ') {
request->url = strndup((char *) &http_packet[start_uri], *ptr - start_uri);
(*ptr)++;
Expand Down Expand Up @@ -403,7 +406,7 @@ http_request *parse_http_request(
size_t ptr = 0;

const enum parse_http_request_status request_line_parse_status =
parse_http_request_line_from_packet(http_packet, http_packet_len, request, &ptr);
parse_http_request_line_from_packet(settings, http_packet, http_packet_len, request, &ptr);
if (request_line_parse_status != PARSE_OK) {
destroy_http_request(request);
return nullptr;
Expand Down
5 changes: 1 addition & 4 deletions src/tiny_http/tiny_http_server_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include <stdint.h>
#include <stddef.h>

#define MAX_URL_LENGTH 8000
#define MAX_HTTP_HEADER_NAME_LENGTH 8000
#define MAX_HTTP_HEADER_VALUE_LENGTH 8000

typedef enum http_version {
HTTP_1_0 = 1,
} http_version;
Expand Down Expand Up @@ -51,6 +47,7 @@ typedef struct http_server_settings {
size_t max_header_name_length;
size_t max_header_value_length;
size_t max_body_length;
size_t max_url_length;
} http_server_settings;

/**
Expand Down
1 change: 1 addition & 0 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ http_server_settings settings = {
.max_header_name_length = 256,
.max_header_value_length = 512,
.max_body_length = 1024 * 1024 * 8, // 8M
.max_url_length = 8000 // NOTE: should be ~2000, but meh the standards doesn't have a comment on it, SEO says 2000
};

void test_request_parse_get_root_curl(void) {
Expand Down

0 comments on commit 3b52314

Please sign in to comment.