about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <sternenseemann@systemli.org>2020-08-11 16:41:44 +0200
committersternenseemann <sternenseemann@systemli.org>2020-08-11 16:41:44 +0200
commit69a06dc3f65c21a5d462b912d492aa38e8c4ad8a (patch)
treeb904c26e1a9ae447997a4f4254e8c988c63d0b95
parentf2a3221f415b970a2a1c03f37b061561d0cc4b28 (diff)
refactor(entry): move internal http_errno to cgiutil
-rw-r--r--cgiutil.c14
-rw-r--r--cgiutil.h11
-rw-r--r--entry.c20
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 <errno.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -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 <unistd.h>
 
 #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