From 42d97e03b3d6a1fdeddb23b8ea500c09aa4c7450 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 6 Sep 2024 15:33:31 +0900 Subject: [PATCH] Remove "?" or "#" starting suffixes from path These suffixes may break loading the correct html template. --- src/http.c | 28 +++++++++++++++++++++++++++- src/http.h | 6 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/http.c b/src/http.c index a43648a..c2b3770 100644 --- a/src/http.c +++ b/src/http.c @@ -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 diff --git a/src/http.h b/src/http.h index e87ae02..f1b7b05 100644 --- a/src/http.h +++ b/src/http.h @@ -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