mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-01 19:17:48 +08:00
Fixed put_dir()
This commit is contained in:
parent
4d85cc6c76
commit
0d885add0e
35
mongoose.c
35
mongoose.c
@ -2348,35 +2348,21 @@ static int put_dir(const char *path) {
|
|||||||
char buf[MAX_PATH_SIZE];
|
char buf[MAX_PATH_SIZE];
|
||||||
const char *s, *p;
|
const char *s, *p;
|
||||||
file_stat_t st;
|
file_stat_t st;
|
||||||
int len, res = 1;
|
|
||||||
|
|
||||||
for (s = p = path + 2; (p = strchr(s, '/')) != NULL; s = ++p) {
|
// Create intermediate directories if they do not exist
|
||||||
len = p - path;
|
for (s = p = path + 1; (p = strchr(s, '/')) != NULL; s = ++p) {
|
||||||
if (len >= (int) sizeof(buf)) {
|
if (p - path >= (int) sizeof(buf)) return -1; // Buffer overflow
|
||||||
res = -1;
|
memcpy(buf, path, p - path);
|
||||||
break;
|
buf[p - path] = '\0';
|
||||||
}
|
if (stat(buf, &st) != 0 && mkdir(buf, 0755) != 0) return -1;
|
||||||
mg_strlcpy(buf, path, len);
|
if (p[1] == '\0') return 0; // Path is a directory itself
|
||||||
memcpy(buf, path, len);
|
|
||||||
|
|
||||||
// Try to create intermediate directory
|
|
||||||
if (stat(buf, &st) != 0 && mkdir(buf, 0755) != 0) {
|
|
||||||
res = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is path itself a directory?
|
|
||||||
if (p[1] == '\0') {
|
|
||||||
res = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_put(struct connection *conn, const char *path) {
|
static void handle_put(struct connection *conn, const char *path) {
|
||||||
file_stat_t st;
|
file_stat_t st;
|
||||||
char buf[100];
|
|
||||||
const char *range, *cl_hdr = mg_get_header(&conn->mg_conn, "Content-Length");
|
const char *range, *cl_hdr = mg_get_header(&conn->mg_conn, "Content-Length");
|
||||||
int64_t r1, r2;
|
int64_t r1, r2;
|
||||||
int rc;
|
int rc;
|
||||||
@ -2403,9 +2389,8 @@ static void handle_put(struct connection *conn, const char *path) {
|
|||||||
lseek(conn->endpoint.fd, r1, SEEK_SET);
|
lseek(conn->endpoint.fd, r1, SEEK_SET);
|
||||||
conn->cl = r2 > r1 ? r2 - r1 + 1: conn->cl - r1;
|
conn->cl = r2 > r1 ? r2 - r1 + 1: conn->cl - r1;
|
||||||
}
|
}
|
||||||
mg_snprintf(buf, sizeof(buf), "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n",
|
mg_printf(&conn->mg_conn, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n",
|
||||||
conn->mg_conn.status_code);
|
conn->mg_conn.status_code);
|
||||||
spool(&conn->remote_iobuf, buf, strlen(buf));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user