From 69a06dc3f65c21a5d462b912d492aa38e8c4ad8a Mon Sep 17 00:00:00 2001 From: sternenseemann Date: Tue, 11 Aug 2020 16:41:44 +0200 Subject: refactor(entry): move internal http_errno to cgiutil --- cgiutil.c | 14 ++++++++++++++ cgiutil.h | 11 +++++++++++ entry.c | 20 ++++---------------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/cgiutil.c b/cgiutil.c index bd0f4a5..410ea62 100644 --- a/cgiutil.c +++ b/cgiutil.c @@ -1,3 +1,4 @@ +#include #include #include @@ -29,3 +30,16 @@ char *http_status_line(int status) { return "500 Internal Server Error"; } } + +int http_errno(int err) { + switch(err) { + case EACCES: + return 403; + case ENOENT: + return 404; + case ENOTDIR: + return 404; + default: + return 500; + } +} diff --git a/cgiutil.h b/cgiutil.h index e3aad54..1d388af 100644 --- a/cgiutil.h +++ b/cgiutil.h @@ -40,3 +40,14 @@ void terminate_headers(void); * @return status code and reason phrase as a string. */ char *http_status_line(int status); + +/*! + * @brief Return HTTP error code for given errno + * + * Incomplete mapping of `errno`s to HTTP error codes. + * Defaults to 500. + * + * @param err POSIX errno + * @return HTTP error code + */ +int http_errno(int err); diff --git a/entry.c b/entry.c index 69a1c57..b421042 100644 --- a/entry.c +++ b/entry.c @@ -10,21 +10,9 @@ #include #include "core.h" +#include "cgiutil.h" #include "entry.h" -int http_error(int err) { - switch(err) { - case EACCES: - return 403; - case ENOENT: - return 404; - case ENOTDIR: - return 404; - default: - return 500; - } -} - int make_entry(const char *blog_dir, char *script_name, char *path_info, struct entry *entry) { // TODO: allow subdirectories? // TODO: no status code return? @@ -107,7 +95,7 @@ int make_entry(const char *blog_dir, char *script_name, char *path_info, struct memset(&file_info, 0, sizeof(struct stat)); if(stat(entry->path, &file_info) == -1) { - return http_error(errno); + return http_errno(errno); } int regular_file = (file_info.st_mode & S_IFMT) == S_IFREG; @@ -119,9 +107,9 @@ int make_entry(const char *blog_dir, char *script_name, char *path_info, struct int access = file_info.st_gid == gid || file_info.st_uid == uid; if(!access) { - return http_error(EACCES); + return http_errno(EACCES); } else if(!regular_file) { - return http_error(ENOENT); + return http_errno(ENOENT); } // use POSIX compatible version, since we don't need nanoseconds -- cgit 1.4.1