moonbridge

diff moonbridge_http.lua @ 78:0ec070d6f5d9

Reverted experimental work on non-blocking I/O with file handles
author jbe
date Sun Apr 05 15:15:06 2015 +0200 (2015-04-05)
parents ad06fc76906a
children fca51922b708
line diff
     1.1 --- a/moonbridge_http.lua	Sun Apr 05 01:17:06 2015 +0200
     1.2 +++ b/moonbridge_http.lua	Sun Apr 05 15:15:06 2015 +0200
     1.3 @@ -180,16 +180,13 @@
     1.4    end
     1.5    local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
     1.6    local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
     1.7 -  local request_idle_timeout, request_header_timeout, response_timeout
     1.8 -  if options.request_idle_timeout ~= nil then
     1.9 -    request_idle_timeout = options.request_idle_timeout or 0
    1.10 +  local request_header_timeout, response_timeout
    1.11 +  if options.request_header_timeout ~= nil then
    1.12 +    request_header_timeout = options.request_header_timeout
    1.13 +  elseif options.timeout ~= nil then
    1.14 +    request_header_timeout = options.timeout or 0
    1.15    else
    1.16 -    request_idle_timeout = 330
    1.17 -  end
    1.18 -  if options.request_header_timeout ~= nil then
    1.19 -    request_header_timeout = options.request_header_timeout or 0
    1.20 -  else
    1.21 -    request_header_timeout = 30
    1.22 +    request_header_timeout = 360
    1.23    end
    1.24    if options.timeout ~= nil then
    1.25      response_timeout = options.timeout or 0
    1.26 @@ -200,6 +197,8 @@
    1.27    return function(socket)
    1.28      local survive = true  -- set to false if process shall be terminated later
    1.29      repeat
    1.30 +      -- (re)set timeout:
    1.31 +      timeout(request_header_timeout or 0)
    1.32        -- process named arguments "request_header_size_limit" and "request_body_size_limit":
    1.33        local remaining_header_size_limit = options.request_header_size_limit or 1024*1024
    1.34        local remaining_body_size_limit = options.request_body_size_limit or 64*1024*1024
    1.35 @@ -833,7 +832,7 @@
    1.36            end
    1.37            if request.headers_flags["Transfer-Encoding"]["chunked"] then
    1.38              while true do
    1.39 -              local line = socket:xread(32 + remaining_body_size_limit, "\n")
    1.40 +              local line = socket:readuntil("\n", 32 + remaining_body_size_limit)
    1.41                if not line then
    1.42                  request_error(true, "400 Bad Request", "Unexpected EOF while reading next chunk of request body")
    1.43                end
    1.44 @@ -854,13 +853,13 @@
    1.45                end
    1.46                if len == 0 then break end
    1.47                read_body_bytes(len, callback)
    1.48 -              local term = socket:xread(2, "\n")
    1.49 +              local term = socket:readuntil("\n", 2)
    1.50                if term ~= "\r\n" and term ~= "\n" then
    1.51                  request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body")
    1.52                end
    1.53              end
    1.54              while true do
    1.55 -              local line = socket:xread(2 + remaining_body_size_limit, "\n")
    1.56 +              local line = socket:readuntil("\n", 2 + remaining_body_size_limit)
    1.57                if line == "\r\n" or line == "\n" then break end
    1.58                remaining_body_size_limit = remaining_body_size_limit - #line
    1.59                if remaining_body_size_limit < 0 then
    1.60 @@ -894,14 +893,8 @@
    1.61            end
    1.62          end
    1.63        })
    1.64 -      -- wait for input:
    1.65 -      if not io.poll({socket.input}, nil, request_idle_timeout) then
    1.66 -        return request_error(false, "408 Request Timeout")
    1.67 -      end
    1.68 -      -- set timeout for request header processing:
    1.69 -      timeout(request_header_timeout)
    1.70        -- read and parse request line:
    1.71 -      local line = socket:xread(remaining_header_size_limit, "\n")
    1.72 +      local line = socket:readuntil("\n", remaining_header_size_limit)
    1.73        if not line then return survive end
    1.74        remaining_header_size_limit = remaining_header_size_limit - #line
    1.75        if remaining_header_size_limit == 0 then
    1.76 @@ -917,7 +910,7 @@
    1.77        end
    1.78        -- read and parse headers:
    1.79        while true do
    1.80 -        local line = socket:xread(remaining_header_size_limit, "\n");
    1.81 +        local line = socket:readuntil("\n", remaining_header_size_limit);
    1.82          remaining_header_size_limit = remaining_header_size_limit - #line
    1.83          if not line then
    1.84            return request_error(false, "400 Bad Request")
    1.85 @@ -998,14 +991,12 @@
    1.86            request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue)
    1.87          end
    1.88        end
    1.89 -      -- (re)set timeout for handler:
    1.90 +      -- (re)set timeout:
    1.91        timeout(response_timeout or 0)
    1.92        -- call underlying handler and remember boolean result:
    1.93        if handler(request) ~= true then survive = false end
    1.94        -- finish request (unless already done by underlying handler):
    1.95        request:finish()
    1.96 -      -- stop timeout timer:
    1.97 -      timeout(0)
    1.98      until connection_close_responded
    1.99      return survive
   1.100    end

Impressum / About Us