]> git.seodisparate.com - c_simple_http/commitdiff
Fix case where URL has extra trailing '/'
authorStephen Seo <seo.disparate@gmail.com>
Mon, 16 Sep 2024 02:20:15 +0000 (11:20 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 16 Sep 2024 02:20:15 +0000 (11:20 +0900)
src/http.c
src/http.h

index 701d8fd0afc0419319c49b3a72175543e963a73c..a3062cc4ed67220e8b68a5dce7a0853c20d540e7 100644 (file)
@@ -199,14 +199,19 @@ char *c_simple_http_strip_path(const char *path, size_t path_size) {
     }
   }
 
-  if (idx >= path_size || path[idx] == 0) {
-    return NULL;
-  }
-
   char *stripped_path = malloc(idx + 1);
   memcpy(stripped_path, path, idx);
   stripped_path[idx] = 0;
 
+  // Strip trailing '/'.
+  while (idx-- > 1) {
+    if (stripped_path[idx] == '/' || stripped_path[idx] == 0) {
+      stripped_path[idx] = 0;
+    } else {
+      break;
+    }
+  }
+
   return stripped_path;
 }
 
index 411ff6bdb07715590061ade9b5d14e71d71896bc..766214b2516872a30200a8734bc6333455ad8911 100644 (file)
@@ -52,7 +52,7 @@ char *c_simple_http_request_response(
 
 /// Takes a PATH string and returns a "bare" path.
 /// This will simply omit the first instance of "?" or "#" and the rest of the
-/// string.
+/// string. This will also remove trailing "/" characters.
 /// Must be free'd if returns non-NULL.
 char *c_simple_http_strip_path(const char *path, size_t path_size);