Remove "?" or "#" starting suffixes from path

These suffixes may break loading the correct html template.
This commit is contained in:
Stephen Seo 2024-09-06 15:33:31 +09:00
parent 0196588f51
commit 42d97e03b3
2 changed files with 33 additions and 1 deletions

View file

@ -116,10 +116,17 @@ char *c_simple_http_request_response(
}
size_t generated_size = 0;
char *stripped_path = c_simple_http_strip_path(
request_path, request_path_idx);
char *generated_buf = c_simple_http_path_to_generated(
request_path,
stripped_path ? stripped_path : request_path,
templates,
&generated_size);
if (stripped_path) {
free(stripped_path);
}
if (!generated_buf || generated_size == 0) {
fprintf(stderr, "ERROR Unable to generate response html for path \"%s\"!\n",
request_path);
@ -132,4 +139,23 @@ char *c_simple_http_request_response(
return generated_buf;
}
char *c_simple_http_strip_path(const char *path, size_t path_size) {
size_t idx = 0;
for (; idx < path_size && path[idx] != 0; ++idx) {
if (path[idx] == '?' || path[idx] == '#') {
break;
}
}
if (idx >= path_size || path[idx] == 0) {
return NULL;
}
char *stripped_path = malloc(idx + 1);
memcpy(stripped_path, path, idx);
stripped_path[idx] = 0;
return stripped_path;
}
// vim: ts=2 sts=2 sw=2

View file

@ -37,6 +37,12 @@ char *c_simple_http_request_response(
size_t *out_size
);
/// Takes a PATH string and returns a "bare" path.
/// This will simply omit the first instance of "?" or "#" and the rest of the
/// string.
/// Must be free'd if returns non-NULL.
char *c_simple_http_strip_path(const char *path, size_t path_size);
#endif
// vim: ts=2 sts=2 sw=2