aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-04-17 23:48:12 -0500
committerame <[email protected]>2024-04-17 23:48:12 -0500
commita0d8a6f3fe365aba0d416f4c33b2c547e4e35820 (patch)
tree193acb90eaaa1dbcec61866ccc2f980ccd471dc9
parenta1838bb037cc1aaa43353a376eb10f37e6293584 (diff)
web server mem leaks fixes
-rw-r--r--.gitignore2
-rw-r--r--readme.md4
-rw-r--r--src/lua.c10
-rw-r--r--src/net.c29
-rw-r--r--tests/net.lua4
5 files changed, 29 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index 4b10e3f..c85e09f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,5 +9,7 @@ test
test2
*.py
+vgcore.*
+
src/*.o
src/*/*.o
diff --git a/readme.md b/readme.md
index 368753e..77d95e9 100644
--- a/readme.md
+++ b/readme.md
@@ -14,9 +14,9 @@ todo:
* <res>:serve()
- * check memory saftey
+ * ~~check memory saftey~~ (*should* be good)
- * memory optimizations
+ * memory optimizations (its getting there)
* settings (parse/read files, etc..)
diff --git a/src/lua.c b/src/lua.c
index 549a250..395481f 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -63,7 +63,7 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){
char aauwu[50] = {0};
sprintf(aauwu, "%p", lua_topointer(src, at2));
- int* sp = malloc(1);
+ //int* sp = malloc(1);
whar = parray_get(seen, aauwu);
if( whar != NULL){
//printf("%s\n",lua_tostring(src, at2 - 1));
@@ -77,8 +77,8 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){
//lua_pushinteger(dest, 55);
int r = luaL_ref(dest, LUA_REGISTRYINDEX);
lua_rawgeti(dest, LUA_REGISTRYINDEX, r);
- *sp = r;
- parray_set(seen, aauwu, sp);
+ //*sp = r;
+ parray_set(seen, aauwu, (void*)&r);
//printf("saved %i\n", *sp);
//for(int i = 0; i != seen->len; i++){
@@ -126,7 +126,7 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){
lua_pushnil(dest);
break;
}
- if(wnull) parray_clear(seen, 1);
+ if(wnull) parray_clear(seen, NONE);
//lua_settop(src, old_top);
_seen = seen;
-} \ No newline at end of file
+}
diff --git a/src/net.c b/src/net.c
index c72f1ef..603c954 100644
--- a/src/net.c
+++ b/src/net.c
@@ -156,17 +156,17 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
if(buffer[oi] == '\n') current->c[current->len - 1] = 0;
parray_set(table, sw->c, (void*)str_init(current->c));
str_clear(current);
+ str_free(sw);
sw = NULL;
key = 1;
}
continue;
} else str_pushl(current, buffer + i, 1);
}
- //printf("'%.*s'\n",header_eof + 5, buffer);
parray_set(table, sw->c, (void*)str_init(current->c));
- //parray_set(table, "Body", (void*)str_initl(buffer + header_eof + 4, buffer_len - header_eof - 4));
str_free(current);
+ if(sw != NULL) str_free(sw);
*_table = table;
return 0;
}
@@ -508,7 +508,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
content.status = state==2?BARRIER_READ:NORMAL;//malloc(sizeof * status); content.status = state==2?BARRIER_READ:NORMAL;
content.dash_count = 0;//malloc(sizeof * dash_count); *dash_count = 0;
- content.current = str_init(content.boundary->c);
+ content.current = str_init("");
content.table_idx = lua_gettop(L);//malloc(sizeof * table_idx); *table_idx = lua_gettop(L);
//parray_set(content, "_table_idx", (void*)(table_idx));
@@ -546,8 +546,6 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
content.status = FILE_HEADER;
buffer+=2;
blen-=i+2;
-
- str_clear(content.current);
content.table_idx = lua_rawlen(L, *files_idx) + 1;
lua_pushinteger(L, content.table_idx);
@@ -586,6 +584,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
luaI_tsets(L, rfiles_idx, content.old->c, content.current->c);
+ str_free(content.old);
content.old = NULL;
str_clear(content.current);
} else if(buffer[i] != '\r' && !(buffer[i] == ' ' && content.current->len == 0)) str_pushl(content.current, buffer + i, 1);
@@ -652,8 +651,11 @@ int l_roll(lua_State* L){
lua_pushstring(L, "Content-Length");
lua_gettable(L, 1);
+ if(lua_type(L, -1) == LUA_TNIL) {
+ lua_pushinteger(L, -1);
+ return 1;
+ }
int content_length = strtol(luaL_checkstring(L, -1), NULL, 10);
-
lua_pushstring(L, "_data");
lua_gettable(L, 1);
struct file_parse* data = (void*)lua_topointer(L, -1);
@@ -663,7 +665,7 @@ int l_roll(lua_State* L){
lua_gettable(L, 1);
int client_fd = luaL_checkinteger(L, -1);
client_fd_errors(client_fd);
-
+
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(client_fd, &rfd);
@@ -714,7 +716,6 @@ int l_roll(lua_State* L){
volatile size_t threads = 0;
void* handle_client(void *_arg){
-time_start(full)
//printf("--\n");
//pthread_mutex_lock(&mutex);
int read_state = 0;
@@ -723,16 +724,19 @@ time_start(full)
char* buffer;
char dummy[2] = {0, 0};
int header_eof;
- //sleep(1);
+ //sleep(1);
//create state for this thread
- lua_State* L = luaL_newstate();
+ lua_State* L = luaL_newstate();
+
luaL_openlibs(L);
pthread_mutex_lock(&mutex);
int old_top = lua_gettop(args->L);
lua_getglobal(args->L, "_G");
+
//time_start(copy)
i_dcopy(args->L, L, NULL);
+
//time_end("copy", copy)
lua_settop(args->L, old_top);
//l_pprint(L);
@@ -809,7 +813,6 @@ time_start(full)
luaI_tsetv(L, req_idx, "cookies", lcookie);
parray_clear(cookie, STR);
- str_free(sC);
parray_remove(table, "Cookie", NONE);
}
@@ -915,6 +918,10 @@ time_start(full)
if((awa = parray_get(file_cont, "_dash_count")) != NULL) free(awa);
parray_clear(file_cont, NONE);*/
+ if(file_cont->boundary != NULL) str_free(file_cont->current);
+ if(file_cont->boundary != NULL) str_free(file_cont->boundary);
+ if(file_cont->boundary_id != NULL) str_free(file_cont->boundary_id);
+
free(file_cont);
}
//printf("end anyways\n");
diff --git a/tests/net.lua b/tests/net.lua
index 7090b89..e482a6f 100644
--- a/tests/net.lua
+++ b/tests/net.lua
@@ -19,7 +19,7 @@ llib.net.listen(
--llib.io.pprint(_G)
llib.io.pprint("online")
_G.server = server
- server:all("/*", function(res, req)
+ server:all("/", function(res, req)
--llib.io.pprint(res)
--llib.io.pprint(res)
--print(res.send)
@@ -71,7 +71,7 @@ llib.net.listen(
res:send(_G.llib.io.readfile("tests/net.lua"))
end)
- server:GET("/test", function(res, req)
+ server:GET("/test55", function(res, req)
res.Code = 403
res:send("<h2>you would never</h2>")
end)