2018-02-04 20:27:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "http.h"
|
|
|
|
#include "resp.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareent(const struct dirent **d1, const struct dirent **d2)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
v = ((*d2)->d_type == DT_DIR ? 1 : -1) -
|
|
|
|
((*d1)->d_type == DT_DIR ? 1 : -1);
|
|
|
|
if (v) {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strcmp((*d1)->d_name, (*d2)->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
suffix(int t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case DT_FIFO: return "|";
|
|
|
|
case DT_DIR: return "/";
|
|
|
|
case DT_LNK: return "@";
|
|
|
|
case DT_SOCK: return "=";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
enum status
|
2020-08-22 21:20:00 +00:00
|
|
|
resp_dir(int fd, const struct response *res)
|
2018-02-04 20:27:33 +00:00
|
|
|
{
|
2020-08-22 21:20:00 +00:00
|
|
|
enum status ret;
|
2018-02-04 20:27:33 +00:00
|
|
|
struct dirent **e;
|
|
|
|
size_t i;
|
2020-08-05 11:41:44 +00:00
|
|
|
int dirlen;
|
2020-03-25 13:07:17 +00:00
|
|
|
char esc[PATH_MAX /* > NAME_MAX */ * 6]; /* strlen("&...;") <= 6 */
|
2018-02-04 20:27:33 +00:00
|
|
|
|
|
|
|
/* read directory */
|
2020-08-22 21:20:00 +00:00
|
|
|
if ((dirlen = scandir(res->path, &e, NULL, compareent)) < 0) {
|
|
|
|
return S_FORBIDDEN;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
/* listing */
|
|
|
|
for (i = 0; i < (size_t)dirlen; i++) {
|
|
|
|
/* skip hidden files, "." and ".." */
|
|
|
|
if (e[i]->d_name[0] == '.') {
|
|
|
|
continue;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
/* entry line */
|
|
|
|
html_escape(e[i]->d_name, esc, sizeof(esc));
|
|
|
|
if (dprintf(fd, "<br />\n\t\t<a href=\"%s%s\">%s%s</a>",
|
|
|
|
esc,
|
|
|
|
(e[i]->d_type == DT_DIR) ? "/" : "",
|
|
|
|
esc,
|
|
|
|
suffix(e[i]->d_type)) < 0) {
|
|
|
|
ret = S_REQUEST_TIMEOUT;
|
2018-02-04 20:27:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
/* listing footer */
|
|
|
|
if (dprintf(fd, "\n\t</body>\n</html>\n") < 0) {
|
|
|
|
ret = S_REQUEST_TIMEOUT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:27:33 +00:00
|
|
|
cleanup:
|
|
|
|
while (dirlen--) {
|
|
|
|
free(e[dirlen]);
|
|
|
|
}
|
|
|
|
free(e);
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
return ret;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum status
|
2020-08-22 21:20:00 +00:00
|
|
|
resp_file(int fd, const struct response *res)
|
2018-02-04 20:27:33 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2020-08-22 21:20:00 +00:00
|
|
|
enum status ret = 0;
|
2018-02-04 20:27:33 +00:00
|
|
|
ssize_t bread, bwritten;
|
2020-08-05 16:59:55 +00:00
|
|
|
size_t remaining;
|
2020-08-05 11:41:44 +00:00
|
|
|
static char buf[BUFSIZ], *p;
|
2018-02-04 20:27:33 +00:00
|
|
|
|
|
|
|
/* open file */
|
2020-08-22 21:20:00 +00:00
|
|
|
if (!(fp = fopen(res->path, "r"))) {
|
|
|
|
ret = S_FORBIDDEN;
|
2018-02-04 20:27:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek to lower bound */
|
2020-08-22 21:20:00 +00:00
|
|
|
if (fseek(fp, res->file.lower, SEEK_SET)) {
|
|
|
|
ret = S_INTERNAL_SERVER_ERROR;
|
2018-02-04 20:27:33 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
/* write data until upper bound is hit */
|
|
|
|
remaining = res->file.upper - res->file.lower + 1;
|
2018-02-04 20:27:33 +00:00
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
while ((bread = fread(buf, 1, MIN(sizeof(buf),
|
|
|
|
remaining), fp))) {
|
|
|
|
if (bread < 0) {
|
|
|
|
ret = S_INTERNAL_SERVER_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
remaining -= bread;
|
|
|
|
p = buf;
|
|
|
|
while (bread > 0) {
|
|
|
|
bwritten = write(fd, p, bread);
|
|
|
|
if (bwritten <= 0) {
|
|
|
|
ret = S_REQUEST_TIMEOUT;
|
2020-04-22 18:46:30 +00:00
|
|
|
goto cleanup;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
2020-08-22 21:20:00 +00:00
|
|
|
bread -= bwritten;
|
|
|
|
p += bwritten;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-08-22 21:20:00 +00:00
|
|
|
return ret;
|
2018-02-04 20:27:33 +00:00
|
|
|
}
|