Add mg_remove_double_dots(), hide mg_http_upload()

This commit is contained in:
Sergey Lyubka committed 2022-06-30 20:27:43 +01:00
1 parent c5751b0bd3
commit 56412193e7
6 files changed
+69 -95

No files matched your search

+2 -22
View File
@@ -621,26 +621,6 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
memcpy(c->send.buf + off - 12, tmp, n); // Set content length
}
static void remove_double_dots(char *s) {
char *p = s;
while (*s != '\0') {
*p++ = *s++;
if (s[-1] == '/' || s[-1] == '\\') {
while (s[0] != '\0') {
if (s[0] == '/' || s[0] == '\\') {
s++;
} else if (s[0] == '.' && s[1] == '.' &&
(s[2] == '/' || s[2] == '\\')) {
s += 2;
} else {
break;
}
}
}
}
*p = '\0';
}
// Resolve requested file into `path` and return its fs->st() result
static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
struct mg_fs *fs, struct mg_str url, struct mg_str dir,
@@ -654,7 +634,7 @@ static int uri_to_path2(struct mg_connection *c, struct mg_http_message *hm,
mg_url_decode(hm->uri.ptr + url.len, hm->uri.len - url.len, path + n,
path_size - n, 0);
path[path_size - 1] = '\0'; // Double-check
remove_double_dots(path);
mg_remove_double_dots(path);
n = strlen(path);
while (n > 1 && path[n - 1] == '/') path[--n] = 0; // Trim trailing slashes
flags = mg_vcmp(&hm->uri, "/") == 0 ? MG_FS_DIR : fs->st(path, NULL, NULL);
@@ -873,7 +853,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
struct mg_fd *fd;
long oft = strtol(offset, NULL, 0);
mg_snprintf(path, sizeof(path), "%s%c%s", dir, MG_DIRSEP, name);
remove_double_dots(path);
mg_remove_double_dots(path);
MG_DEBUG(("%d bytes @ %ld [%s]", (int) hm->body.len, oft, path));
if (oft == 0) fs->rm(path);
if ((fd = mg_fs_open(fs, path, MG_FS_WRITE)) == NULL) {