From 86bfb5aa91bc623043aae56de6cee29632a16324 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 29 Oct 2024 14:56:52 +0900 Subject: [PATCH] Add enum for result info when getting static file --- src/static.c | 18 +++++++++++++++++- src/static.h | 9 +++++++++ src/test.c | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/static.c b/src/static.c index 43f33bf..d23fbd8 100644 --- a/src/static.c +++ b/src/static.c @@ -114,7 +114,11 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, C_SIMPLE_HTTP_StaticFileInfo file_info; memset(&file_info, 0, sizeof(C_SIMPLE_HTTP_StaticFileInfo)); - if (!static_dir || !path || !c_simple_http_is_xdg_mime_available()) { + if (!static_dir || !path) { + file_info.result = STATIC_FILE_RESULT_InvalidParameter; + return file_info; + } else if (!c_simple_http_is_xdg_mime_available()) { + file_info.result = STATIC_FILE_RESULT_NoXDGMimeAvailable; return file_info; } @@ -128,10 +132,12 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, buf_size *= 2; buf = realloc(buf, buf_size); if (buf == NULL) { + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } } else { free(buf); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } } else { @@ -148,6 +154,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, "ERROR: Failed to chdir into \"%s\"! (errno %d)\n", static_dir, errno); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } @@ -162,6 +169,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, } if (path[idx] == 0) { fprintf(stderr, "ERROR: Received invalid path \"%s\"!\n", path); + file_info.result = STATIC_FILE_RESULT_InvalidParameter; return file_info; } } @@ -169,6 +177,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, if (fd == NULL) { fprintf(stderr, "ERROR: Failed to open path \"%s\"!\n", path + idx); + file_info.result = STATIC_FILE_RESULT_FileError; return file_info; } @@ -176,6 +185,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, long long_ret = ftell(fd); if (long_ret < 0) { fprintf(stderr, "ERROR: Failed to seek in path fd \"%s\"!\n", path); + file_info.result = STATIC_FILE_RESULT_FileError; return file_info; } fseek(fd, 0, SEEK_SET); @@ -186,6 +196,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, 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; return file_info; } @@ -204,6 +215,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, c_simple_http_cleanup_static_file_info(&file_info); close(from_xdg_mime_pipe[1]); close(from_xdg_mime_pipe[0]); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } @@ -234,6 +246,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, c_simple_http_cleanup_static_file_info(&file_info); close(from_xdg_mime_pipe[1]); close(from_xdg_mime_pipe[0]); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } @@ -253,6 +266,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, if (buf == NULL) { c_simple_http_cleanup_static_file_info(&file_info); close(from_xdg_mime_pipe[0]); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } } @@ -264,6 +278,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, if (ret != 0) { c_simple_http_cleanup_static_file_info(&file_info); + file_info.result = STATIC_FILE_RESULT_InternalError; return file_info; } @@ -274,6 +289,7 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir, file_info.mime_type = buf; + file_info.result = STATIC_FILE_RESULT_OK; return file_info; } diff --git a/src/static.h b/src/static.h index 30379b0..82121ee 100644 --- a/src/static.h +++ b/src/static.h @@ -20,10 +20,19 @@ // Standard library includes. #include +typedef enum C_SIMPLE_HTTP_StaticFileResult { + STATIC_FILE_RESULT_OK, + STATIC_FILE_RESULT_FileError, + STATIC_FILE_RESULT_InvalidParameter, + STATIC_FILE_RESULT_NoXDGMimeAvailable, + STATIC_FILE_RESULT_InternalError +} C_SIMPLE_HTTP_StaticFileResult; + typedef struct C_SIMPLE_HTTP_StaticFileInfo { char *buf; uint64_t buf_size; char *mime_type; + C_SIMPLE_HTTP_StaticFileResult result; } C_SIMPLE_HTTP_StaticFileInfo; /// Returns non-zero if "xdg_mime" is available. diff --git a/src/test.c b/src/test.c index fd7731c..30cefcc 100644 --- a/src/test.c +++ b/src/test.c @@ -988,6 +988,7 @@ int main(int argc, char **argv) { CHECK_TRUE(info.buf); CHECK_TRUE(info.buf_size > 0); CHECK_TRUE(info.mime_type); + CHECK_TRUE(info.result == STATIC_FILE_RESULT_OK); printf("unit test mime type is: %s\n", info.mime_type); c_simple_http_cleanup_static_file_info(&info); } else {