From 2e3607ab70ca069da846f875d9eb3685f93a71f2 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Tue, 20 Aug 2013 20:57:56 +0200 Subject: http.server: remove useless do .. end block --- lem/http/server.lua | 215 ++++++++++++++++++++++++++-------------------------- 1 file changed, 106 insertions(+), 109 deletions(-) diff --git a/lem/http/server.lua b/lem/http/server.lua index cafab39..fcab33b 100644 --- a/lem/http/server.lua +++ b/lem/http/server.lua @@ -58,150 +58,147 @@ local function check_match(entry, req, res, ok, ...) return true end -do - local urldecode = M.urldecode - local newresponse = response.new +local urldecode = M.urldecode +local newresponse = response.new - 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 +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('^([^?]*)')) + req.path = urldecode(uri:match('^([^?]*)')) - local res = newresponse(req) + local res = newresponse(req) - if version ~= '1.0' and version ~= '1.1' then - response.version_not_supported(req, res) - version = '1.1' + if version ~= '1.0' and version ~= '1.1' then + response.version_not_supported(req, res) + version = '1.1' + else + local expect = req.headers['expect'] + if expect and expect ~= '100-continue' then + response.expectation_failed(req, res) else - local expect = req.headers['expect'] - if expect and expect ~= '100-continue' then - response.expectation_failed(req, res) - else - self.handler(req, res) - end + self.handler(req, res) end + end - local headers = res.headers - local file, close = res.file, false - if type(file) == 'string' then - file, err = io.open(file) - if file then - close = true - else - self.debug('open', err) - res = rewresponse(req) - headers = res.headers - response.not_found(req, res) - end + local headers = res.headers + local file, close = res.file, false + if type(file) == 'string' then + file, err = io.open(file) + if file then + close = true + else + self.debug('open', err) + res = rewresponse(req) + headers = res.headers + response.not_found(req, res) end + end - if not res.status then - if #res == 0 and file == nil then - res.status = 204 - else - res.status = 200 - end + if not res.status then + if #res == 0 and file == nil then + res.status = 204 + else + res.status = 200 end + end - if headers['Content-Length'] == nil and res.status ~= 204 then - local len - if file then - len = file:size() - else - len = 0 - for i = 1, #res do - len = len + #res[i] - end + if headers['Content-Length'] == nil and res.status ~= 204 then + local len + if file then + len = file:size() + else + len = 0 + for i = 1, #res do + len = len + #res[i] end - - headers['Content-Length'] = len end - if headers['Date'] == nil then - headers['Date'] = date('!%a, %d %b %Y %T GMT') - end + headers['Content-Length'] = len + end - if headers['Server'] == nil then - headers['Server'] = 'Hathaway/0.1 LEM/0.3' - end + if headers['Date'] == nil then + headers['Date'] = date('!%a, %d %b %Y %T GMT') + end - if req.headers['connection'] == 'close' and headers['Connection'] == nil then - headers['Connection'] = 'close' - end + if headers['Server'] == nil then + headers['Server'] = 'Hathaway/0.1 LEM/0.3' + end - local rope = {} - do - local status = res.status - if type(status) == 'number' then - status = response.status_string[status] - end + if req.headers['connection'] == 'close' and headers['Connection'] == nil then + headers['Connection'] = 'close' + end - rope[1] = format('HTTP/%s %s\r\n', version, status) + local rope = {} + do + local status = res.status + if type(status) == 'number' then + status = response.status_string[status] end - res:appendheader(rope, 1) + rope[1] = format('HTTP/%s %s\r\n', version, status) + end - client:cork() + res:appendheader(rope, 1) - local ok, err = client:write(concat(rope)) - if not ok then self.debug('write', err) break end + client:cork() - if method ~= 'HEAD' then - if file then - ok, err = client:sendfile(file, headers['Content-Length']) - if close then file:close() end - else - local body = concat(res) - if #body > 0 then - ok, err = client:write(body) - end + local ok, err = client:write(concat(rope)) + if not ok then self.debug('write', err) break end + + if method ~= 'HEAD' then + if file then + ok, err = client:sendfile(file, headers['Content-Length']) + if close then file:close() end + else + local body = concat(res) + if #body > 0 then + ok, err = client:write(body) end - if not ok then self.debug('write', err) break end end + if not ok then self.debug('write', err) break end + end - client:uncork() + client:uncork() - until version == '1.0' - or headers['Connection'] == 'close' + until version == '1.0' or headers['Connection'] == 'close' - client:close() - end + client:close() +end - local Server = {} - Server.__index = Server - M.Server = Server +local Server = {} +Server.__index = Server +M.Server = Server - function Server:run() - return self.socket:autospawn(function(...) return handleHTTP(self, ...) end) - end +function Server:run() + return self.socket:autospawn(function(...) return handleHTTP(self, ...) end) +end - function Server:close() - return self.socket:close() - end +function Server:close() + return self.socket:close() +end - local type, setmetatable = type, setmetatable +local type, setmetatable = type, setmetatable - function M.new(host, port, handler) - local socket, err - if type(host) == 'string' then - socket, err = io.tcp.listen(host, port) - if not socket then - return nil, err - end - else - socket = host - handler = port +function M.new(host, port, handler) + local socket, err + if type(host) == 'string' then + socket, err = io.tcp.listen(host, port) + if not socket then + return nil, err end - - return setmetatable({ - socket = socket, - handler = handler, - debug = M.debug - }, Server) + else + socket = host + handler = port end + + return setmetatable({ + socket = socket, + handler = handler, + debug = M.debug + }, Server) end return M -- cgit v1.2.1