summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2018-02-09 16:41:11 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2018-02-09 16:41:11 +0200
commitbcd0331d29201fc749fb1edaeede1ca035f13b2d (patch)
treef65af5f02f8fc995d03c20b2d213455dac10fa00 /src
parentd2765b90ce67a26db4ff980842f0a0f3dc37d546 (diff)
downloadfileserv-bcd0331d29201fc749fb1edaeede1ca035f13b2d.tar.gz
fileserv-bcd0331d29201fc749fb1edaeede1ca035f13b2d.tar.bz2
Return correct response code if the file cannot be opened or a directory is requested
* src/fileserv.c (fileserv_handler): Derive the response code from the system errno.
Diffstat (limited to 'src')
-rw-r--r--src/fileserv.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/fileserv.c b/src/fileserv.c
index 4490d73..0749ab4 100644
--- a/src/fileserv.c
+++ b/src/fileserv.c
@@ -681,6 +681,12 @@ http_error(struct MHD_Connection *connection,
return ret;
}
+static inline int
+errno_to_http_code(void)
+{
+ return errno == ENOENT ? MHD_HTTP_NOT_FOUND : MHD_HTTP_FORBIDDEN;
+}
+
static int
fileserv_handler(void *cls,
struct MHD_Connection *conn,
@@ -718,17 +724,28 @@ fileserv_handler(void *cls,
fd = open(file_name, O_RDONLY);
if (fd == -1) {
free(file_name);
- return http_error(conn, method, url, MHD_HTTP_NOT_FOUND, NULL);
+ return http_error(conn, method, url,
+ errno_to_http_code(),
+ NULL);
}
type = get_file_type(file_name);
free(file_name);
- if (fstat(fd, &st) || !S_ISREG(st.st_mode)) {
+ if (fstat(fd, &st)) {
close(fd);
- return http_error(conn, method, url, MHD_HTTP_NOT_FOUND, NULL);
+ return http_error(conn, method, url,
+ errno_to_http_code(),
+ NULL);
}
-
+
+ if (!S_ISREG(st.st_mode)) {
+ close(fd);
+ return http_error(conn, method, url,
+ MHD_HTTP_FORBIDDEN,
+ NULL);
+ }
+
response = MHD_create_response_from_fd64(st.st_size, fd);
if (!response) {
close(fd);
@@ -871,7 +888,8 @@ main(int argc, char **argv)
fileserv_acl, server_addr,
fileserv_handler, NULL,
MHD_OPTION_LISTEN_SOCKET, fd,
- MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger, NULL,
+ MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger,
+ NULL,
MHD_OPTION_END);
/* Unblock only the fatal signals */
sigdelset(&sigs, SIGPIPE);

Return to:

Send suggestions and report system problems to the System administrator.