diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2018-02-09 16:41:11 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2018-02-09 16:41:11 +0200 |
commit | bcd0331d29201fc749fb1edaeede1ca035f13b2d (patch) | |
tree | f65af5f02f8fc995d03c20b2d213455dac10fa00 | |
parent | d2765b90ce67a26db4ff980842f0a0f3dc37d546 (diff) | |
download | fileserv-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.
-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 @@ -683,2 +683,8 @@ http_error(struct MHD_Connection *connection, +static inline int +errno_to_http_code(void) +{ + return errno == ENOENT ? MHD_HTTP_NOT_FOUND : MHD_HTTP_FORBIDDEN; +} + static int @@ -720,3 +726,5 @@ fileserv_handler(void *cls, 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); } @@ -726,7 +734,16 @@ fileserv_handler(void *cls, - 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); @@ -873,3 +890,4 @@ main(int argc, char **argv) MHD_OPTION_LISTEN_SOCKET, fd, - MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger, NULL, + MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger, + NULL, MHD_OPTION_END); |