# HG changeset patch # User jbe # Date 1432688148 -7200 # Node ID 8ab33bfb47e7a8eb36c77d06423b086b0a485081 # Parent 08cf8e1865e9e534c64bfa12e84d3eb20b6ce0df Minimal changes to HTTP module to support interface of new :read(...) method diff -r 08cf8e1865e9 -r 8ab33bfb47e7 moonbridge_http.lua --- a/moonbridge_http.lua Tue May 19 23:15:22 2015 +0200 +++ b/moonbridge_http.lua Wed May 27 02:55:48 2015 +0200 @@ -182,17 +182,17 @@ local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024 local request_idle_timeout, request_header_timeout, response_timeout if options.request_idle_timeout ~= nil then - request_idle_timeout = options.request_idle_timeout or 0 + request_idle_timeout = options.request_idle_timeout else request_idle_timeout = 330 end if options.request_header_timeout ~= nil then - request_header_timeout = options.request_header_timeout or 0 + request_header_timeout = options.request_header_timeout else request_header_timeout = 30 end if options.timeout ~= nil then - response_timeout = options.timeout or 0 + response_timeout = options.timeout else response_timeout = 1800 end @@ -840,7 +840,7 @@ while true do local line = socket:read(32 + remaining_body_size_limit, "\n") if not line then - request_error(true, "400 Bad Request", "Unexpected EOF while reading next chunk of request body") + request_error(true, "400 Bad Request", "Read error while reading next chunk of request body") end local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$") local chunkext @@ -860,12 +860,18 @@ if len == 0 then break end read_body_bytes(len, callback) local term = socket:read(2, "\n") + if not term then + request_error(true, "400 Bad Request", "Read error while reading chunk of request body") + end if term ~= "\r\n" and term ~= "\n" then request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body") end end while true do local line = socket:read(2 + remaining_body_size_limit, "\n") + if not line then + request_error(true, "400 Bad Request", "Read error while reading chunk of request body") + end if line == "\r\n" or line == "\n" then break end remaining_body_size_limit = remaining_body_size_limit - #line if remaining_body_size_limit < 0 then @@ -900,14 +906,17 @@ end }) -- wait for input: - if not moonbridge_io.poll({[socket] = true}, nil, request_idle_timeout) then + if not moonbridge_io.poll({[socket] = true}, nil, request_idle_timeout or nil) then return request_error(false, "408 Request Timeout") end -- set timeout for request header processing: - timeout(request_header_timeout) + timeout(request_header_timeout or 0) -- read and parse request line: local line = socket:read(remaining_header_size_limit, "\n") - if not line then return survive end + if not line then + return request_error(false, "400 Bad Request", "Read error while reading request line") + end + if line == "" then return survive end remaining_header_size_limit = remaining_header_size_limit - #line if remaining_header_size_limit == 0 then return request_error(false, "414 Request-URI Too Long") @@ -922,11 +931,11 @@ end -- read and parse headers: while true do - local line = socket:read(remaining_header_size_limit, "\n"); - remaining_header_size_limit = remaining_header_size_limit - #line + local line = socket:read(remaining_header_size_limit, "\n") if not line then - return request_error(false, "400 Bad Request") + return request_error(false, "400 Bad Request", "Read error while reading header line") end + remaining_header_size_limit = remaining_header_size_limit - #line if line == "\r\n" or line == "\n" then break end