Update c_simple_http_get_file(...) in static.c

Add option to not fetch mime-type and default to
"application/octet-stream".
This commit is contained in:
Stephen Seo 2024-10-30 13:24:36 +09:00
parent 86bfb5aa91
commit fe0f87614d
3 changed files with 98 additions and 84 deletions

View file

@ -109,15 +109,15 @@ void c_simple_http_cleanup_static_file_info(
}
}
C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir,
const char *path) {
C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
const char *static_dir, const char *path, int_fast8_t ignore_mime_type) {
C_SIMPLE_HTTP_StaticFileInfo file_info;
memset(&file_info, 0, sizeof(C_SIMPLE_HTTP_StaticFileInfo));
if (!static_dir || !path) {
file_info.result = STATIC_FILE_RESULT_InvalidParameter;
return file_info;
} else if (!c_simple_http_is_xdg_mime_available()) {
} else if (!ignore_mime_type && !c_simple_http_is_xdg_mime_available()) {
file_info.result = STATIC_FILE_RESULT_NoXDGMimeAvailable;
return file_info;
}
@ -202,6 +202,9 @@ C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(const char *static_dir,
simple_archiver_helper_cleanup_FILE(&fd);
if (ignore_mime_type) {
file_info.mime_type = strdup("application/octet-stream");
} else {
int from_xdg_mime_pipe[2];
ret = pipe(from_xdg_mime_pipe);
@ -288,6 +291,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;

View file

@ -41,8 +41,10 @@ int_fast8_t c_simple_http_is_xdg_mime_available(void);
void c_simple_http_cleanup_static_file_info(
C_SIMPLE_HTTP_StaticFileInfo *file_info);
/// If ignore_mime_type is non-zero, then mime information will not be fetched.
/// The mime_type string will therefore default to "application/octet-stream".
C_SIMPLE_HTTP_StaticFileInfo c_simple_http_get_file(
const char *static_dir, const char *path);
const char *static_dir, const char *path, int_fast8_t ignore_mime_type);
#endif

View file

@ -984,7 +984,7 @@ int main(int argc, char **argv) {
if (is_xdg_mime_exists) {
CHECK_TRUE(c_simple_http_is_xdg_mime_available());
C_SIMPLE_HTTP_StaticFileInfo info = c_simple_http_get_file(".", argv[0]);
C_SIMPLE_HTTP_StaticFileInfo info = c_simple_http_get_file(".", argv[0], 0);
CHECK_TRUE(info.buf);
CHECK_TRUE(info.buf_size > 0);
CHECK_TRUE(info.mime_type);
@ -994,6 +994,14 @@ int main(int argc, char **argv) {
} else {
CHECK_FALSE(c_simple_http_is_xdg_mime_available());
}
C_SIMPLE_HTTP_StaticFileInfo info = c_simple_http_get_file(".", argv[0], 1);
CHECK_TRUE(info.buf);
CHECK_TRUE(info.buf_size > 0);
CHECK_TRUE(info.mime_type);
CHECK_TRUE(info.result == STATIC_FILE_RESULT_OK);
CHECK_STREQ(info.mime_type, "application/octet-stream");
c_simple_http_cleanup_static_file_info(&info);
}
RETURN()