Fix case where URL has extra trailing '/'
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-16 11:20:15 +09:00
parent ea88466a98
commit f15452baa4
2 changed files with 10 additions and 5 deletions

View 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); char *stripped_path = malloc(idx + 1);
memcpy(stripped_path, path, idx); memcpy(stripped_path, path, idx);
stripped_path[idx] = 0; 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; return stripped_path;
} }

View file

@ -52,7 +52,7 @@ char *c_simple_http_request_response(
/// Takes a PATH string and returns a "bare" path. /// Takes a PATH string and returns a "bare" path.
/// This will simply omit the first instance of "?" or "#" and the rest of the /// 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. /// Must be free'd if returns non-NULL.
char *c_simple_http_strip_path(const char *path, size_t path_size); char *c_simple_http_strip_path(const char *path, size_t path_size);