From 7d1e5c43b1d6c20774352ec91dd054bcdb29c688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Tue, 20 Aug 2013 18:28:30 +0000 Subject: hathaway: remove unnecessary scope indentation 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 | 218 +++++++++++++++++++++++++++---------------------------- 1 file 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) -- cgit v1.2.1