diff --git a/mongoose.c b/mongoose.c index a74c1d71..4f28969d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -950,9 +950,10 @@ struct dirent *readdir(DIR *d) { static void printdirentry(struct mg_connection *c, struct mg_http_message *hm, 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); const char *slash = is_dir ? "/" : ""; + const char *es = hm->uri.ptr[hm->uri.len - 1] != '/' ? "/" : ""; if (is_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)); - // mg_escape(file_name, path, sizeof(path)); - // href = mg_url_encode(mg_mk_str(file_name)); - mg_http_printf_chunk(c, - " %s%s" - "%s%s\n", - (int) hm->uri.len, hm->uri.ptr, name, slash, name, slash, - mod, size); - // free((void *) href.p); + mg_printf(c, + " %s%s" + "%s%s\n", + (int) hm->uri.len, hm->uri.ptr, es, name, slash, name, slash, mod, + size); } static void listdir(struct mg_connection *c, struct mg_http_message *hm, 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; DIR *dirp; while (p > dir && *p != '/') *p-- = '\0'; if ((dirp = (opendir(dir))) != NULL) { - mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"); - mg_http_printf_chunk( - c, - "Index of %.*s" - "" - "

Index of %.*s

" - "" - "\n", - (int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr); + size_t off, n; + mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nContent-Length: \r\n"); + off = c->send.len; // Start of body + mg_printf(c, + "Index of %.*s" + "" + "

Index of %.*s

NameModifiedSize

" + "" + "\n", + (int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr); while ((dp = readdir(dirp)) != NULL) { mg_stat_t st; // 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); } closedir(dirp); - mg_http_printf_chunk( - c, - "" - "
NameModifiedSize


Mongoose v.%s
\n", - MG_VERSION); - mg_http_write_chunk(c, "", 0); + mg_printf(c, + "
" + "
Mongoose v.%s
\n", + MG_VERSION); + n = snprintf(tmp, sizeof(tmp), "%lu", (unsigned long) (c->send.len - off)); + memcpy(c->send.buf + off - 10, tmp, n); // Set content length } else { mg_http_reply(c, 400, "", "Cannot open dir"); LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno)); diff --git a/src/http.c b/src/http.c index a2efca6d..b0a7258a 100644 --- a/src/http.c +++ b/src/http.c @@ -530,9 +530,10 @@ struct dirent *readdir(DIR *d) { static void printdirentry(struct mg_connection *c, struct mg_http_message *hm, 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); const char *slash = is_dir ? "/" : ""; + const char *es = hm->uri.ptr[hm->uri.len - 1] != '/' ? "/" : ""; if (is_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)); - // mg_escape(file_name, path, sizeof(path)); - // href = mg_url_encode(mg_mk_str(file_name)); - mg_http_printf_chunk(c, - " %s%s" - "%s%s\n", - (int) hm->uri.len, hm->uri.ptr, name, slash, name, slash, - mod, size); - // free((void *) href.p); + mg_printf(c, + " %s%s" + "%s%s\n", + (int) hm->uri.len, hm->uri.ptr, es, name, slash, name, slash, mod, + size); } static void listdir(struct mg_connection *c, struct mg_http_message *hm, 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; DIR *dirp; while (p > dir && *p != '/') *p-- = '\0'; if ((dirp = (opendir(dir))) != NULL) { - mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"); - mg_http_printf_chunk( - c, - "Index of %.*s" - "" - "

Index of %.*s

" - "" - "\n", - (int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr); + size_t off, n; + mg_printf(c, "%s\r\n", "HTTP/1.1 200 OK\r\nContent-Length: \r\n"); + off = c->send.len; // Start of body + mg_printf(c, + "Index of %.*s" + "" + "

Index of %.*s

NameModifiedSize

" + "" + "\n", + (int) hm->uri.len, hm->uri.ptr, (int) hm->uri.len, hm->uri.ptr); while ((dp = readdir(dirp)) != NULL) { mg_stat_t st; // 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); } closedir(dirp); - mg_http_printf_chunk( - c, - "" - "
NameModifiedSize


Mongoose v.%s
\n", - MG_VERSION); - mg_http_write_chunk(c, "", 0); + mg_printf(c, + "
" + "
Mongoose v.%s
\n", + MG_VERSION); + n = snprintf(tmp, sizeof(tmp), "%lu", (unsigned long) (c->send.len - off)); + memcpy(c->send.buf + off - 10, tmp, n); // Set content length } else { mg_http_reply(c, 400, "", "Cannot open dir"); LOG(LL_DEBUG, ("%lu opendir(%s) -> %d", c->id, dir, errno));