aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-05 10:03:10 -0600
committerame <[email protected]>2024-02-05 10:03:10 -0600
commit80f4057a7634fd51832f60ca5a91887d9cb853f2 (patch)
tree575622fbfda31c6566ff32800f8b83dd10184715
parentd108b847196269ee4a4c2b2d31baebfed11eb9b8 (diff)
docs and functionality probably
-rw-r--r--docs/net.md48
-rw-r--r--src/net.c143
-rw-r--r--tests/net.lua14
3 files changed, 136 insertions, 69 deletions
diff --git a/docs/net.md b/docs/net.md
index b75ae46..b129aa1 100644
--- a/docs/net.md
+++ b/docs/net.md
@@ -4,6 +4,7 @@
'takes a function with 1 argument and a integer for a port
+(intentionally styled after expressjs:3)
the function will be ran on initilization, the argument has info on the server and functions to set it up
**
@@ -34,46 +35,51 @@ server:unlock()
closes server
-### server:use
+### server:GET
-'takes a function with 3 paramaters
+'takes a string (the path) and a function to be ran in the background on request
-first and second are res and req as described in server:GET, the third is a function to move to the next point, executes in the order given and can be chained
+the function has 2 arguments, the first (res) contains functions and info about resolving the request,
+the second (req) contains info on the request, the path allows for wildcards, multiple get requests per path is allowed
```lua
-server:use(function(res, req, next)
- if(req['Version'] == "HTTP/1.1") then
- next()
+
+
+server:GET("*", function(res, req, next)
+ if(req['Version'] ~= "HTTP/1.1") then
+ res:deny()
end
end)
-server:GET("/", function(res, req)
+...
+server:GET("/", function(res, req) do
--version will always be 1.1, as per the middleware
+ ...
end)
+...
```
-### server:GET
-
-'takes a string (the path) and a function to be ran in the background on request
+#### res:deny **
-the function has 2 arguments, the first (res) contains functions and info about resolving the request,
-the second (req) contains info on the request
+denies request as if there was no server
```lua
...
-server:GET("/", function(res, req) do
- ...
-end)
+res:deny() --make the client timeout, lol
...
```
-#### res:deny **
+#### res:write
-denies request as if there was no server
+'takes a string
+
+sends the string to the client, constructs a header on first call (whether or not res.header._sent is null)
+(the constructed header can not be changed later on in the request), and sends the string without closing the client
```lua
...
-res:deny() --make the client timeout, lol
+res:write("<h1>hello world</h1>")
+res:write("<h1>good bye world</h1>")
...
```
@@ -81,7 +87,7 @@ res:deny() --make the client timeout, lol
'takes a string
-sends the string to the client
+sends the string to the client, constructs a header then closes client_fd
```lua
...
@@ -89,9 +95,9 @@ res:send("<h1>hello world</h1>")
...
```
-#### res:close **
+#### res:end
-closes connection
+closes connection, sets res.client_fd to -1, any calls that use this value will fail
#### res.header
diff --git a/src/net.c b/src/net.c
index ad48011..058eda9 100644
--- a/src/net.c
+++ b/src/net.c
@@ -217,28 +217,14 @@ void http_code(int code, char* code_det){
}
}
-int l_send(lua_State* L){
- int res_idx = 1;
-
- lua_pushvalue(L, res_idx);
- lua_pushstring(L, "client_fd");
- lua_gettable(L, res_idx);
- int client_fd = luaL_checkinteger(L, -1);
-
- /*
- lua_pushvalue(L, res_idx);
- lua_pushstring(L, "Content");
- lua_gettable(L, res_idx);*/
- char* content = (char*)luaL_checkstring(L, 2);
-
- lua_pushvalue(L, res_idx);
- lua_pushstring(L, "header");
- lua_gettable(L, -2);
- int header = lua_gettop(L);
+void i_write_header(lua_State* L, int header_top, str** _resp, char* content){
+ str* resp;
+ lua_pushvalue(L, header_top);
str* header_vs = str_init("");
lua_pushnil(L);
- for(;lua_next(L, header) != 0;){
+
+ for(;lua_next(L, header_top) != 0;){
char* key = (char*)luaL_checklstring(L, -2, NULL);
if(strcmp(key, "Code") != 0){
str_push(header_vs, key);
@@ -249,18 +235,99 @@ int l_send(lua_State* L){
lua_pop(L, 1);
}
- lua_pushvalue(L, header);
+ lua_pushvalue(L, header_top);
lua_pushstring(L, "Code");
- lua_gettable(L, header);
+ lua_gettable(L, header_top);
int code = luaL_checkinteger(L, -1);
- str* resp;
-
+
char code_det[50] = {0};
http_code(code, code_det);
http_build(&resp, code, code_det, header_vs->c, content);
+
+ str_free(header_vs);
+
+ *_resp = resp;
+}
+int l_write(lua_State* L){
+ int res_idx = 1;
+
+ lua_pushvalue(L, res_idx);
+ lua_pushstring(L, "client_fd");
+ lua_gettable(L, res_idx);
+ int client_fd = luaL_checkinteger(L, -1);
+ if(client_fd <= 0) abort(); // add error message
+
+ char* content = (char*)luaL_checkstring(L, 2);
+
+ lua_pushvalue(L, res_idx);
+ lua_pushstring(L, "header");
+ lua_gettable(L, -2);
+ int header_top = lua_gettop(L);
+
+ lua_pushstring(L, "_sent");
+ lua_gettable(L, -2);
+ str* resp;
+ if(lua_isnil(L, -1)){
+ i_write_header(L, header_top, &resp, content);
+
+ lua_pushvalue(L, header_top);
+ lua_pushstring(L, "_sent");
+ lua_pushinteger(L, 1);
+ lua_settable(L, -3);
+ } else {
+ resp = str_init(content);
+ }
+
send(client_fd, resp->c, resp->len, 0);
+
str_free(resp);
- str_free(header_vs);
+ return 0;
+}
+
+int l_send(lua_State* L){
+ int res_idx = 1;
+
+ lua_pushvalue(L, res_idx);
+ lua_pushstring(L, "client_fd");
+ lua_gettable(L, res_idx);
+ int client_fd = luaL_checkinteger(L, -1);
+ if(client_fd <= 0) abort(); // add error message
+
+ char* content = (char*)luaL_checkstring(L, 2);
+
+ lua_pushvalue(L, res_idx);
+ lua_pushstring(L, "header");
+ lua_gettable(L, -2);
+ int header = lua_gettop(L);
+
+ str* resp;
+ i_write_header(L, header, &resp, content);
+
+ send(client_fd, resp->c, resp->len, 0);
+
+ lua_pushstring(L, "client_fd");
+ lua_pushinteger(L, -1);
+ lua_settable(L, res_idx);
+ closesocket(client_fd);
+
+ str_free(resp);
+ return 0;
+}
+
+int l_end(lua_State* L){
+ int res_idx = 1;
+
+ lua_pushvalue(L, res_idx);
+ lua_pushstring(L, "client_fd");
+ lua_gettable(L, res_idx);
+ int client_fd = luaL_checkinteger(L, -1);
+ if(client_fd <= 0) abort(); // add error message
+
+ lua_pushstring(L, "client_fd");
+ lua_pushinteger(L, -1);
+ lua_settable(L, res_idx);
+ closesocket(client_fd);
+
return 0;
}
@@ -319,6 +386,14 @@ void* handle_client(void *_arg){
lua_pushcfunction(L, l_send);
lua_settable(L, -3);
+ lua_pushstring(L, "write");
+ lua_pushcfunction(L, l_write);
+ lua_settable(L, -3);
+
+ lua_pushstring(L, "end");
+ lua_pushcfunction(L, l_end);
+ lua_settable(L, -3);
+
//values
lua_pushstring(L, "client_fd");
lua_pushinteger(L, client_fd);
@@ -360,21 +435,11 @@ void* handle_client(void *_arg){
lua_call(L, 2, 0);
}
}
- /*
- for(int i = 0; i != awa->len; i++){
- //struct lchar* wowa = awa->cs[i];
-
- luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c);
-
- int func = lua_gettop(L);
-
- lua_pushvalue(L, func); // push function call
- lua_pushvalue(L, res_idx); //push methods related to dealing with the request
- lua_pushvalue(L, req_idx); //push info about the request
+ parray_lclear(owo);
- //call the function
- lua_call(L, 2, 0);
- }*/
+ lua_pushstring(L, "client_fd");
+ lua_gettable(L, res_idx);
+ client_fd = luaL_checkinteger(L, -1);
}
@@ -386,7 +451,7 @@ void* handle_client(void *_arg){
free(table);
}
- closesocket(client_fd);
+ if(client_fd > 0) closesocket(client_fd);
free(args);
free(buffer);
lua_close(L);
diff --git a/tests/net.lua b/tests/net.lua
index fd75cee..443880c 100644
--- a/tests/net.lua
+++ b/tests/net.lua
@@ -11,7 +11,7 @@ aea = 5
--llib.io.pprint(_G.sleep)
llib.io.pprint(llib.net.listen(
function(server)
- --print("wowa")
+ print("wowa")
server:GET("/", function(res, req)
--llib.io.pprint(res)
--llib.io.pprint(res)
@@ -24,14 +24,10 @@ llib.io.pprint(llib.net.listen(
end)
server:GET("/", function(res, req)
- --llib.io.pprint(res)
- --llib.io.pprint(res)
- --print(res.send)
- --res:send("hi");
- --res.Code = 201
- --wwo.sleep(1)
- --wwo.llib.io.pprint(wwo.sleep)
- res:send("<h2>hello world</h2>")
+
+ res:write("test")
+ res:write("test2")
+ res:end()
end)
server:GET("/test", function(res, req)