From fe09d3576ada9918f55431298c5d9a099e690761 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 12 Jun 2025 13:57:51 -0500 Subject: rewrite mimetype parsing --- src/config.c | 2 +- src/crypto.h | 2 ++ src/io.h | 2 ++ src/lua.c | 4 ++++ src/lua.h | 2 ++ src/math.h | 2 ++ src/net.c | 13 +++++++++++-- src/net.h | 5 +++++ src/net/lua.c | 2 +- src/net/util.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/reg.c | 11 ++++++++--- src/table.h | 1 + src/test.h | 2 ++ src/thread.h | 2 ++ 14 files changed, 93 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index ad21b0c..37f2908 100644 --- a/src/config.c +++ b/src/config.c @@ -86,5 +86,5 @@ int i_config_metatable(lua_State* L, struct config* conf){ lua_pushvalue(L, meta_idx); lua_setmetatable(L, idx); - return 1; + return idx; } diff --git a/src/crypto.h b/src/crypto.h index 2edabe0..324c170 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -112,6 +112,8 @@ int l_##luaname##_update(lua_State* L){\ return 1;\ } +#define clean_lullaby_crypto luaI_nothing + static const luaL_Reg crypto_function_list [] = { {"setpearson",l_setpearson}, {"xxh64",l_xxh64}, {"xxh32",l_xxh32}, {"fletcher8",l_fletcher8}, diff --git a/src/io.h b/src/io.h index b959bbd..c42965c 100644 --- a/src/io.h +++ b/src/io.h @@ -36,6 +36,8 @@ extern int _collapse_all; extern int _collapse_to_memory; extern int _print_meta; +#define clean_lullaby_io luaI_nothing + static struct config io_config[] = { {.name = "filechunksize", .type = c_int, .value = {.c_int = &_file_malloc_chunk}}, {.name = "print_type", .type = c_int, .value = {.c_int = &_print_type}}, diff --git a/src/lua.c b/src/lua.c index ebddd03..a1dc7e9 100644 --- a/src/lua.c +++ b/src/lua.c @@ -10,6 +10,10 @@ static int ii = 0; static int malloc_count = 0; +int luaI_nothing(lua_State* L){ + return 0; +} + void* __malloc_(size_t N){ printf("hi"); malloc_count++; diff --git a/src/lua.h b/src/lua.h index a88f005..2e7aa5e 100644 --- a/src/lua.h +++ b/src/lua.h @@ -37,6 +37,8 @@ typedef int (*stream_read_function)(uint64_t, str**, void**); typedef int (*stream_free_function)(void**); void luaI_newstream(lua_State* L, stream_read_function, stream_free_function, void*); +int luaI_nothing(lua_State*); + //generic macro that takes other macros (see below) #define _tset_b(L, Tidx, K, V, F)\ lua_pushstring(L, K);\ diff --git a/src/math.h b/src/math.h index 3855019..22d9b13 100644 --- a/src/math.h +++ b/src/math.h @@ -4,6 +4,8 @@ int l_lcm(lua_State*); int l_gcd(lua_State*); +#define clean_lullaby_math luaI_nothing + static const luaL_Reg math_function_list [] = { {"lcm",l_lcm}, //{"gcd",l_gcd}, diff --git a/src/net.c b/src/net.c index fc68a91..4bb20d4 100644 --- a/src/net.c +++ b/src/net.c @@ -939,8 +939,10 @@ net_end: } parray_clear(table, STR); } - shutdown(client_fd, 2); - close(client_fd); + if(client_fd != -1){ + shutdown(client_fd, 2); + closesocket(client_fd); + } free(args); free(buffer); lua_close(L); @@ -953,6 +955,13 @@ net_end: return NULL; } +int clean_lullaby_net(lua_State* L){ + if(mime_type != NULL){ + map_clear(mime_type, FREE); + } + return 0; +} + int start_serv(lua_State* L, int port){ parse_mimetypes(); //need these on windows for sockets (stupid) diff --git a/src/net.h b/src/net.h index ac5c6b7..8c36057 100644 --- a/src/net.h +++ b/src/net.h @@ -41,6 +41,8 @@ int start_serv(lua_State* L, int port); // static char* http_codes[600] = {0}; +int clean_lullaby_net(lua_State* L); + static const luaL_Reg net_function_list [] = { {"listen",l_listen}, //{"request",l_request}, @@ -50,6 +52,9 @@ static const luaL_Reg net_function_list [] = { {NULL,NULL} }; +extern char* _mimetypes; + static struct config net_config[] = { + {.name = "mimetypes", .type = c_string, .value = {.c_string = &_mimetypes}}, {.type = c_none} }; diff --git a/src/net/lua.c b/src/net/lua.c index d60c018..ba9a204 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -229,7 +229,7 @@ int l_sendfile(lua_State* L){ } char* ext = strrchr(path, '.'); - if(ext){ + if(ext && mime_type != NULL){ char* content_type = map_get(mime_type, ext + 1); if(content_type) diff --git a/src/net/util.c b/src/net/util.c index 92d3bce..e8e54d0 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -407,15 +407,64 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){ } map_t* mime_type = NULL; +char* _mimetypes = "/etc/mime.types"; + void parse_mimetypes(){ mime_type = map_init(); + + FILE* fp = fopen(_mimetypes, "r"); + if(fp == NULL) return (void)printf("unable to load mimetypes, set llby.net.mimetypes to a proper location, or nil to skip this\n"); + + char* line = NULL; + size_t len = 0; + ssize_t read; + + for(;(read = getline(&line, &len, fp)) != -1;){ + if(line[0] == '#' || line[0] == '\n') continue; + int used = 0; + char* mtype = calloc(1024, sizeof * mtype); + int mtype_len = 0; + int i = 0; + for(; line[i] != ' ' && line[i] != '\t' && i < read; i++){ + mtype[mtype_len] = line[i]; + mtype_len++; + } + + char* type = calloc(512, sizeof * type); + int type_len = 0; + for(; i < read; i++){ + if(line[i] == ' ' || line[i] == '\t' || line[i] == '\n'){ + if(type_len == 0) continue; + char* mtype_c = calloc(1024, sizeof * mtype); + strcpy(mtype_c, mtype); + map_set(&mime_type, type, mtype_c); + used = 1; + type_len = 0; + type = calloc(512, sizeof * type); + } else { + type[type_len] = line[i]; + type_len++; + } + } + free(mtype); + free(type); + } + + fclose(fp); +} + +void _parse_mimetypes(){ + mime_type = map_init(); FILE* fp = fopen(MIMETYPES, "r"); char* buffer = calloc(1024, sizeof * buffer); for(;fgets(buffer, 1024, fp); memset(buffer, 0, 1024)){ int i; for(i = 0; buffer[i] == ' '; i++); - if(buffer[i] == '#') continue; + if(buffer[i] == '#') { + for(; buffer[i] != '\n' && buffer[i] != '\0'; i++); + continue; + } //printf("s: '%s'\n",buffer + i); char* type = calloc(512, sizeof * type); diff --git a/src/reg.c b/src/reg.c index 9393aa4..b0ea080 100644 --- a/src/reg.c +++ b/src/reg.c @@ -12,7 +12,12 @@ int luaopen_lullaby_##name (lua_State* L){\ luaL_register(L, #name, name##_function_list);\ int tidx = lua_gettop(L);\ - i_config_metatable(L, name##_config);\ + int idx = i_config_metatable(L, name##_config);\ + lua_pushvalue(L, idx);\ + lua_getmetatable(L, -1);\ + int midx = lua_gettop(L);\ + luaI_tsetcf(L, midx, "__gc", clean_lullaby_##name);\ + lua_setmetatable(L, idx);\ lua_settop(L, tidx);\ return 1;\ } @@ -29,7 +34,7 @@ open_common(test); lua_pushstring(L, #name);\ luaopen_lullaby_##name(L);\ lua_settable(L, T); - + int luaopen_lullaby(lua_State* L) { lua_newtable(L); @@ -42,7 +47,7 @@ int luaopen_lullaby(lua_State* L) { push(top, net); push(top, thread); push(top, test); - luaI_tsets(L, top, "version", GIT_COMMIT) + luaI_tsets(L, top, "version", GIT_COMMIT); lua_settop(L, top); return 1; diff --git a/src/table.h b/src/table.h index 988eb45..51ef993 100644 --- a/src/table.h +++ b/src/table.h @@ -22,6 +22,7 @@ int l_to_char_array(lua_State*); int l_unpack(lua_State*); int l_split(lua_State*); +#define clean_lullaby_table luaI_nothing static const luaL_Reg table_function_list [] = { {"len",l_len}, {"reverse",l_reverse}, diff --git a/src/test.h b/src/test.h index bb39cc8..8dddb80 100644 --- a/src/test.h +++ b/src/test.h @@ -6,6 +6,8 @@ int l_stack_dump(lua_State*); int l_upvalue_key_table(lua_State* L); int l_stream_test(lua_State* L); +#define clean_lullaby_test luaI_nothing + static const luaL_Reg test_function_list [] = { {"_match", ld_match}, {"stack_dump", l_stack_dump}, diff --git a/src/thread.h b/src/thread.h index 714c499..be9cadd 100644 --- a/src/thread.h +++ b/src/thread.h @@ -9,6 +9,8 @@ int l_testcopy(lua_State*); void lib_thread_clean(); +#define clean_lullaby_thread luaI_nothing + static const luaL_Reg thread_function_list [] = { {"async",l_async}, {"lock",l_tlock}, -- cgit v1.2.3