diff options
Diffstat (limited to 'src/fileserv.c')
-rw-r--r-- | src/fileserv.c | 28 |
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); |