normalize_path removes prefix one ./ and multi ../

pull/42/head
YuQing 2021-12-23 10:04:48 +08:00
parent fdb6bfb233
commit 750c2c5e8a
2 changed files with 42 additions and 7 deletions

View File

@ -3031,8 +3031,12 @@ char *format_http_date(time_t t, BufferInfo *buffer)
int normalize_path(const char *from, const char *filename, int normalize_path(const char *from, const char *filename,
char *full_filename, const int size) char *full_filename, const int size)
{ {
const char *start;
const char *last; const char *last;
int len; const char *end;
int up_count;
int path_len;
int i;
if (IS_FILE_RESOURCE(from)) { if (IS_FILE_RESOURCE(from)) {
from = from + FILE_RESOURCE_TAG_LEN; from = from + FILE_RESOURCE_TAG_LEN;
@ -3047,8 +3051,35 @@ int normalize_path(const char *from, const char *filename,
last = strrchr(from, '/'); last = strrchr(from, '/');
if (last != NULL) { if (last != NULL) {
len = last - from; end = filename + strlen(filename);
return snprintf(full_filename, size, "%.*s/%s", len, from, filename); if (memcmp(filename, "./", 2) == 0) {
start = filename + 2;
} else {
start = filename;
}
up_count = 0;
while (start + 3 < end) {
if (memcmp(start, "../", 3) != 0) {
break;
}
++up_count;
start += 3;
}
path_len = last - from;
for (i=0; i<up_count; i++) {
last = fc_memrchr(from, '/', path_len);
if (last == NULL) {
logWarning("file: "__FILE__", line: %d, "
"too many ../ in the path resolve filename: %s, "
"from filename: %s", __LINE__, filename, from);
break;
}
path_len = last - from;
}
return snprintf(full_filename, size, "%.*s/%s",
path_len, from, start);
} else { } else {
logWarning("file: "__FILE__", line: %d, " logWarning("file: "__FILE__", line: %d, "
"no \"/\" in the from filename: %s", "no \"/\" in the from filename: %s",
@ -3077,8 +3108,12 @@ int normalize_uri(const string_t *from, const char *uri,
} }
end = uri + strlen(uri); end = uri + strlen(uri);
up_count = 0; if (memcmp(uri, "./", 2) == 0) {
start = uri + 2;
} else {
start = uri; start = uri;
}
up_count = 0;
while (start + 3 < end) { while (start + 3 < end) {
if (memcmp(start, "../", 3) != 0) { if (memcmp(start, "../", 3) != 0) {
break; break;
@ -3098,7 +3133,7 @@ int normalize_uri(const string_t *from, const char *uri,
if (up_count == 0) { if (up_count == 0) {
return snprintf(dest, size, "%.*s/%s", return snprintf(dest, size, "%.*s/%s",
(int)(last - from->str), from->str, uri); (int)(last - from->str), from->str, start);
} else { } else {
fpath.str = (char *)from->str; fpath.str = (char *)from->str;
fpath.len = last - from->str; fpath.len = last - from->str;

View File

@ -26,7 +26,7 @@
#include "fastcommon/array_allocator.h" #include "fastcommon/array_allocator.h"
#include "fastcommon/sorted_array.h" #include "fastcommon/sorted_array.h"
#define ELEMENT_COUNT 1000 #define ELEMENT_COUNT 64 * 1024
static bool silence; static bool silence;
@ -72,7 +72,7 @@ static int test_i64()
} }
last_index = ELEMENT_COUNT - 1; last_index = ELEMENT_COUNT - 1;
for (i=0; i<input->count; i++) { for (i=0; i<ELEMENT_COUNT / 10; i++) {
index = (int64_t)rand() * last_index / (int64_t)RAND_MAX; index = (int64_t)rand() * last_index / (int64_t)RAND_MAX;
tmp = input->elts[index]; tmp = input->elts[index];
input->elts[index] = input->elts[last_index - index]; input->elts[index] = input->elts[last_index - index];