moonbridge

changeset 45:ab51824e139b

Added missing assert_io(...) for socket.output:close(); Close peer connection (with TCP RST) on I/O error
author jbe
date Thu Mar 19 16:54:50 2015 +0100 (2015-03-19)
parents e835cda61478
children ac37c1f28d71
files moonbridge_http.lua
line diff
     1.1 --- a/moonbridge_http.lua	Tue Mar 17 23:06:48 2015 +0100
     1.2 +++ b/moonbridge_http.lua	Thu Mar 19 16:54:50 2015 +0100
     1.3 @@ -178,21 +178,27 @@
     1.4      t[#t+1] = ""
     1.5      preamble = table.concat(t, "\r\n")
     1.6    end
     1.7 -  local function assert_io(retval, errmsg)
     1.8 -    if retval then
     1.9 -      return retval
    1.10 -    end
    1.11 -    if options.io_error_handler then
    1.12 -      options.io_error_handler(errmsg)
    1.13 -      error(errmsg)
    1.14 -    end
    1.15 -    error(errmsg, 2)
    1.16 -  end
    1.17    -- desired chunk sizes:
    1.18    local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
    1.19    local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
    1.20    -- return connect handler:
    1.21    return function(socket)
    1.22 +    -- handing I/O errors:
    1.23 +    local socket_closed = false
    1.24 +    local function assert_io(retval, errmsg)
    1.25 +      if retval then
    1.26 +        return retval
    1.27 +      end
    1.28 +      if not socket_closed then
    1.29 +        socket_closed = true
    1.30 +        socket:cancel()
    1.31 +      end
    1.32 +      if options.io_error_handler then
    1.33 +        options.io_error_handler(errmsg)
    1.34 +        error(errmsg)
    1.35 +      end
    1.36 +      error(errmsg, 2)
    1.37 +    end
    1.38      local survive = true  -- set to false if process shall be terminated later
    1.39      repeat
    1.40        -- process named arguments "request_header_size_limit" and "request_body_size_limit":
    1.41 @@ -249,12 +255,13 @@
    1.42        local function finish_response()
    1.43          if connection_close_responded then
    1.44            -- close output stream:
    1.45 -          socket.output:close()
    1.46 +          assert_io(socket.output:close())
    1.47            -- wait for EOF of peer to avoid immediate TCP RST condition:
    1.48            timeout(2, function()
    1.49              while socket.input:read(input_chunk_size) do end
    1.50            end)
    1.51            -- fully close socket:
    1.52 +          socket_closed = true  -- avoid double close on error
    1.53            assert_io(socket:close())
    1.54          else
    1.55            assert_io(socket:flush())

Impressum / About Us