# HG changeset patch # User jbe # Date 1425752257 -3600 # Node ID 7b3707ba603e7000280e4c0d14332646a7fe4133 # Parent c89d8d4429878e8a16a1d0c8b2a7f80bdfba8644 Removed unnecessary else-block (due to return in then-blocks) diff -r c89d8d442987 -r 7b3707ba603e moonbridge_http.lua --- a/moonbridge_http.lua Mon Mar 02 00:54:58 2015 +0100 +++ b/moonbridge_http.lua Sat Mar 07 19:17:37 2015 +0100 @@ -829,95 +829,94 @@ return error_response("400 Bad Request") elseif proto ~= "HTTP/1.1" then return error_response("505 HTTP Version Not Supported") - else - -- read and parse headers: - while true do - local line = socket:readuntil("\n", remaining_header_size_limit); - remaining_header_size_limit = remaining_header_size_limit - #line - if not line then - return error_response("400 Bad Request") - end - if line == "\r\n" or line == "\n" then - break - end - if remaining_header_size_limit == 0 then - return error_response("413 Request Entity Too Large", "Headers too long") - end - local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$") - if not key then - return error_response("400 Bad Request") + end + -- read and parse headers: + while true do + local line = socket:readuntil("\n", remaining_header_size_limit); + remaining_header_size_limit = remaining_header_size_limit - #line + if not line then + return error_response("400 Bad Request") + end + if line == "\r\n" or line == "\n" then + break + end + if remaining_header_size_limit == 0 then + return error_response("413 Request Entity Too Large", "Headers too long") + end + local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$") + if not key then + return error_response("400 Bad Request") + end + local values = request.headers[key] + values[#values+1] = value + end + -- process "Connection: close" header if existent: + connection_close_requested = request.headers_flags["Connection"]["close"] + -- process "Content-Length" header if existent: + do + local values = request.headers_csv_table["Content-Length"] + if #values > 0 then + request_body_content_length = tonumber(values[1]) + local proper_value = tostring(request_body_content_length) + for i, value in ipairs(values) do + value = string.match(value, "^0*(.*)") + if value ~= proper_value then + return error_response("400 Bad Request", "Content-Length header(s) invalid") + end end - local values = request.headers[key] - values[#values+1] = value - end - -- process "Connection: close" header if existent: - connection_close_requested = request.headers_flags["Connection"]["close"] - -- process "Content-Length" header if existent: - do - local values = request.headers_csv_table["Content-Length"] - if #values > 0 then - request_body_content_length = tonumber(values[1]) - local proper_value = tostring(request_body_content_length) - for i, value in ipairs(values) do - value = string.match(value, "^0*(.*)") - if value ~= proper_value then - return error_response("400 Bad Request", "Content-Length header(s) invalid") - end - end - if request_body_content_length > remaining_body_size_limit then - return error_response("413 Request Entity Too Large", "Request body too big") - end + if request_body_content_length > remaining_body_size_limit then + return error_response("413 Request Entity Too Large", "Request body too big") end end - -- process "Transfer-Encoding" header if existent: - do - local flag = request.headers_flags["Transfer-Encoding"]["chunked"] - local list = request.headers_csv_table["Transfer-Encoding"] - if (flag and #list ~= 1) or (not flag and #list ~= 0) then - return error_response("400 Bad Request", "Unexpected Transfer-Encoding") - end + end + -- process "Transfer-Encoding" header if existent: + do + local flag = request.headers_flags["Transfer-Encoding"]["chunked"] + local list = request.headers_csv_table["Transfer-Encoding"] + if (flag and #list ~= 1) or (not flag and #list ~= 0) then + return error_response("400 Bad Request", "Unexpected Transfer-Encoding") end - -- process "Expect" header if existent: - for i, value in ipairs(request.headers_csv_table["Expect"]) do - if string.lower(value) ~= "100-continue" then - return error_response("417 Expectation Failed", "Unexpected Expect header") - end - end - -- get mandatory Host header according to RFC 7230: - request.host = request.headers_value["Host"] - if not request.host then - return error_response("400 Bad Request", "No valid host header") + end + -- process "Expect" header if existent: + for i, value in ipairs(request.headers_csv_table["Expect"]) do + if string.lower(value) ~= "100-continue" then + return error_response("417 Expectation Failed", "Unexpected Expect header") end - -- parse request target: - request.path, request.query = string.match(target, "^/([^?]*)(.*)$") - if not request.path then - local host2 - host2, request.path, request.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$") - if host2 then - if request.host ~= host2 then - return error_response("400 Bad Request", "No valid host header") - end - elseif not (target == "*" and request.method == "OPTIONS") then - return error_response("400 Bad Request", "Invalid request target") + end + -- get mandatory Host header according to RFC 7230: + request.host = request.headers_value["Host"] + if not request.host then + return error_response("400 Bad Request", "No valid host header") + end + -- parse request target: + request.path, request.query = string.match(target, "^/([^?]*)(.*)$") + if not request.path then + local host2 + host2, request.path, request.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$") + if host2 then + if request.host ~= host2 then + return error_response("400 Bad Request", "No valid host header") end + elseif not (target == "*" and request.method == "OPTIONS") then + return error_response("400 Bad Request", "Invalid request target") end - -- parse GET params: - if request.query then - read_urlencoded_form(request.get_params_list, request.query) + end + -- parse GET params: + if request.query then + read_urlencoded_form(request.get_params_list, request.query) + end + -- parse cookies: + for i, line in ipairs(request.headers["Cookie"]) do + for rawkey, rawvalue in + string.gmatch(line, "([^=; ]*)=([^=; ]*)") + do + request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue) end - -- parse cookies: - for i, line in ipairs(request.headers["Cookie"]) do - for rawkey, rawvalue in - string.gmatch(line, "([^=; ]*)=([^=; ]*)") - do - request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue) - end - end - -- call underlying handler and remember boolean result: - if handler(request) ~= true then survive = false end - -- finish request (unless already done by underlying handler): - request:finish() end + -- call underlying handler and remember boolean result: + if handler(request) ~= true then survive = false end + -- finish request (unless already done by underlying handler): + request:finish() until connection_close_responded return survive end