]> git.seodisparate.com - c_simple_http/commitdiff
Impl. alternate delimeter creating cache-filename
authorStephen Seo <seo.disparate@gmail.com>
Mon, 23 Sep 2024 08:42:02 +0000 (17:42 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 23 Sep 2024 08:42:02 +0000 (17:42 +0900)
src/html_cache.c
src/test.c

index f851c09162fcfd3f5d22c53a32d27323cd55f8c7..d5dbc3c0cbdcbb2761ea1f7f324f43c5dd3d3577 100644 (file)
@@ -41,12 +41,23 @@ char *c_simple_http_path_to_cache_filename(const char *path) {
     return buf;
   }
 
+  // Check if path has "0x2F" inside of it to determine if delimeters will be
+  // "0x2F" or "%2F".
+  uint_fast8_t is_normal_delimeter = 1;
+  const size_t path_len = strlen(stripped_path);
+  for (size_t idx = 0; stripped_path[idx] != 0; ++idx) {
+    if (idx + 4 <= path_len && strncmp(stripped_path + idx, "0x2F", 4) == 0) {
+      is_normal_delimeter = 0;
+      break;
+    }
+  }
+
+  // Create the cache-filename piece by piece.
   __attribute__((cleanup(simple_archiver_list_free)))
   SDArchiverLinkedList *parts = simple_archiver_list_init();
 
   size_t idx = 0;
   size_t prev_idx = 0;
-  const size_t path_len = strlen(stripped_path);
   size_t size;
 
   for (; idx < path_len && stripped_path[idx] != 0; ++idx) {
@@ -58,10 +69,17 @@ char *c_simple_http_path_to_cache_filename(const char *path) {
       c_simple_http_add_string_part(parts, temp_buf, 0);
       free(temp_buf);
 
-      temp_buf = malloc(5);
-      memcpy(temp_buf, "0x2F", 5);
-      c_simple_http_add_string_part(parts, temp_buf, 0);
-      free(temp_buf);
+      if (is_normal_delimeter) {
+        temp_buf = malloc(5);
+        memcpy(temp_buf, "0x2F", 5);
+        c_simple_http_add_string_part(parts, temp_buf, 0);
+        free(temp_buf);
+      } else {
+        temp_buf = malloc(4);
+        memcpy(temp_buf, "%2F", 4);
+        c_simple_http_add_string_part(parts, temp_buf, 0);
+        free(temp_buf);
+      }
 
       prev_idx = idx + 1;
     }
index 86cc92af9275aa32a86e4de2d589a57ba2101e5c..8f049f44d5a66a10bc2d0cdb5f4dfb7f886472d1 100644 (file)
@@ -599,6 +599,23 @@ int main(void) {
     ret = c_simple_http_path_to_cache_filename("/outer///inner");
     CHECK_TRUE(strcmp(ret, "0x2Fouter0x2Finner") == 0);
     free(ret);
+
+    ret = c_simple_http_path_to_cache_filename("/outer/with_hex_0x2F_inner");
+    CHECK_TRUE(strcmp(ret, "%2Fouter%2Fwith_hex_0x2F_inner") == 0);
+    free(ret);
+
+    ret = c_simple_http_path_to_cache_filename("/outer/0x2F_hex_inner");
+    CHECK_TRUE(strcmp(ret, "%2Fouter%2F0x2F_hex_inner") == 0);
+    free(ret);
+
+    ret = c_simple_http_path_to_cache_filename("/outer0x2F/inner_hex_0x2F");
+    CHECK_TRUE(strcmp(ret, "%2Fouter0x2F%2Finner_hex_0x2F") == 0);
+    free(ret);
+
+    ret = c_simple_http_path_to_cache_filename(
+        "/0x2Fouter0x2F/0x2Finner_0x2F_hex_0x2F");
+    CHECK_TRUE(strcmp(ret, "%2F0x2Fouter0x2F%2F0x2Finner_0x2F_hex_0x2F") == 0);
+    free(ret);
   }
 
   RETURN()