From 41e6879383dd48bce4579139e60ce0ebd5213ffd Mon Sep 17 00:00:00 2001 From: ytr289 Date: Wed, 6 Apr 2016 09:33:10 -0400 Subject: [PATCH 1/4] replacing ngx.var.request_uri with ngx.var.uri --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45fa9cb..5f5cd9b 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ http { local ok, errmsg = r:execute( ngx.var.request_method, - ngx.var.request_uri, + ngx.var.uri, ngx.req.get_uri_args(), -- all these parameters ngx.req.get_post_args(), -- will be merged in order {other_arg = 1}) -- into a single "params" table From 76c352e55643c209dfa196fb6378edc14df186de Mon Sep 17 00:00:00 2001 From: ytr289 Date: Wed, 6 Apr 2016 09:33:55 -0400 Subject: [PATCH 2/4] adding support for dot(.) in the uri resource .e.g /abc.def@gmail.com/ --- router.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/router.lua b/router.lua index 3ca9a28..30c2d8b 100644 --- a/router.lua +++ b/router.lua @@ -32,18 +32,25 @@ local router = { local COLON_BYTE = string.byte(':', 1) local WILDCARD_BYTE = string.byte('*', 1) local HTTP_METHODS = {'get', 'post', 'put', 'patch', 'delete', 'trace', 'connect', 'options', 'head'} +local STR_STR_MATCH_SLASH = "[^/.]+" +local STR_STR_SPLIT_SLASH = "([^/]+)(.*)" +local STR_LEAF = "LEAF" +local STR_TABLE = "table" +local STR_STRING = "string" +local STR_ALL_METHODS = "get post put patch delete trace connect options head" +local STR_ANY = "any" local function match_one_path(node, path, f) - for token in path:gmatch("[^/.]+") do + for token in path:gmatch(STR_STR_MATCH_SLASH) do node[token] = node[token] or {} node = node[token] end - node["LEAF"] = f + node[STR_LEAF] = f end local function resolve(path, node, params) - local _, _, current_token, rest = path:find("([^/.]+)(.*)") - if not current_token then return node["LEAF"], params end + local _, _, current_token, rest = path:find(STR_STR_SPLIT_SLASH) + if not current_token then return node[STR_LEAF], params end for child_token, child_node in pairs(node) do if child_token == current_token then @@ -65,7 +72,7 @@ local function resolve(path, node, params) elseif child_token:byte(1) == WILDCARD_BYTE then -- it's a * local param_name = child_token:sub(2) params[param_name] = current_token .. rest - return node[child_token]["LEAF"], params + return node[child_token][STR_LEAF], params end end @@ -73,7 +80,7 @@ local function resolve(path, node, params) end local function merge(destination, origin, visited) - if type(origin) ~= 'table' then return origin end + if type(origin) ~= STR_TABLE then return origin end if visited[origin] then return visited[origin] end if destination == nil then destination = {} end @@ -114,7 +121,7 @@ function Router:execute(method, path, ...) end function Router:match(method, path, fun) - if type(method) == 'string' then -- always make the method to table. + if type(method) == STR_STRING then -- always make the method to table. method = {[method] = {[path] = fun}} end for m, routes in pairs(method) do @@ -131,7 +138,7 @@ for _,method in ipairs(HTTP_METHODS) do end -- end end -Router['any'] = function(self, path, f) -- match any method +Router[STR_ANY] = function(self, path, f) -- match any method for _,method in ipairs(HTTP_METHODS) do self:match(method:upper(), path, function(params) return f(params, method) end) end From 4cc691c8b098f33bf1fc1eeac6f2c9a02858ca6a Mon Sep 17 00:00:00 2001 From: ytr289 Date: Wed, 6 Apr 2016 09:52:55 -0400 Subject: [PATCH 3/4] updated test cases --- spec/router_spec.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/router_spec.lua b/spec/router_spec.lua index a35fb77..394cf1d 100644 --- a/spec/router_spec.lua +++ b/spec/router_spec.lua @@ -45,7 +45,7 @@ describe("Router", function() r:match("GET", "/foo/:id.json", write_dummy) r:execute("GET", "/foo/1.json") - assert.same(dummy.params, {id="1"}) + assert.same(dummy.params, {id="1.json"}) end) end) @@ -90,7 +90,7 @@ describe("Router", function() } }) r:execute("GET", "/foo/1.json") - assert.same(dummy.params, {id="1"}) + assert.same(dummy.params, {id="1.json"}) end) end) end) @@ -145,7 +145,7 @@ describe("Router", function() it("posts url params with an extension", function() local f, params = r:resolve("POST", "/s/21.json") assert.equals(type(f), 'function') - assert.same(params, {id = "21"}) + assert.same(params, {id = "21.json"}) end) it("gets with backtracking over url params", function() @@ -194,7 +194,7 @@ describe("Router", function() it("runs the specified function with a url param with an extension", function() r:execute("GET", "/s/21.json") - assert.same(dummy.params, {id = '21'}) + assert.same(dummy.params, {id = '21.json'}) end) it("runs the specified function with a url param in a post", function() @@ -204,7 +204,7 @@ describe("Router", function() it("runs the specified function with a url param in a post with an extension", function() r:execute("POST", "/s/21.json") - assert.same(dummy.params, {id = '21'}) + assert.same(dummy.params, {id = '21.json'}) end) describe('when given extra parameters', function() From f4722e05371278bcd9c951ce7768778998fd85f5 Mon Sep 17 00:00:00 2001 From: ytr289 Date: Wed, 6 Apr 2016 09:58:53 -0400 Subject: [PATCH 4/4] updated test cases --- spec/router_spec.lua | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/spec/router_spec.lua b/spec/router_spec.lua index 394cf1d..246b2c2 100644 --- a/spec/router_spec.lua +++ b/spec/router_spec.lua @@ -41,12 +41,6 @@ describe("Router", function() assert.same(dummy.params, {id="2"}) end) - it("supports an extension on a param", function() - r:match("GET", "/foo/:id.json", write_dummy) - - r:execute("GET", "/foo/1.json") - assert.same(dummy.params, {id="1.json"}) - end) end) describe('when first param is a table', function() @@ -83,15 +77,6 @@ describe("Router", function() assert.same(dummy.params, {id="2"}) end) - it("supports an extension on a param", function() - r:match({ - GET = { - ["/foo/:id.json"] = write_dummy - } - }) - r:execute("GET", "/foo/1.json") - assert.same(dummy.params, {id="1.json"}) - end) end) end) @@ -139,7 +124,7 @@ describe("Router", function() it("gets url params with an extension", function() local f, params = r:resolve("GET", "/s/21.json") assert.equals(type(f), 'function') - assert.same(params, {id = "21"}) + assert.same(params, {id = "21.json"}) end) it("posts url params with an extension", function()