aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2025-09-02 00:07:22 -0500
committerame <[email protected]>2025-09-02 00:07:22 -0500
commit6e6e948a553cc062b439f349c0b545df0223d9a1 (patch)
tree4a5d67acbe5636f47fee1471a94bc7490de1d0cb
parent70dcbc67382084d924d353f9c741cd8c0e46cb0f (diff)
parse net path and query
-rw-r--r--library/lullaby/net.lua8
-rw-r--r--src/net.c67
2 files changed, 73 insertions, 2 deletions
diff --git a/library/lullaby/net.lua b/library/lullaby/net.lua
index 9fb1dc2..416851f 100644
--- a/library/lullaby/net.lua
+++ b/library/lullaby/net.lua
@@ -60,12 +60,20 @@ req_table.ip = 0
---@type string
req_table.Body = ""
+---@type string
+req_table.path = ""
+
+---@type string
+req_table.rawpath = ""
+
---@type any|nil
req_table.files = {}
---@type any|nil
req_table.cookies = {}
+---@type any|nil
+req_table.query = {}
---@param T server-table
---@param route string
diff --git a/src/net.c b/src/net.c
index d68da2c..49eabd7 100644
--- a/src/net.c
+++ b/src/net.c
@@ -2,6 +2,7 @@
#include "net/util.h"
#include "net/lua.h"
#include "net/luai.h"
+#include "types/str.h"
#include <fcntl.h>
@@ -652,6 +653,50 @@ int _request(lua_State* L, struct request_state* state){
return 1;
}
+struct net_path_t {
+ str* path;
+ parray_t* query;
+};
+
+void path_parse(struct net_path_t* path, str* raw){
+ path->path = str_init("");
+ path->query = parray_init();
+
+ str* query_key = str_init("");
+ str* query_value = str_init("");
+
+ str** reading = &path->path;
+
+ for(int i = 0; i <= raw->len; i++){
+ if(raw->len - i > 1){
+ switch(raw->c[i]){
+ case '&':
+ parray_set(path->query, query_key->c, query_value);
+ str_clear(query_key);
+ query_value = str_init("");
+ //dont want to break here
+ case '?':
+ reading = &query_key;
+ i++;
+ break;
+ case '=':
+ reading = &query_value;
+ i++;
+ break;
+ }
+ }
+
+ str_pushl(*reading, raw->c + i, 1);
+ }
+
+ if(*reading == query_value){
+ parray_set(path->query, query_key->c, query_value);
+ } else {
+ str_free(query_value);
+ }
+ str_free(query_key);
+}
+
#define max_uri_size 2048
_Atomic size_t threads = 0;
@@ -693,12 +738,15 @@ void* handle_client(void *_arg){
sprintf(portc, "%i", args->port);
str* aa = str_init(portc);
+ struct net_path_t parsed_path;
+ path_parse(&parsed_path, path);
+
str* decoded_path;
- int decoded_err = percent_decode(path, &decoded_path);
+ int decoded_err = percent_decode(parsed_path.path, &decoded_path);
larray_t* params = NULL;
parray_t* v = NULL;
- if(decoded_err == 1){
+ if(decoded_err == 1 || paths == NULL){
net_error(client_fd, 400);
} else {
str_push(aa, decoded_path->c);
@@ -734,6 +782,16 @@ void* handle_client(void *_arg){
parray_remove(table, "Cookie", STR);
}
+ if(parsed_path.query->len != 0){
+ lua_newtable(L);
+ int lquery = lua_gettop(L);
+ for(int i = 0; i != parsed_path.query->len; i++){
+ luaI_tsetsl(L, lquery, parsed_path.query->P[i].key->c, ((str*)parsed_path.query->P[i].value)->c, ((str*)parsed_path.query->P[i].value)->len);
+ }
+
+ luaI_tsetv(L, req_idx, "query", lquery);
+ }
+
lua_pushlightuserdata(L, file_cont);
int ld = lua_gettop(L);
@@ -747,6 +805,8 @@ void* handle_client(void *_arg){
}
luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr));
+ luaI_tsets(L, req_idx, "path", parsed_path.path->c);
+ luaI_tsets(L, req_idx, "rawpath", path->c);
if(bite == -1){
client_fd = -2;
@@ -837,6 +897,9 @@ net_end:
}
+ str_free(parsed_path.path);
+ parray_clear(parsed_path.query, STR);
+
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);