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
@@ -678,12 +678,18 @@ http_error(struct MHD_Connection *connection,
MHD_RESPMEM_PERSISTENT);
ret = MHD_queue_response(connection, status, response);
MHD_destroy_response(response);
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,
const char *url, const char *method,
const char *version,
const char *upload_data, size_t *upload_data_size,
@@ -715,23 +721,34 @@ fileserv_handler(void *cls,
if (status != MHD_HTTP_OK)
return http_error(conn, method, url, status, NULL);
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);
return MHD_NO;
}
@@ -868,13 +885,14 @@ main(int argc, char **argv)
mhd = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD
| MHD_USE_ERROR_LOG, 0,
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);
pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
/* Wait for signal to arrive */
sigwait(&sigs, &c);

Return to:

Send suggestions and report system problems to the System administrator.