mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Fix #1209
This commit is contained in:
1 parent
d327ee9834
commit
582fde5c6e
2 files changed
+50
-52
No files matched your search
+25
-26
@@ -950,9 +950,10 @@ struct dirent *readdir(DIR *d) {
|
|||||||
|
|
||||||
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
||||||
const char *name, mg_stat_t *stp) {
|
const char *name, mg_stat_t *stp) {
|
||||||
char size[64], mod[64]; //, path[PATH_MAX];
|
char size[64], mod[64];
|
||||||
int is_dir = S_ISDIR(stp->st_mode);
|
int is_dir = S_ISDIR(stp->st_mode);
|
||||||
const char *slash = is_dir ? "/" : "";
|
const char *slash = is_dir ? "/" : "";
|
||||||
|
const char *es = hm->uri.ptr[hm->uri.len - 1] != '/' ? "/" : "";
|
||||||
|
|
||||||
if (is_dir) {
|
if (is_dir) {
|
||||||
snprintf(size, sizeof(size), "%s", "[DIR]");
|
snprintf(size, sizeof(size), "%s", "[DIR]");
|
||||||
@@ -968,34 +969,32 @@ static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
|
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
|
||||||
// mg_escape(file_name, path, sizeof(path));
|
mg_printf(c,
|
||||||
// href = mg_url_encode(mg_mk_str(file_name));
|
" <tr><td><a href=\"%.*s%s%s%s\">%s%s</a></td>"
|
||||||
mg_http_printf_chunk(c,
|
"<td>%s</td><td>%s</td></tr>\n",
|
||||||
" <tr><td><a href=\"%.*s%s%s\">%s%s</a></td>"
|
(int) hm->uri.len, hm->uri.ptr, es, name, slash, name, slash, mod,
|
||||||
"<td>%s</td><td>%s</td></tr>\n",
|
size);
|
||||||
(int) hm->uri.len, hm->uri.ptr, name, slash, name, slash,
|
|
||||||
mod, size);
|
|
||||||
// free((void *) href.p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
||||||
char *dir) {
|
char *dir) {
|
||||||
char path[PATH_MAX + 1], *p = &dir[strlen(dir) - 1];
|
char path[PATH_MAX + 1], *p = &dir[strlen(dir) - 1], tmp[10];
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
DIR *dirp;
|
DIR *dirp;
|
||||||
|
|
||||||
while (p > dir && *p != '/') *p-- = '\0';
|
while (p > dir && *p != '/') *p-- = '\0';
|
||||||
if ((dirp = (opendir(dir))) != NULL) {
|
if ((dirp = (opendir(dir))) != NULL) {
|
||||||
mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n");
|
size_t off, n;
|
||||||
mg_http_printf_chunk(
|
mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nContent-Length: \r\n");
|
||||||
c,
|
off = c->send.len; // Start of body
|
||||||
"<!DOCTYPE html><html><head><title>Index of %.*s</title>"
|
mg_printf(c,
|
||||||
"<style>th,td {text-align: left; padding-right: 1em; "
|
"<!DOCTYPE html><html><head><title>Index of %.*s</title>"
|
||||||
"font-family: monospace; }</style></head>"
|
"<style>th,td {text-align: left; padding-right: 1em; "
|
||||||
"<body><h1>Index of %.*s</h1><table cellpadding=\"0\"><thead>"
|
"font-family: monospace; }</style></head>"
|
||||||
"<tr><th>Name</th><th>Modified</th><th>Size</th></tr>"
|
"<body><h1>Index of %.*s</h1><table cellpadding=\"0\"><thead>"
|
||||||
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
|
"<tr><th>Name</th><th>Modified</th><th>Size</th></tr>"
|
||||||
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
|
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
|
||||||
|
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
|
||||||
while ((dp = readdir(dirp)) != NULL) {
|
while ((dp = readdir(dirp)) != NULL) {
|
||||||
mg_stat_t st;
|
mg_stat_t st;
|
||||||
// Do not show current dir and hidden files
|
// Do not show current dir and hidden files
|
||||||
@@ -1008,12 +1007,12 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
printdirentry(c, hm, dp->d_name, &st);
|
printdirentry(c, hm, dp->d_name, &st);
|
||||||
}
|
}
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
mg_http_printf_chunk(
|
mg_printf(c,
|
||||||
c,
|
"</tbody><tfoot><tr><td colspan=\"3\"><hr></td></tr></tfoot>"
|
||||||
"</tbody><tfoot><tr><td colspan=\"3\"><hr></td></tr></tfoot>"
|
"</table><address>Mongoose v.%s</address></body></html>\n",
|
||||||
"</table><address>Mongoose v.%s</address></body></html>\n",
|
MG_VERSION);
|
||||||
MG_VERSION);
|
n = snprintf(tmp, sizeof(tmp), "%lu", (unsigned long) (c->send.len - off));
|
||||||
mg_http_write_chunk(c, "", 0);
|
memcpy(c->send.buf + off - 10, tmp, n); // Set content length
|
||||||
} else {
|
} else {
|
||||||
mg_http_reply(c, 400, "", "Cannot open dir");
|
mg_http_reply(c, 400, "", "Cannot open dir");
|
||||||
LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno));
|
LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno));
|
||||||
|
|||||||
+25
-26
@@ -530,9 +530,10 @@ struct dirent *readdir(DIR *d) {
|
|||||||
|
|
||||||
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
||||||
const char *name, mg_stat_t *stp) {
|
const char *name, mg_stat_t *stp) {
|
||||||
char size[64], mod[64]; //, path[PATH_MAX];
|
char size[64], mod[64];
|
||||||
int is_dir = S_ISDIR(stp->st_mode);
|
int is_dir = S_ISDIR(stp->st_mode);
|
||||||
const char *slash = is_dir ? "/" : "";
|
const char *slash = is_dir ? "/" : "";
|
||||||
|
const char *es = hm->uri.ptr[hm->uri.len - 1] != '/' ? "/" : "";
|
||||||
|
|
||||||
if (is_dir) {
|
if (is_dir) {
|
||||||
snprintf(size, sizeof(size), "%s", "[DIR]");
|
snprintf(size, sizeof(size), "%s", "[DIR]");
|
||||||
@@ -548,34 +549,32 @@ static void printdirentry(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
|
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
|
||||||
// mg_escape(file_name, path, sizeof(path));
|
mg_printf(c,
|
||||||
// href = mg_url_encode(mg_mk_str(file_name));
|
" <tr><td><a href=\"%.*s%s%s%s\">%s%s</a></td>"
|
||||||
mg_http_printf_chunk(c,
|
"<td>%s</td><td>%s</td></tr>\n",
|
||||||
" <tr><td><a href=\"%.*s%s%s\">%s%s</a></td>"
|
(int) hm->uri.len, hm->uri.ptr, es, name, slash, name, slash, mod,
|
||||||
"<td>%s</td><td>%s</td></tr>\n",
|
size);
|
||||||
(int) hm->uri.len, hm->uri.ptr, name, slash, name, slash,
|
|
||||||
mod, size);
|
|
||||||
// free((void *) href.p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
||||||
char *dir) {
|
char *dir) {
|
||||||
char path[PATH_MAX + 1], *p = &dir[strlen(dir) - 1];
|
char path[PATH_MAX + 1], *p = &dir[strlen(dir) - 1], tmp[10];
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
DIR *dirp;
|
DIR *dirp;
|
||||||
|
|
||||||
while (p > dir && *p != '/') *p-- = '\0';
|
while (p > dir && *p != '/') *p-- = '\0';
|
||||||
if ((dirp = (opendir(dir))) != NULL) {
|
if ((dirp = (opendir(dir))) != NULL) {
|
||||||
mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n");
|
size_t off, n;
|
||||||
mg_http_printf_chunk(
|
mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nContent-Length: \r\n");
|
||||||
c,
|
off = c->send.len; // Start of body
|
||||||
"<!DOCTYPE html><html><head><title>Index of %.*s</title>"
|
mg_printf(c,
|
||||||
"<style>th,td {text-align: left; padding-right: 1em; "
|
"<!DOCTYPE html><html><head><title>Index of %.*s</title>"
|
||||||
"font-family: monospace; }</style></head>"
|
"<style>th,td {text-align: left; padding-right: 1em; "
|
||||||
"<body><h1>Index of %.*s</h1><table cellpadding=\"0\"><thead>"
|
"font-family: monospace; }</style></head>"
|
||||||
"<tr><th>Name</th><th>Modified</th><th>Size</th></tr>"
|
"<body><h1>Index of %.*s</h1><table cellpadding=\"0\"><thead>"
|
||||||
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
|
"<tr><th>Name</th><th>Modified</th><th>Size</th></tr>"
|
||||||
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
|
"<tr><td colspan=\"3\"><hr></td></tr></thead><tbody>\n",
|
||||||
|
(int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr);
|
||||||
while ((dp = readdir(dirp)) != NULL) {
|
while ((dp = readdir(dirp)) != NULL) {
|
||||||
mg_stat_t st;
|
mg_stat_t st;
|
||||||
// Do not show current dir and hidden files
|
// Do not show current dir and hidden files
|
||||||
@@ -588,12 +587,12 @@ static void listdir(struct mg_connection *c, struct mg_http_message *hm,
|
|||||||
printdirentry(c, hm, dp->d_name, &st);
|
printdirentry(c, hm, dp->d_name, &st);
|
||||||
}
|
}
|
||||||
closedir(dirp);
|
closedir(dirp);
|
||||||
mg_http_printf_chunk(
|
mg_printf(c,
|
||||||
c,
|
"</tbody><tfoot><tr><td colspan=\"3\"><hr></td></tr></tfoot>"
|
||||||
"</tbody><tfoot><tr><td colspan=\"3\"><hr></td></tr></tfoot>"
|
"</table><address>Mongoose v.%s</address></body></html>\n",
|
||||||
"</table><address>Mongoose v.%s</address></body></html>\n",
|
MG_VERSION);
|
||||||
MG_VERSION);
|
n = snprintf(tmp, sizeof(tmp), "%lu", (unsigned long) (c->send.len - off));
|
||||||
mg_http_write_chunk(c, "", 0);
|
memcpy(c->send.buf + off - 10, tmp, n); // Set content length
|
||||||
} else {
|
} else {
|
||||||
mg_http_reply(c, 400, "", "Cannot open dir");
|
mg_http_reply(c, 400, "", "Cannot open dir");
|
||||||
LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno));
|
LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno));
|
||||||
|
|||||||
Reference in new issue
Block a user