diff options
| author | ame <[email protected]> | 2025-06-12 13:57:51 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2025-06-12 13:57:51 -0500 |
| commit | fe09d3576ada9918f55431298c5d9a099e690761 (patch) | |
| tree | 32fc93c956154a0cdc6a760f3acca1263d4feb2a | |
| parent | 8e7257aac8b30aaa57577770fd636e784361e35d (diff) | |
rewrite mimetype parsing
| -rw-r--r-- | library/lullaby/net.lua | 1 | ||||
| -rw-r--r-- | src/config.c | 2 | ||||
| -rw-r--r-- | src/crypto.h | 2 | ||||
| -rw-r--r-- | src/io.h | 2 | ||||
| -rw-r--r-- | src/lua.c | 4 | ||||
| -rw-r--r-- | src/lua.h | 2 | ||||
| -rw-r--r-- | src/math.h | 2 | ||||
| -rw-r--r-- | src/net.c | 13 | ||||
| -rw-r--r-- | src/net.h | 5 | ||||
| -rw-r--r-- | src/net/lua.c | 2 | ||||
| -rw-r--r-- | src/net/util.c | 51 | ||||
| -rw-r--r-- | src/reg.c | 11 | ||||
| -rw-r--r-- | src/table.h | 1 | ||||
| -rw-r--r-- | src/test.h | 2 | ||||
| -rw-r--r-- | src/thread.h | 2 |
15 files changed, 94 insertions, 8 deletions
diff --git a/library/lullaby/net.lua b/library/lullaby/net.lua index 57636a4..148d39f 100644 --- a/library/lullaby/net.lua +++ b/library/lullaby/net.lua @@ -2,6 +2,7 @@ local common = require("common") ---@meta ---@class net +---@field mimetypes string filepath for mimetype definitions (defaults to /etc/mime.types). nil to skip local net = {} ---@class server-table 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},
@@ -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}}, @@ -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++;
@@ -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);\
@@ -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}, @@ -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)
@@ -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); @@ -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}, @@ -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},
|
