From fe168cff249a672aced9cda1439ac7aea343c9af Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 24 Apr 2026 05:06:15 -0500 Subject: http body parsing & request reading --- src/lua.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'src/lua.c') diff --git a/src/lua.c b/src/lua.c index 39972c2..3accdb4 100644 --- a/src/lua.c +++ b/src/lua.c @@ -25,18 +25,23 @@ void __free_(void* p){ return (free)(p); } +int luaI_iserror(lua_State* L){ + if(lua_gettop(L) < 3) return 0; + return lua_type(L, -3) == LUA_TNIL && + lua_type(L, -2) == LUA_TSTRING && + lua_type(L, -1) == LUA_TNUMBER; +} + int _stream_read(lua_State* L){ uint64_t len = 0; if(lua_gettop(L) > 1){ len = lua_tointeger(L, 2); } - lua_pushstring(L, "_read"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_read"); stream_read_function rf = lua_touserdata(L, -1); - lua_pushstring(L, "_state"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_state"); void* state = lua_touserdata(L, -1); str* cont = str_init(""); @@ -55,6 +60,15 @@ int _stream_read(lua_State* L){ return 1; } +int _stream_load(lua_State* L){ + _stream_read(L); + if(luaI_iserror(L)) return 3; + int idx = lua_gettop(L); + luaI_tsetv(L, 1, "txt", idx); + + return 0; +} + int _stream_file(lua_State* L){ const int CHUNK_SIZE = 4096; uint64_t maxlen = 0; @@ -68,12 +82,10 @@ int _stream_file(lua_State* L){ mode = lua_tostring(L, 4); } - lua_pushstring(L, "_read"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_read"); stream_read_function rf = lua_touserdata(L, -1); - lua_pushstring(L, "_state"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_state"); void* state = lua_touserdata(L, -1); const char* filename = lua_tostring(L, 2); @@ -90,6 +102,7 @@ int _stream_file(lua_State* L){ if(ret < 0){ fclose(f); + str_free(cont); luaI_error(L, ret, "read error"); } @@ -102,18 +115,17 @@ int _stream_file(lua_State* L){ break; } } + str_free(cont); fclose(f); return 0; } int _stream_free(lua_State* L){ - lua_pushstring(L, "_free"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_free"); void* rf = lua_touserdata(L, -1); - lua_pushstring(L, "_state"); - lua_gettable(L, 1); + lua_getfield(L, 1, "_state"); void* state = lua_touserdata(L, -1); if(rf != NULL){ @@ -130,9 +142,11 @@ void luaI_newstream(lua_State* L, stream_read_function readf, stream_free_functi luaI_tsetlud(L, tidx, "_free", freef); luaI_tsetlud(L, tidx, "_state", state); luaI_tsetcf(L, tidx, "read", _stream_read); + luaI_tsetcf(L, tidx, "load", _stream_load); luaI_tsetcf(L, tidx, "close", _stream_free); luaI_tsetb(L, tidx, "more", 1); luaI_tsetcf(L, tidx, "file", _stream_file); + luaI_tsets(L, tidx, "txt", "") lua_newtable(L); int midx = lua_gettop(L); -- cgit v1.2.3