diff options
Diffstat (limited to 'src')
| -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 |
5 files changed, 105 insertions, 16 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} +}; |
