summaryrefslogtreecommitdiff
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
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.
-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,
681 return ret; 681 return ret;
682} 682}
683 683
684static inline int
685errno_to_http_code(void)
686{
687 return errno == ENOENT ? MHD_HTTP_NOT_FOUND : MHD_HTTP_FORBIDDEN;
688}
689
684static int 690static int
685fileserv_handler(void *cls, 691fileserv_handler(void *cls,
686 struct MHD_Connection *conn, 692 struct MHD_Connection *conn,
@@ -718,17 +724,28 @@ fileserv_handler(void *cls,
718 fd = open(file_name, O_RDONLY); 724 fd = open(file_name, O_RDONLY);
719 if (fd == -1) { 725 if (fd == -1) {
720 free(file_name); 726 free(file_name);
721 return http_error(conn, method, url, MHD_HTTP_NOT_FOUND, NULL); 727 return http_error(conn, method, url,
728 errno_to_http_code(),
729 NULL);
722 } 730 }
723 731
724 type = get_file_type(file_name); 732 type = get_file_type(file_name);
725 free(file_name); 733 free(file_name);
726 734
727 if (fstat(fd, &st) || !S_ISREG(st.st_mode)) { 735 if (fstat(fd, &st)) {
728 close(fd); 736 close(fd);
729 return http_error(conn, method, url, MHD_HTTP_NOT_FOUND, NULL); 737 return http_error(conn, method, url,
738 errno_to_http_code(),
739 NULL);
730 } 740 }
731 741
742 if (!S_ISREG(st.st_mode)) {
743 close(fd);
744 return http_error(conn, method, url,
745 MHD_HTTP_FORBIDDEN,
746 NULL);
747 }
748
732 response = MHD_create_response_from_fd64(st.st_size, fd); 749 response = MHD_create_response_from_fd64(st.st_size, fd);
733 if (!response) { 750 if (!response) {
734 close(fd); 751 close(fd);
@@ -871,7 +888,8 @@ main(int argc, char **argv)
871 fileserv_acl, server_addr, 888 fileserv_acl, server_addr,
872 fileserv_handler, NULL, 889 fileserv_handler, NULL,
873 MHD_OPTION_LISTEN_SOCKET, fd, 890 MHD_OPTION_LISTEN_SOCKET, fd,
874 MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger, NULL, 891 MHD_OPTION_EXTERNAL_LOGGER, fileserv_logger,
892 NULL,
875 MHD_OPTION_END); 893 MHD_OPTION_END);
876 /* Unblock only the fatal signals */ 894 /* Unblock only the fatal signals */
877 sigdelset(&sigs, SIGPIPE); 895 sigdelset(&sigs, SIGPIPE);

Return to:

Send suggestions and report system problems to the System administrator.