diff options
| author | amelia squires <[email protected]> | 2024-08-29 00:10:05 -0500 |
|---|---|---|
| committer | amelia squires <[email protected]> | 2024-08-29 00:10:05 -0500 |
| commit | 4f0b3173cae639de5874afaf2df6ab113c7277fd (patch) | |
| tree | 0ebd6d7755e5827516041e00f1e6df4285618b1a | |
| parent | d0c7c5b0dd9d20bfc323dc10c67f9de12d58c343 (diff) | |
should be working + tests
| -rw-r--r-- | src/net/util.c | 81 | ||||
| -rw-r--r-- | src/net/util.h | 3 | ||||
| -rw-r--r-- | src/reg.c | 3 | ||||
| -rw-r--r-- | src/test.c | 25 | ||||
| -rw-r--r-- | src/test.h | 9 | ||||
| -rw-r--r-- | tests/match.lua | 37 | ||||
| -rw-r--r-- | tests/net.lua | 13 |
7 files changed, 154 insertions, 17 deletions
diff --git a/src/net/util.c b/src/net/util.c index bc2c6bb..c25ac39 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -240,37 +240,84 @@ int content_disposition(str* src, parray_t** _dest){ return 1; } -int match_param(char* path, char* match){ - int pi, mi = pi = 0; +int match_param(char* path, char* match, parray_t* arr){ + int pi, index, imatch, start, mi; + mi = pi = imatch = start = 0; + index = -1; + int step = 0; //0 increment both - //1 move match to find '/' or '\0' - //2 move path to find '}' - for(; path[pi] != '\0' && match[mi] != '\0';){ + //1 move path to find '}' + //2 move match to find '/' or '\0' + char* name; + + for(; /*(path[pi] != '\0' || step == 2) && */(match[mi] != '\0' || step == 1);){ if(step == 0){ if(path[pi] == '{'){ + step = 1; + start = pi; + } else if(path[pi] == '*'){ + //printf("found\n"); + index = pi; + imatch = mi; + } else { - pi++; + + if(path[pi] != match[mi]){ + //printf("whaa\n"); + if(index == -1) return 0; + //printf("here\n"); + pi = index + 1; + imatch++; + mi = imatch; + + } mi++; } + pi++; + } else if (step == 1){ - if(match[mi] == '/'){ - printf("\n"); + if(path[pi] == '}'){ step = 2; + name = calloc(pi - start, sizeof * name); + memcpy(name, path + start + 1, pi - start - 1); + start = mi; + //printf(": "); } else { - printf("%c",match[mi]); - mi++; + //printf("%c", path[pi]); } + pi++; + } else if (step == 2){ - if(path[pi] == '}'){ + //change this to maybe match the next path char? + if(match[mi] == '/'){ step = 0; + + char* out = calloc(mi - start, sizeof * out); + memcpy(out, match + start, mi - start); + parray_set(arr, name, out); + free(name); + //printf("\n"); } else { - pi++; + //printf("%c", match[mi]); + mi++; } } } - return 0; + + if(step == 2){ + char* out = calloc(mi - start, sizeof * out); + memcpy(out, match + start, mi - start); + parray_set(arr, name, out); + free(name); + } + + if(path[pi] != 0) for(; path[pi] == '*'; pi++); + + //printf("path: '%s':'%s'\nmatch: '%s':'%s'\nend: %i,%i\n",path, &path[pi], match,&match[mi],path[pi],match[mi]); + + return path[pi] == 0 && match[mi] == 0; } parray_t* route_match(parray_t* paths, char* request){ @@ -278,12 +325,14 @@ parray_t* route_match(parray_t* paths, char* request){ out->len = 0; for(int i = 0; i != paths->len; i++){ - match_param(paths->P[i].key->c, request); - if(strcmp(request, paths->P[i].key->c) == 0){ + //if(match_param(paths->P[i].key->c, request)) + //*if(strcmp(request, paths->P[i].key->c) == 0)*/{ + //printf("pass!\n"); out->P[out->len] = paths->P[i]; out->len++; - } + //} } + printf("\n"); return out; } diff --git a/src/net/util.h b/src/net/util.h index ea7c294..17a99ac 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -47,3 +47,6 @@ void client_fd_errors(int client_fd); int content_disposition(str* src, parray_t** _dest); parray_t* route_match(parray_t* paths, char* path); + +int match_param(char* path, char* match, parray_t* arr); + @@ -6,6 +6,7 @@ #include "math.h"
#include "net.h"
#include "thread.h"
+#include "test.h"
#include <signal.h>
#include <stdlib.h>
@@ -36,6 +37,7 @@ open_common(math); open_common(config);
open_common(net);
open_common(thread);
+open_common(test);
#define push(T, name)\
lua_pushstring(L, #name);\
@@ -62,6 +64,7 @@ int luaopen_lullaby(lua_State* L) { push(top, config);
push(top, net);
push(top, thread);
+ push(top, test);
luaI_tsets(L, top, "version", GIT_COMMIT)
//lreg("array", array_function_list);
//lreg("crypto", crypto_function_list);
diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..4408878 --- /dev/null +++ b/src/test.c @@ -0,0 +1,25 @@ +#include "test.h" +#include "net/util.h" +#include "types/parray.h" + +int ld_match(lua_State* L){ + parray_t* a = parray_init(); + int o = match_param(lua_tostring(L, 1), lua_tostring(L, 2), a); + + if(o == 0){ + lua_pushinteger(L, o); + return 1; + } + + lua_newtable(L); + int tidx = lua_gettop(L); + for(int i = 0; i != a->len; i++){ + //printf("%s:%s\n",a->P[i].key->c, (char*)a->P[i].value); + luaI_tsets(L, tidx, a->P[i].key->c, (char*)a->P[i].value); + } + + lua_pushinteger(L, o); + lua_pushvalue(L, tidx); + return 2; +} + diff --git a/src/test.h b/src/test.h new file mode 100644 index 0000000..01527c6 --- /dev/null +++ b/src/test.h @@ -0,0 +1,9 @@ +#include "lua.h" + +int ld_match(lua_State*); + +static const luaL_Reg test_function_list [] = { + {"_match", ld_match}, + + {NULL,NULL} +}; diff --git a/tests/match.lua b/tests/match.lua new file mode 100644 index 0000000..d9c1533 --- /dev/null +++ b/tests/match.lua @@ -0,0 +1,37 @@ +llb = require "lullaby" + +function test(a, b, match, expect) + res, out = llb.test._match(a, b) + + if res ~= match then + return llb.io.error("res != match") + end + + if res == 0 then return end + + if llb.array.len(out) ~= llb.array.len(expect) then + return llb.io.error("out != expect") + end + + for i, v in ipairs(expect) do + if v ~= out[i] then + return llb.io.error("out != expect") + end + end + llb.io.pprint(out) +end + +test("/{test}/","/name/", 1, {test="name"}) +test("*","/wdejowe/wde", 1, {}) +test("/*/{hello}/{meow}/end","/blah/blah/hii/end", 1, {hello="blah", meow="hii"}) +test("/*/*/{test}/*/*/end/*/real","/a/b/testing/d/e/end/f/real", 1, {test="testing"}) +test("/*/a/b/end/{word}","/w/o/m/p/a/b/end/meow", 1, {word="meow"}) +test("*", "meow/meow/meow", 1, {}) +test("{meow}", "owo", 1, {meow="owo"}) +test("/{meow}", "/owo", 1, {meow="owo"}) +test("{meow}", "/", 0) +test("/{meow}", "/", 0) + + + + diff --git a/tests/net.lua b/tests/net.lua index fdb17d4..940e2fb 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -19,12 +19,23 @@ net.listen( _G.server = server
server:all("/{name}", function(res, req)
- print("name is "..req.name)
+ --print("name is "..req.name)
+ print("name")
end)
+
+ server:all("/{name}/nya/{user}", function(res, req)
+ --print("name is "..req.name)
+ print("name id user")
+ end)
+
server:all("*", function(res, req)
print("all")
end)
+ server:all("/{name}/user/*/{id}", function(res, req)
+ print("owo")
+ end)
+
server:all("/", function(res, req)
b = crypto.md5("hello")
|
