moonbridge
changeset 38:7b3707ba603e
Removed unnecessary else-block (due to return in then-blocks)
author | jbe |
---|---|
date | Sat Mar 07 19:17:37 2015 +0100 (2015-03-07) |
parents | c89d8d442987 |
children | 5e73b75bd2dc |
files | moonbridge_http.lua |
line diff
1.1 --- a/moonbridge_http.lua Mon Mar 02 00:54:58 2015 +0100 1.2 +++ b/moonbridge_http.lua Sat Mar 07 19:17:37 2015 +0100 1.3 @@ -829,95 +829,94 @@ 1.4 return error_response("400 Bad Request") 1.5 elseif proto ~= "HTTP/1.1" then 1.6 return error_response("505 HTTP Version Not Supported") 1.7 - else 1.8 - -- read and parse headers: 1.9 - while true do 1.10 - local line = socket:readuntil("\n", remaining_header_size_limit); 1.11 - remaining_header_size_limit = remaining_header_size_limit - #line 1.12 - if not line then 1.13 - return error_response("400 Bad Request") 1.14 - end 1.15 - if line == "\r\n" or line == "\n" then 1.16 - break 1.17 - end 1.18 - if remaining_header_size_limit == 0 then 1.19 - return error_response("413 Request Entity Too Large", "Headers too long") 1.20 - end 1.21 - local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$") 1.22 - if not key then 1.23 - return error_response("400 Bad Request") 1.24 + end 1.25 + -- read and parse headers: 1.26 + while true do 1.27 + local line = socket:readuntil("\n", remaining_header_size_limit); 1.28 + remaining_header_size_limit = remaining_header_size_limit - #line 1.29 + if not line then 1.30 + return error_response("400 Bad Request") 1.31 + end 1.32 + if line == "\r\n" or line == "\n" then 1.33 + break 1.34 + end 1.35 + if remaining_header_size_limit == 0 then 1.36 + return error_response("413 Request Entity Too Large", "Headers too long") 1.37 + end 1.38 + local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$") 1.39 + if not key then 1.40 + return error_response("400 Bad Request") 1.41 + end 1.42 + local values = request.headers[key] 1.43 + values[#values+1] = value 1.44 + end 1.45 + -- process "Connection: close" header if existent: 1.46 + connection_close_requested = request.headers_flags["Connection"]["close"] 1.47 + -- process "Content-Length" header if existent: 1.48 + do 1.49 + local values = request.headers_csv_table["Content-Length"] 1.50 + if #values > 0 then 1.51 + request_body_content_length = tonumber(values[1]) 1.52 + local proper_value = tostring(request_body_content_length) 1.53 + for i, value in ipairs(values) do 1.54 + value = string.match(value, "^0*(.*)") 1.55 + if value ~= proper_value then 1.56 + return error_response("400 Bad Request", "Content-Length header(s) invalid") 1.57 + end 1.58 end 1.59 - local values = request.headers[key] 1.60 - values[#values+1] = value 1.61 - end 1.62 - -- process "Connection: close" header if existent: 1.63 - connection_close_requested = request.headers_flags["Connection"]["close"] 1.64 - -- process "Content-Length" header if existent: 1.65 - do 1.66 - local values = request.headers_csv_table["Content-Length"] 1.67 - if #values > 0 then 1.68 - request_body_content_length = tonumber(values[1]) 1.69 - local proper_value = tostring(request_body_content_length) 1.70 - for i, value in ipairs(values) do 1.71 - value = string.match(value, "^0*(.*)") 1.72 - if value ~= proper_value then 1.73 - return error_response("400 Bad Request", "Content-Length header(s) invalid") 1.74 - end 1.75 - end 1.76 - if request_body_content_length > remaining_body_size_limit then 1.77 - return error_response("413 Request Entity Too Large", "Request body too big") 1.78 - end 1.79 + if request_body_content_length > remaining_body_size_limit then 1.80 + return error_response("413 Request Entity Too Large", "Request body too big") 1.81 end 1.82 end 1.83 - -- process "Transfer-Encoding" header if existent: 1.84 - do 1.85 - local flag = request.headers_flags["Transfer-Encoding"]["chunked"] 1.86 - local list = request.headers_csv_table["Transfer-Encoding"] 1.87 - if (flag and #list ~= 1) or (not flag and #list ~= 0) then 1.88 - return error_response("400 Bad Request", "Unexpected Transfer-Encoding") 1.89 - end 1.90 + end 1.91 + -- process "Transfer-Encoding" header if existent: 1.92 + do 1.93 + local flag = request.headers_flags["Transfer-Encoding"]["chunked"] 1.94 + local list = request.headers_csv_table["Transfer-Encoding"] 1.95 + if (flag and #list ~= 1) or (not flag and #list ~= 0) then 1.96 + return error_response("400 Bad Request", "Unexpected Transfer-Encoding") 1.97 end 1.98 - -- process "Expect" header if existent: 1.99 - for i, value in ipairs(request.headers_csv_table["Expect"]) do 1.100 - if string.lower(value) ~= "100-continue" then 1.101 - return error_response("417 Expectation Failed", "Unexpected Expect header") 1.102 - end 1.103 - end 1.104 - -- get mandatory Host header according to RFC 7230: 1.105 - request.host = request.headers_value["Host"] 1.106 - if not request.host then 1.107 - return error_response("400 Bad Request", "No valid host header") 1.108 + end 1.109 + -- process "Expect" header if existent: 1.110 + for i, value in ipairs(request.headers_csv_table["Expect"]) do 1.111 + if string.lower(value) ~= "100-continue" then 1.112 + return error_response("417 Expectation Failed", "Unexpected Expect header") 1.113 end 1.114 - -- parse request target: 1.115 - request.path, request.query = string.match(target, "^/([^?]*)(.*)$") 1.116 - if not request.path then 1.117 - local host2 1.118 - host2, request.path, request.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$") 1.119 - if host2 then 1.120 - if request.host ~= host2 then 1.121 - return error_response("400 Bad Request", "No valid host header") 1.122 - end 1.123 - elseif not (target == "*" and request.method == "OPTIONS") then 1.124 - return error_response("400 Bad Request", "Invalid request target") 1.125 + end 1.126 + -- get mandatory Host header according to RFC 7230: 1.127 + request.host = request.headers_value["Host"] 1.128 + if not request.host then 1.129 + return error_response("400 Bad Request", "No valid host header") 1.130 + end 1.131 + -- parse request target: 1.132 + request.path, request.query = string.match(target, "^/([^?]*)(.*)$") 1.133 + if not request.path then 1.134 + local host2 1.135 + host2, request.path, request.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$") 1.136 + if host2 then 1.137 + if request.host ~= host2 then 1.138 + return error_response("400 Bad Request", "No valid host header") 1.139 end 1.140 + elseif not (target == "*" and request.method == "OPTIONS") then 1.141 + return error_response("400 Bad Request", "Invalid request target") 1.142 end 1.143 - -- parse GET params: 1.144 - if request.query then 1.145 - read_urlencoded_form(request.get_params_list, request.query) 1.146 + end 1.147 + -- parse GET params: 1.148 + if request.query then 1.149 + read_urlencoded_form(request.get_params_list, request.query) 1.150 + end 1.151 + -- parse cookies: 1.152 + for i, line in ipairs(request.headers["Cookie"]) do 1.153 + for rawkey, rawvalue in 1.154 + string.gmatch(line, "([^=; ]*)=([^=; ]*)") 1.155 + do 1.156 + request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue) 1.157 end 1.158 - -- parse cookies: 1.159 - for i, line in ipairs(request.headers["Cookie"]) do 1.160 - for rawkey, rawvalue in 1.161 - string.gmatch(line, "([^=; ]*)=([^=; ]*)") 1.162 - do 1.163 - request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue) 1.164 - end 1.165 - end 1.166 - -- call underlying handler and remember boolean result: 1.167 - if handler(request) ~= true then survive = false end 1.168 - -- finish request (unless already done by underlying handler): 1.169 - request:finish() 1.170 end 1.171 + -- call underlying handler and remember boolean result: 1.172 + if handler(request) ~= true then survive = false end 1.173 + -- finish request (unless already done by underlying handler): 1.174 + request:finish() 1.175 until connection_close_responded 1.176 return survive 1.177 end