]> git.seodisparate.com - c_simple_http/commitdiff
Remove "?" or "#" starting suffixes from path
authorStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 06:33:31 +0000 (15:33 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 06:33:31 +0000 (15:33 +0900)
These suffixes may break loading the correct html template.

src/http.c
src/http.h

index a43648ab11c5f564bdb7d1a98a94db7fd7600410..c2b3770afff80f14ce3ce5407533523dc5dae3ea 100644 (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
index e87ae0275d336c8e3b2285321c0927b2661b30e5..f1b7b0557910e116e52c9d88adb628e5d5e2c569 100644 (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