]> git.seodisparate.com - c_simple_http/commitdiff
Properly handle 404 when checking static-dir
authorStephen Seo <seo.disparate@gmail.com>
Wed, 30 Oct 2024 06:52:27 +0000 (15:52 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 30 Oct 2024 06:52:27 +0000 (15:52 +0900)
src/main.c
src/static.c
src/static.h

index 92b7b5b620fd69e746151e9952bba0964c77e9dc..3beaefe8d66e9d58915ea7c5bc66244ccb2fc4ec 100644 (file)
@@ -450,6 +450,8 @@ int main(int argc, char **argv) {
             response_code = C_SIMPLE_HTTP_Response_500_Internal_Server_Error;
           } else if (file_info.result == STATIC_FILE_RESULT_InvalidParameter) {
             response_code = C_SIMPLE_HTTP_Response_400_Bad_Request;
+          } else if (file_info.result == STATIC_FILE_RESULT_404NotFound) {
+            response_code = C_SIMPLE_HTTP_Response_404_Not_Found;
           } else {
             response_code = C_SIMPLE_HTTP_Response_500_Internal_Server_Error;
           }
index bbcec60a43a6e7338e49f2004afdcbdd117ced28..18c9e9212cdbdfa12c2df511f380dc995a029c86 100644 (file)
@@ -56,7 +56,7 @@ void internal_cleanup_prev_cwd(char **path) {
   if (path && *path) {
     int ret = chdir(*path);
     if (ret != 0) {
-      fprintf(stderr, "WARNING: chdir back to cwd failed! (errno %d)\n", errno);
+      fprintf(stderr, "WARNING chdir back to cwd failed! (errno %d)\n", errno);
     }
     free(*path);
     *path = NULL;
@@ -151,7 +151,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
   int ret = chdir(static_dir);
   if (ret != 0) {
     fprintf(stderr,
-            "ERROR: Failed to chdir into \"%s\"! (errno %d)\n",
+            "ERROR Failed to chdir into \"%s\"! (errno %d)\n",
             static_dir,
             errno);
     file_info.result = STATIC_FILE_RESULT_InternalError;
@@ -168,7 +168,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
       }
     }
     if (path[idx] == 0) {
-      fprintf(stderr, "ERROR: Received invalid path \"%s\"!\n", path);
+      fprintf(stderr, "ERROR Received invalid path \"%s\"!\n", path);
       file_info.result = STATIC_FILE_RESULT_InvalidParameter;
       return file_info;
     }
@@ -176,15 +176,16 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
   fd = fopen(path + idx, "rb");
 
   if (fd == NULL) {
-    fprintf(stderr, "ERROR: Failed to open path \"%s\"!\n", path + idx);
-    file_info.result = STATIC_FILE_RESULT_FileError;
+    fprintf(
+      stderr, "WARNING Failed to open path \"%s\" in static dir!\n", path + idx);
+    file_info.result = STATIC_FILE_RESULT_404NotFound;
     return file_info;
   }
 
   fseek(fd, 0, SEEK_END);
   long long_ret = ftell(fd);
   if (long_ret < 0) {
-    fprintf(stderr, "ERROR: Failed to seek in path fd \"%s\"!\n", path);
+    fprintf(stderr, "ERROR Failed to seek in path fd \"%s\"!\n", path);
     file_info.result = STATIC_FILE_RESULT_FileError;
     return file_info;
   }
@@ -193,7 +194,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
   file_info.buf = malloc(file_info.buf_size);
   size_t size_t_ret = fread(file_info.buf, 1, file_info.buf_size, fd);
   if (size_t_ret != file_info.buf_size) {
-    fprintf(stderr, "ERROR: Failed to read path fd \"%s\"!\n", path);
+    fprintf(stderr, "ERROR Failed to read path fd \"%s\"!\n", path);
     free(file_info.buf);
     file_info.buf_size = 0;
     file_info.result = STATIC_FILE_RESULT_FileError;
index 4a8bbc8943cb56ab4232b674f1c4ab0b1a7b4edf..98deed8e61bf03c1cf1adf539613bcfde87f49b2 100644 (file)
@@ -25,7 +25,8 @@ typedef enum C_SIMPLE_HTTP_StaticFileResult {
   STATIC_FILE_RESULT_FileError,
   STATIC_FILE_RESULT_InvalidParameter,
   STATIC_FILE_RESULT_NoXDGMimeAvailable,
-  STATIC_FILE_RESULT_InternalError
+  STATIC_FILE_RESULT_InternalError,
+  STATIC_FILE_RESULT_404NotFound
 } C_SIMPLE_HTTP_StaticFileResult;
 
 typedef struct C_SIMPLE_HTTP_StaticFileInfo {