Impl. alternate delimeter creating cache-filename
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 8s

This commit is contained in:
Stephen Seo 2024-09-23 17:42:02 +09:00
parent 7cc0d624be
commit 206cad6f57
2 changed files with 40 additions and 5 deletions

View file

@ -41,12 +41,23 @@ char *c_simple_http_path_to_cache_filename(const char *path) {
return buf; 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))) __attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *parts = simple_archiver_list_init(); SDArchiverLinkedList *parts = simple_archiver_list_init();
size_t idx = 0; size_t idx = 0;
size_t prev_idx = 0; size_t prev_idx = 0;
const size_t path_len = strlen(stripped_path);
size_t size; size_t size;
for (; idx < path_len && stripped_path[idx] != 0; ++idx) { 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); c_simple_http_add_string_part(parts, temp_buf, 0);
free(temp_buf); free(temp_buf);
if (is_normal_delimeter) {
temp_buf = malloc(5); temp_buf = malloc(5);
memcpy(temp_buf, "0x2F", 5); memcpy(temp_buf, "0x2F", 5);
c_simple_http_add_string_part(parts, temp_buf, 0); c_simple_http_add_string_part(parts, temp_buf, 0);
free(temp_buf); 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; prev_idx = idx + 1;
} }

View file

@ -599,6 +599,23 @@ int main(void) {
ret = c_simple_http_path_to_cache_filename("/outer///inner"); ret = c_simple_http_path_to_cache_filename("/outer///inner");
CHECK_TRUE(strcmp(ret, "0x2Fouter0x2Finner") == 0); CHECK_TRUE(strcmp(ret, "0x2Fouter0x2Finner") == 0);
free(ret); 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() RETURN()