summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2012-09-04 18:00:57 +0000
committerEmil Renner Berthing <esmil@mailme.dk>2013-01-06 20:46:38 +0100
commitc67706958deb660cb0034f42e1cd84bb30718dcf (patch)
treece833eed25a0e3df0604ce617ed76b72630509d6
parent61816fc33d7f0fe45881272f752770ce580c7ed5 (diff)
downloadlem-c67706958deb660cb0034f42e1cd84bb30718dcf.tar.gz
lem-c67706958deb660cb0034f42e1cd84bb30718dcf.tar.xz
lem-c67706958deb660cb0034f42e1cd84bb30718dcf.zip
http: match requests on path excluding query component
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
-rw-r--r--lem/hathaway.lua36
-rw-r--r--lem/http/server.lua16
2 files changed, 34 insertions, 18 deletions
diff --git a/lem/hathaway.lua b/lem/hathaway.lua
index c411bfc..cb925f8 100644
--- a/lem/hathaway.lua
+++ b/lem/hathaway.lua
@@ -28,28 +28,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
@@ -132,11 +132,11 @@ do
end
local function handler(req, res)
- local method, uri = req.method, req.uri
- M.debug('info', format("%s %s HTTP/%s", method, uri, req.version))
- local path = lookup[uri]
- if path then
- local handler = path[method]
+ local method, path = req.method, req.path
+ M.debug('info', format("%s %s HTTP/%s", method, req.uri, req.version))
+ local entry = lookup[path]
+ if entry then
+ local handler = entry[method]
if handler then
handler(req, res)
else
@@ -151,7 +151,7 @@ do
httpserv.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 ad405f6..932b4df 100644
--- a/lem/http/server.lua
+++ b/lem/http/server.lua
@@ -120,6 +120,18 @@ end
function M.debug() end
do
+ local gsub, char, tonumber = string.gsub, string.char, tonumber
+
+ local function tochar(str)
+ return char(tonumber(str, 16))
+ end
+
+ function M.urldecode(str)
+ return gsub(gsub(str, '+', ' '), '%%(%x%x)', tochar)
+ end
+end
+
+do
local Response = {}
Response.__index = Response
M.Response = Response
@@ -148,12 +160,16 @@ do
return true
end
+ local urldecode = M.urldecode
+
local function handleHTTP(self, client)
repeat
local req, err = client:read('HTTPRequest')
if not req then self.debug('read', err) break end
local method, uri, version = req.method, req.uri, req.version
+ req.path = urldecode(uri:match('^([^?]*)'))
+
local res = new_response(req)
if version ~= '1.0' and version ~= '1.1' then