summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-20 18:28:30 +0000
committerEmil Renner Berthing <esmil@mailme.dk>2013-08-21 22:25:43 +0200
commit7d1e5c43b1d6c20774352ec91dd054bcdb29c688 (patch)
tree875ccbcd2f4f6c0581a7e024c1b38e9c1c1e851d
parent8499b5bc97bff61e1c37902c9147ba362fff3e58 (diff)
downloadlem-7d1e5c43b1d6c20774352ec91dd054bcdb29c688.tar.gz
lem-7d1e5c43b1d6c20774352ec91dd054bcdb29c688.tar.xz
lem-7d1e5c43b1d6c20774352ec91dd054bcdb29c688.zip
hathaway: remove unnecessary scope indentation
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
-rw-r--r--lem/hathaway.lua218
1 files changed, 108 insertions, 110 deletions
diff --git a/lem/hathaway.lua b/lem/hathaway.lua
index ca43ab3..f31320c 100644
--- a/lem/hathaway.lua
+++ b/lem/hathaway.lua
@@ -1,7 +1,7 @@
--
-- This file is part of LEM, a Lua Event Machine.
-- Copyright 2011-2013 Emil Renner Berthing
--- Copyright 2012 Asbjørn Sloth Tønnesen
+-- Copyright 2012-2013 Asbjørn Sloth Tønnesen
--
-- LEM is free software: you can redistribute it and/or modify it
-- under the terms of the GNU Lesser General Public License as
@@ -26,141 +26,139 @@ local M = {}
function M.debug() end
+local lookup = {}
+M.lookup = lookup
+
+function M.GET(path, handler)
+ local entry = lookup[path]
+ if entry then
+ entry['HEAD'] = handler
+ entry['GET'] = handler
+ else
+ entry = {
+ ['HEAD'] = handler,
+ ['GET'] = handler,
+ }
+ lookup[path] = entry
+ end
+end
+
do
- local lookup = {}
- M.lookup = lookup
+ local function static_setter(method)
+ return function(path, handler)
+ local entry = lookup[path]
+ if entry then
+ entry[method] = handler
+ else
+ lookup[path] = { [method] = handler }
+ end
+ end
+ end
- function M.GET(path, handler)
- local entry = lookup[path]
- if entry then
- entry['HEAD'] = handler
- entry['GET'] = handler
- else
- entry = {
- ['HEAD'] = handler,
+ M.POST = static_setter('POST')
+ M.PUT = static_setter('PUT')
+ M.DELETE = static_setter('DELETE')
+ M.OPTIONS = static_setter('OPTIONS')
+end
+
+function M.GETM(pattern, handler)
+ local i = 1
+ while true do
+ local entry = lookup[i]
+ if entry == nil then
+ lookup[i] = { pattern,
['GET'] = handler,
+ ['HEAD'] = handler
}
- lookup[path] = entry
+ break
end
+ if entry[1] == pattern then
+ entry['GET'] = handler
+ entry['HEAD'] = handler
+ break
+ end
+ i = i + 1
end
+end
- do
- local function static_setter(method)
- return function(path, handler)
- local entry = lookup[path]
- if entry then
+do
+ local function match_setter(method)
+ return function(pattern, handler)
+ local i = 1
+ while true do
+ local entry = lookup[i]
+ if entry == nil then
+ lookup[i] = { pattern, [method] = handler }
+ break
+ end
+ if entry[1] == pattern then
entry[method] = handler
- else
- lookup[path] = { [method] = handler }
+ break
end
+ i = i + 1
end
end
-
- M.POST = static_setter('POST')
- M.PUT = static_setter('PUT')
- M.DELETE = static_setter('DELETE')
- M.OPTIONS = static_setter('OPTIONS')
- end
-
- function M.GETM(pattern, handler)
- local i = 1
- while true do
- local entry = lookup[i]
- if entry == nil then
- lookup[i] = { pattern,
- ['GET'] = handler,
- ['HEAD'] = handler
- }
- break
- end
- if entry[1] == pattern then
- entry['GET'] = handler
- entry['HEAD'] = handler
- break
- end
- i = i + 1
- end
end
- do
- local function match_setter(method)
- return function(pattern, handler)
- local i = 1
- while true do
- local entry = lookup[i]
- if entry == nil then
- lookup[i] = { pattern, [method] = handler }
- break
- end
- if entry[1] == pattern then
- entry[method] = handler
- break
- end
- i = i + 1
- end
- end
- end
+ M.POSTM = match_setter('POST')
+ M.PUTM = match_setter('PUT')
+ M.DELETEM = match_setter('DELETE')
+ M.OPTIONSM = match_setter('OPTIONS')
+end
- M.POSTM = match_setter('POST')
- M.PUTM = match_setter('PUT')
- M.DELETEM = match_setter('DELETE')
- M.OPTIONSM = match_setter('OPTIONS')
+local function check_match(entry, req, res, ok, ...)
+ if not ok then return false end
+ local handler = entry[req.method]
+ if handler then
+ handler(req, res, ok, ...)
+ else
+ httpresp.method_not_allowed(req, res)
end
+ return true
+end
- local function check_match(entry, req, res, ok, ...)
- if not ok then return false end
- local handler = entry[req.method]
+local function handler(req, res)
+ 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, ok, ...)
+ handler(req, res)
else
httpresp.method_not_allowed(req, res)
end
- return true
- end
-
- local function handler(req, res)
- 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
- httpresp.method_not_allowed(req, res)
+ else
+ local i = 0
+ repeat
+ i = i + 1
+ local entry = lookup[i]
+ if not entry then
+ httpresp.not_found(req, res)
+ break
end
- else
- local i = 0
- repeat
- i = i + 1
- local entry = lookup[i]
- if not entry then
- httpresp.not_found(req, res)
- break
- end
- until check_match(entry, req, res, path:match(entry[1]))
- end
+ until check_match(entry, req, res, path:match(entry[1]))
end
+end
- function M.Hathaway(host, port)
- local server, err
- if port then
- server, err = httpserv.new(host, port, handler)
- else
- server, err = httpserv.new(host, handler)
- end
- if not server then M.debug('new', err) return nil, err end
+function M.Hathaway(host, port)
+ local server, err
+ if port then
+ server, err = httpserv.new(host, port, handler)
+ else
+ server, err = httpserv.new(host, handler)
+ end
+ if not server then M.debug('new', err) return nil, err end
- M.server = server
- server.debug = M.debug
+ M.server = server
+ server.debug = M.debug
- local ok, err = server:run()
- if not ok and err ~= 'interrupted' then
- M.debug('run', err)
- return nil, err
- end
- return true
+ local ok, err = server:run()
+ if not ok and err ~= 'interrupted' then
+ M.debug('run', err)
+ return nil, err
end
+ return true
end
function M.import(env)