From 206cad6f57daf5641f4f79555fb87a6c54db7887 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 23 Sep 2024 17:42:02 +0900 Subject: [PATCH] Impl. alternate delimeter creating cache-filename --- src/html_cache.c | 28 +++++++++++++++++++++++----- src/test.c | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/html_cache.c b/src/html_cache.c index f851c09..d5dbc3c 100644 --- a/src/html_cache.c +++ b/src/html_cache.c @@ -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; } diff --git a/src/test.c b/src/test.c index 86cc92a..8f049f4 100644 --- a/src/test.c +++ b/src/test.c @@ -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()