From fe33c0e979ebfc41c3eb9f561589f0d5d8810aaf Mon Sep 17 00:00:00 2001 From: amelia squires Date: Sun, 29 Sep 2024 02:49:05 -0500 Subject: docs n stuff --- docs/net.md | 31 ++++++++++++++++++++++++++++ src/lua.c | 3 +++ src/net.c | 1 + src/net/common.h | 3 +++ src/net/lua.c | 8 ++++++++ src/net/util.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/net/util.h | 3 +++ src/thread.c | 15 ++++++++++---- src/types/str.c | 2 +- tests/buffer.lua | 9 ++++++++ tests/net.lua | 2 +- tests/t.lua | 16 ++++++++++++--- 12 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 tests/buffer.lua diff --git a/docs/net.md b/docs/net.md index 9b85c18..0807c72 100644 --- a/docs/net.md +++ b/docs/net.md @@ -12,12 +12,41 @@ right now everything within a server:GET function is partially global, it can re it can not read/copy local variables or modify globals ** +allows a third optional argument that offers some other options in a table format + +|name|default value|extra info| +|--|--|--| +|mime.types.file**|/etc/mime.types|formated similarly to [this](https://wiki.debian.org/MIME/etc/mime.types)| +|max.connections**|64|maximum number of connections that can be opened at the same time, will respond with error(503)| +|max.header.size**|1<<20 (1048576)|max header size before refusing connection with error(431)| +|max.uri.size**|idk yet|maximum uri size before refusing request with error(414)| +|max.request.timeout**|idk yet|maximum time server will wait for request to be parsed| + + +the server will send these codes for these reasons +|code|cause| +|--|--| +|503**|too many current requests, more than max.connections| +|500**|anytime a server route crashes| +|431**|header is larger than max header size, more than max.header.size| +|414**|request uri is longer than max.uri.size| +|408**|request took too longer than max.request.timeout| +|404**|request has no defined route| + +(more to come?**) + ```lua llib.net.listen(function(server) ... end, 80) ``` +```lua +llib.net.listen(function(server) + ... +end, 80, {["mime.types.file"] = "/etc/mime.types"}) +``` + ### server:lock server:unlock continues on the current thread, but pauses all other threads at that point @@ -127,6 +156,8 @@ res.header["test"] = "wowa" 'takes one string, which is a path that will be served, must be a file +res.header["Content-Type"] is set automatically depending on the file extention, using /etc/mime.types, or whatever option was supplied to listen (see listen options) + ```lua ... res:sendfile("./html/index.html") diff --git a/src/lua.c b/src/lua.c index 225dfbf..d6a5523 100644 --- a/src/lua.c +++ b/src/lua.c @@ -139,6 +139,9 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ modi = 1; lua_pushlightuserdata(dest, lua_touserdata(src, -1)); break; + case LUA_TTHREAD: + lua_pushnil(dest); + break; default: printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type); abort(); diff --git a/src/net.c b/src/net.c index 66f2a3d..88e6690 100644 --- a/src/net.c +++ b/src/net.c @@ -265,6 +265,7 @@ net_end: } int start_serv(lua_State* L, int port){ + parse_mimetypes(); //need these on windows for sockets (stupid) #ifdef _WIN32 WSADATA Data; diff --git a/src/net/common.h b/src/net/common.h index 4120734..6b1a25a 100644 --- a/src/net/common.h +++ b/src/net/common.h @@ -19,6 +19,7 @@ #include "../table.h" #include "../types/str.h" #include "../types/parray.h" +#include "../types/map.h" #define max_con 200 //2^42 @@ -62,4 +63,6 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t con_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; +extern map_t* mime_type; + #endif diff --git a/src/net/lua.c b/src/net/lua.c index dd73db2..df2213b 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -216,6 +216,14 @@ int l_sendfile(lua_State* L){ p_fatal("missing permissions"); } + char* ext = strrchr(path, '.'); + if(ext){ + char* content_type = map_get(mime_type, ext + 1); + + if(content_type) + {luaI_tsets(L, header, "Content-Type", content_type);} + } + str* r; i_write_header(L, header, &r, "", 0); send(client_fd, r->c, r->len, 0); diff --git a/src/net/util.c b/src/net/util.c index 2dd9a7f..44a525b 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -356,3 +356,65 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){ *_params = params; return out; } + +map_t* mime_type = NULL; +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; + + //printf("s: '%s'\n",buffer + i); + char* type = calloc(512, sizeof * type); + int type_len = 0; + for(; buffer[i + type_len] != ' ' && buffer[i + type_len] != '\t'; type_len++){ + //printf("%c", buffer[i + type_len]); + type[type_len] = buffer[i + type_len]; + if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n') break; + } + type[type_len] = '\0'; + + //check if the type has an associated file type + if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n'){ + free(type); + continue; + } + type = realloc(type, (type_len + 1) * sizeof * type); + + + int file_type_len = 0; + char* file_type = calloc(512, sizeof * file_type); + i += type_len; + + for(;buffer[i] == ' ' || buffer[i] == '\t'; i++); + + int used = 0; + for(;;){ + if(buffer[i + file_type_len] == ' ' || buffer[i + file_type_len] == '\n' || buffer[i + file_type_len] == '\0'){ + used = 1; + file_type[file_type_len] = '\0'; + file_type = realloc(file_type, (file_type_len + 1) * sizeof * file_type); + //printf("set %s to %s\n",file_type, type); + map_set(&mime_type, file_type, type); + file_type = calloc(512, sizeof * file_type); + if(buffer[i + file_type_len] != ' ') break; + i += file_type_len + 1; + file_type_len = 0; + //printf("finish\n"); + } else { + file_type[file_type_len] = buffer[i + file_type_len]; + file_type_len++; + } + } + free(file_type); + + //printf("e: '%s'\n", type); + if(!used)free(type); + } + //printf("done\n"); + +} diff --git a/src/net/util.h b/src/net/util.h index fbe73f8..e592420 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -1,5 +1,7 @@ #include "common.h" #include "../types/larray.h" + +#define MIMETYPES "/etc/mime.types" /** * @brief calls recv into buffer until everything is read * @@ -50,3 +52,4 @@ parray_t* route_match(parray_t* paths, char* path, larray_t** params); int match_param(char* path, char* match, parray_t* arr); +void parse_mimetypes(); diff --git a/src/thread.c b/src/thread.c index 033da33..db8e54e 100644 --- a/src/thread.c +++ b/src/thread.c @@ -269,6 +269,7 @@ int _buffer_mod(lua_State* L){ int l_buffer_index(lua_State* L){ uint64_t len, hash; + struct thread_buffer *buffer = lua_touserdata(L, 1); const char* str = luaL_tolstring(L, 2, &len); hash = fnv_1((uint8_t*)str, len, v_1); @@ -284,7 +285,15 @@ int l_buffer_index(lua_State* L){ lua_pushcfunction(L, _buffer_mod); break; default: - lua_pushnil(L); + lua_pushstring(buffer->L, str); + lua_gettable(buffer->L, 1); + if(lua_isnil(buffer->L, -1)){ + lua_pushnil(L); + return 1; + } + + luaI_deepcopy(buffer->L, L, SKIP_GC); + lua_pop(buffer->L, 1); break; } return 1; @@ -349,7 +358,6 @@ return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); } int l_buffer_gc(lua_State* L){ - printf("gc\n"); struct thread_buffer *buffer = lua_touserdata(L, 1); pthread_mutex_lock(&*buffer->lock); pthread_mutex_destroy(&*buffer->lock); @@ -387,9 +395,8 @@ int l_buffer(lua_State* L){ void _lua_getfenv(lua_State* L){ } -int l_testcopy(lua_State* L){ - +int l_testcopy(lua_State* L){ lua_State* temp = luaL_newstate(); luaI_deepcopy(L, temp, SKIP_GC); lua_close(temp); diff --git a/src/types/str.c b/src/types/str.c index 0378285..4b257cb 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -12,7 +12,7 @@ str* str_initl(const char* init, size_t len){ if(s->c == NULL) p_fatal("failed to allocate string\n"); s->len = len ; - memcpy(s->c, init, len + 1); + memcpy(s->c, init, (len + 1) * sizeof * init); return s; } diff --git a/tests/buffer.lua b/tests/buffer.lua new file mode 100644 index 0000000..3309aed --- /dev/null +++ b/tests/buffer.lua @@ -0,0 +1,9 @@ +llby = require "lullaby" + +b = llby.thread.buffer(llby.crypto.md5()) + +print(b.update(b:get(), "meow"):final()) +print(b.update(b:get(), "meow"):final()) +b:mod(function(this) return this:update("meow") end) +print(b.update(b:get(), "meow"):final()) + diff --git a/tests/net.lua b/tests/net.lua index d1b5369..e389fb0 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -58,7 +58,7 @@ net.listen( end) server:GET("/aa", function(res, req) - res.header["Content-Type"] = "text/plain" + --res.header["Content-Type"] = "text/plain" res:sendfile("readme.md") end) diff --git a/tests/t.lua b/tests/t.lua index 9c71c61..09ee15d 100644 --- a/tests/t.lua +++ b/tests/t.lua @@ -1,5 +1,15 @@ a = require "lullaby" -o = a.crypto.sha224() ---o:update("me") ---print(o:final()) +b = coroutine.create(function() + os.execute("sleep 2") + print("co") +end) + +c = a.thread.async(function(res, req) + coroutine.resume(b) +end) + +os.execute("sleep 0.5") +print("owo") + +c:await() -- cgit v1.2.3