From 66aedfd65cff3494b70e8072646094479e3bfed8 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 8 Jun 2026 22:43:05 -0500 Subject: net util --- src/lua.c | 82 +++++++++++++++++++--------------------------------------- src/lua.h | 3 +++ src/net.c | 25 +++++++++++------- src/net/lua.c | 24 +++++++++++++++++ src/net/lua.h | 1 + src/net/util.c | 22 +++++++++++++++- src/net/util.h | 1 + src/util.c | 11 ++++++++ src/util.h | 3 +++ 9 files changed, 106 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/lua.c b/src/lua.c index f60fba5..2c9692f 100644 --- a/src/lua.c +++ b/src/lua.c @@ -5,6 +5,7 @@ #include #include "types/str.h" #include +#include "util.h" #include "types/parray.h" static int malloc_count = 0; @@ -13,6 +14,32 @@ int luaI_nothing(lua_State* L){ return 0; } +int luaI_lowercase_index(lua_State* L){ + if(lua_type(L, 2) == LUA_TSTRING){ + str* s = str_init(lua_tostring(L, 2)); + str_lowercase(s); + lua_pushlstring(L, s->c, s->len); + str_free(s); + } + lua_rawget(L, 1); + + return 1; +} + +int luaI_lowercase_newindex(lua_State* L){ + if(lua_type(L, 2) == LUA_TSTRING){ + str* s = str_init(lua_tostring(L, 2)); + str_lowercase(s); + lua_pushlstring(L, s->c, s->len); + str_free(s); + lua_pushvalue(L, 3); + } + + lua_rawset(L, 1); + return 0; +} + + int luaI_iserror(lua_State* L){ if(lua_gettop(L) < 3) return 0; return lua_type(L, -3) == LUA_TNIL && @@ -322,61 +349,6 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ lua_settop(src, old_top); } -void luaI_deepcopy2(lua_State* src, lua_State* dest){ - switch(lua_type(src, -1)){ - case LUA_TNUMBER: - lua_pushinteger(dest, lua_tointeger(src, -1)); - break; - - case LUA_TSTRING:; - size_t size = 0; - const char* str = lua_tolstring(src, -1, &size); - lua_pushlstring(dest, str, size); - break; - - case LUA_TTABLE:; - const void* p = lua_topointer(src, -1); - char* p_string = calloc(80, sizeof * p_string); - sprintf(p_string, "%p", p); - - //lua_getfield(dest, LUA_REGISTRYINDEX, p_string); - lua_pushstring(dest, p_string); - lua_gettable(dest, LUA_REGISTRYINDEX); - if(!lua_isnil(dest, -1)){ - break; - } - - lua_pop(dest, 1); - lua_pushstring(dest, p_string); - lua_newtable(dest); - //lua_setfield(dest, LUA_REGISTRYINDEX, p_string); - //lua_getfield(dest, LUA_REGISTRYINDEX, p_string); - lua_settable(dest, LUA_REGISTRYINDEX); - - lua_pushstring(dest, p_string); - lua_gettable(dest, LUA_REGISTRYINDEX); - - free(p_string); - - int src_top = lua_gettop(src); - int dest_top = lua_gettop(dest); - - lua_pushnil(src); - for(;lua_next(src, src_top) != 0;){ - luaI_deepcopy2(src, dest); - lua_pop(src, 1); - luaI_deepcopy2(src, dest); - - lua_settable(dest, dest_top); - } - break; - - default: - lua_pushinteger(dest, 4); - break; - } -} - int env_table(lua_State* L, int provide_table){ if(provide_table == 0){ lua_newtable(L); diff --git a/src/lua.h b/src/lua.h index da65101..3672730 100644 --- a/src/lua.h +++ b/src/lua.h @@ -27,6 +27,9 @@ enum table_cache { #define GIT_COMMIT "unknown" #endif +int luaI_lowercase_index(lua_State* L); +int luaI_lowercase_newindex(lua_State* L); + enum table_cache table_cache(lua_State* L, char* key, int index); void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags); diff --git a/src/net.c b/src/net.c index 54c3658..df84295 100644 --- a/src/net.c +++ b/src/net.c @@ -718,6 +718,16 @@ void* handle_client(void *_arg){ lua_newtable(L); int res_idx = lua_gettop(L); + lua_newtable(L); + int shared_metatable = lua_gettop(L); + luaI_tsetcf(L, shared_metatable, "__index", luaI_lowercase_index); + luaI_tsetcf(L, shared_metatable, "__newindex", luaI_lowercase_newindex); + + lua_pushvalue(L, shared_metatable); + lua_setmetatable(L, req_idx); + lua_pushvalue(L, shared_metatable); + lua_setmetatable(L, res_idx); + //handle cookies if(sC != NULL){ lua_newtable(L); @@ -779,10 +789,11 @@ void* handle_client(void *_arg){ luaI_tsetcf(L, res_idx, "close", l_close); luaI_tsetcf(L, res_idx, "stop", l_stop); luaI_tsetcf(L, res_idx, "upgrade", l_connection_upgrade); - + luaI_tsetcf(L, res_idx, "error", l_neterror) //values //luaI_tseti(L, res_idx, "client_fd", client_fd); + luaI_tsetb(L, res_idx, "open", 1); luaI_tsetlud(L, res_idx, "_", ctx); luaI_tsets(L, res_idx, "_request", sR->c); @@ -872,14 +883,7 @@ net_end: parray_clear(table, STR); } - if(args->ctx->sock != -1){ - if(args->ctx->ssl != NULL) SSL_shutdown(args->ctx->ssl); - else { - shutdown(args->ctx->sock, 2); - closesocket(args->ctx->sock); - } - } - + net_ctx_close(args->ctx); free(args); free(buffer); lua_close(L); @@ -990,6 +994,7 @@ int start_serv(lua_State* L, int port, parray_t* paths, struct net_server_state* fprintf(stderr, "SSL_new fail\n"); close(*client_fd); free(client_fd); + free(args->ctx); free(args); continue; } @@ -1001,6 +1006,7 @@ int start_serv(lua_State* L, int port, parray_t* paths, struct net_server_state* close(*client_fd); free(client_fd); SSL_free(args->ctx->ssl); + free(args->ctx); free(args); continue; } @@ -1027,7 +1033,6 @@ int start_serv(lua_State* L, int port, parray_t* paths, struct net_server_state* pthread_create(&thread_id, NULL, handle_client, (void*)args); pthread_detach(thread_id); - //handle_client((void*)args); free(client_fd); } } diff --git a/src/net/lua.c b/src/net/lua.c index ffa10c4..025828b 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -85,11 +85,29 @@ int l_send(lua_State* L){ else closesocket(ctx->sock); ctx->sock = -1; + lua_pushboolean(L, 0); + lua_setfield(L, res_idx, "open"); + //printf("%i | %i\n'%s'\n%i\n",client_fd,a,resp->c,resp->len); str_free(resp); return 0; } +int l_neterror(lua_State* L){ + int res_idx = 1; + lua_getfield(L, res_idx, "_"); + struct net_data* ctx = lua_touserdata(L, -1); + + client_fd_errors(ctx->sock); + + net_error(ctx, luaL_checkinteger(L, 2)); + + lua_pushboolean(L, 0); + lua_setfield(L, res_idx, "open"); + + return 0; +} + int l_close(lua_State* L){ int res_idx = 1; @@ -103,6 +121,9 @@ int l_close(lua_State* L){ else closesocket(ctx->sock); ctx->sock = -1; + lua_pushboolean(L, 0); + lua_setfield(L, res_idx, "open"); + return 0; } @@ -267,6 +288,9 @@ int l_sendfile(lua_State* L){ break; } + lua_pushboolean(L, 0); + lua_setfield(L, res_idx, "open"); + free(buffer); fclose(fp); diff --git a/src/net/lua.h b/src/net/lua.h index 492a999..ef4c0e4 100644 --- a/src/net/lua.h +++ b/src/net/lua.h @@ -2,6 +2,7 @@ int l_write(lua_State* L); int l_send(lua_State* L); +int l_neterror(lua_State* L); int l_close(lua_State* L); int l_stop(lua_State* L); int l_roll(lua_State* L); diff --git a/src/net/util.c b/src/net/util.c index d770f5e..ce4fc26 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -1,5 +1,6 @@ #include "common.h" #include "util.h" +#include "ssl.h" #include int64_t recv_header(struct net_data* data, char** _buffer, char** header_eof){ @@ -467,7 +468,11 @@ void _parse_mimetypes(){ int net_error(struct net_data* ctx, int code){ char out[512] = {0}; - sprintf(out, "HTTP/1.1 %i %s\n\n", code, http_code(code)); + str* s = str_init(http_code(code)); + str_lowercase(s); + + sprintf(out, "HTTP/1.1 %i %s\n\n%s", code, http_code(code), s->c); + free(s); net_ctx_write(ctx, out, strlen(out)); return 0; } @@ -513,3 +518,18 @@ int net_ctx_write(struct net_data* data, void* buffer, size_t c){ } return SSL_write(data->ssl, buffer, c); } + +int net_ctx_close(struct net_data* data){ + if(data->sock != -1){ + if(data->ssl != NULL) SSL_shutdown(data->ssl); + else { + shutdown(data->sock, 2); + closesocket(data->sock); + } + } + + data->sock = -1; + free(data); + + return 0; +} diff --git a/src/net/util.h b/src/net/util.h index 8b11e85..602ee53 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -60,3 +60,4 @@ int percent_decode(str* input, str** _output); int net_ctx_read(struct net_data* data, void* buffer, size_t c); int net_ctx_write(struct net_data* data, void* buffer, size_t c); +int net_ctx_close(struct net_data* data); diff --git a/src/util.c b/src/util.c index eacb196..79a3cee 100644 --- a/src/util.c +++ b/src/util.c @@ -50,6 +50,17 @@ int gen_parse(char* inp, int len, parray_t** _table){ return 1; } +int tolower(int i){ + if('A' <= i && i <= 'Z') i += 32; + return i; +} + +void str_lowercase(str* str){ + for(size_t i = 0; i != str->len; i++){ + str->c[i] = tolower(str->c[i]); + } +} + char* strnstr(const char *s1, const char *s2, size_t n) { // simplistic algorithm with O(n2) worst case, stolen from stack overflow size_t i, len; diff --git a/src/util.h b/src/util.h index 588b968..3672c08 100644 --- a/src/util.h +++ b/src/util.h @@ -34,6 +34,9 @@ int gen_parse(char*,int, parray_t**); +int tolower(int i); +void str_lowercase(str* str); + #define p_fatal(M) _p_fatal(M, __LINE__, __FILE__, __func__ ); void _p_fatal(const char*, int, const char*, const char*); void p_error(const char*); -- cgit v1.2.3