From 402201ca29667b6c2dd37b9b681901c8f1106527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Tue, 4 Sep 2012 18:00:57 +0000 Subject: http: match requests on path excluding query component MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- lem/hathaway.lua | 35 ++++++++++++++++++----------------- lem/http/server.lua | 10 ++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lem/hathaway.lua b/lem/hathaway.lua index 6d4e2da..b23934c 100644 --- a/lem/hathaway.lua +++ b/lem/hathaway.lua @@ -50,28 +50,28 @@ do local lookup = {} M.lookup = lookup - function M.GET(uri, handler) - local path = lookup[uri] - if path then - path['HEAD'] = handler - path['GET'] = handler + function M.GET(path, handler) + local entry = lookup[path] + if entry then + entry['HEAD'] = handler + entry['GET'] = handler else - path = { + entry = { ['HEAD'] = handler, ['GET'] = handler, } - lookup[uri] = path + lookup[path] = entry end end do local function static_setter(method) - return function(uri, handler) - local path = lookup[uri] - if path then - path[method] = handler + return function(path, handler) + local entry = lookup[path] + if entry then + entry[method] = handler else - lookup[uri] = { [method] = handler } + lookup[path] = { [method] = handler } end end end @@ -137,10 +137,11 @@ do end local function handler(req, res) - local method, uri, version = req.method, req.uri, req.version - local path = lookup[uri] - if path then - local handler = path[method] + local method, path, version = req.method, req.path, req.version + + local entry = lookup[path] + if entry then + local handler = entry[method] if handler then handler(req, res) else @@ -155,7 +156,7 @@ do M.not_found(req, res) break end - until check_match(entry, req, res, uri:match(entry[1])) + until check_match(entry, req, res, path:match(entry[1])) end end diff --git a/lem/http/server.lua b/lem/http/server.lua index 2e15cd0..155d681 100644 --- a/lem/http/server.lua +++ b/lem/http/server.lua @@ -121,6 +121,16 @@ do local req, err = istream:read('HTTPRequest') if not req then M.debug(err) break end local method, uri, version = req.method, req.uri, req.version + + do + local pos = req.uri:find('?', 1, true) + if pos ~= nil then + req.path = req.uri:sub(1, pos-1) + else + req.path = req.uri + end + end + M.debug(format("%s %s HTTP/%s", method, uri, version)) req.ostream = ostream -- cgit v1.2.1