moonbridge

changeset 181:f0dc143f510a

Some code cleanup / fixes in HTTP module
author jbe
date Fri Jun 19 02:03:18 2015 +0200 (2015-06-19)
parents 31820816f554
children a79ed835b6de
files moonbridge_http.lua
line diff
     1.1 --- a/moonbridge_http.lua	Fri Jun 19 00:44:01 2015 +0200
     1.2 +++ b/moonbridge_http.lua	Fri Jun 19 02:03:18 2015 +0200
     1.3 @@ -354,30 +354,32 @@
     1.4        local chunk_bytes = 0          -- sum of lengths of chunks to send
     1.5        local streamed_post_params         = {}  -- mapping from POST field name to stream function
     1.6        local streamed_post_param_patterns = {}  -- list of POST field pattern and stream function pairs
     1.7 -      -- functions to assert proper output/closing:
     1.8 -      local function assert_output(...)
     1.9 -        local retval, errmsg = ...
    1.10 -        if retval then return ...  end
    1.11 -        state = "faulty"
    1.12 -        socket:reset()
    1.13 -        error("Could not send data to client: " .. errmsg)
    1.14 -      end
    1.15 -      local function assert_close(...)
    1.16 -        local retval, errmsg = ...
    1.17 -        if retval then return ...  end
    1.18 -        state = "faulty"
    1.19 -        error("Could not finish sending data to client: " .. errmsg)
    1.20 -      end
    1.21        -- function to assert non-faulty handle:
    1.22        local function assert_not_faulty()
    1.23          assert(state ~= "faulty", "Tried to use faulty request handle")
    1.24        end
    1.25        -- functions to send data to the browser:
    1.26        local function send(...)
    1.27 -        assert_output(socket:write_call(unblock, ...))
    1.28 +        local old_state, state = state, "faulty"
    1.29 +        if not socket:write_call(unblock, ...) then
    1.30 +          socket:reset()
    1.31 +          error("Could not send data to client: " .. errmsg)
    1.32 +        end
    1.33 +        state = old_state
    1.34        end
    1.35        local function send_flush(...)
    1.36 -        assert_output(socket:flush_call(unblock, ...))
    1.37 +        local old_state, state = state, "faulty"
    1.38 +        if not socket:flush_call(unblock, ...) then
    1.39 +          socket:reset()
    1.40 +          error("Could not send data to client: " .. errmsg)
    1.41 +        end
    1.42 +        state = old_state
    1.43 +      end
    1.44 +      -- function to assert proper finish/close/reset:
    1.45 +      local function assert_close(retval, errmsg)
    1.46 +        if not retval then
    1.47 +          error("Could not finish sending data to client: " .. errmsg)
    1.48 +        end
    1.49        end
    1.50        -- function to finish request:
    1.51        local function finish()
    1.52 @@ -387,26 +389,29 @@
    1.53            -- close output stream:
    1.54            send_flush()
    1.55            assert_close(socket:finish())
    1.56 -          -- wait for EOF of peer to avoid immediate TCP RST condition:
    1.57 +          -- wait for EOF from peer to avoid immediate TCP RST condition:
    1.58            consume_all()
    1.59            -- fully close socket:
    1.60            assert_close(socket:close())
    1.61          else
    1.62 +          -- flush outgoing data:
    1.63            send_flush()
    1.64 -          process_body_chunk = nil
    1.65 +          -- consume incoming data:
    1.66            consume_all()
    1.67          end
    1.68        end
    1.69        -- function that writes out buffered chunks (without flushing the socket):
    1.70        function send_chunk()
    1.71          if chunk_bytes > 0 then
    1.72 -          assert_output(socket:write(string.format("%x\r\n", chunk_bytes)))
    1.73 -          for i = 1, #chunk_parts do  -- TODO: evaluated only once?
    1.74 +          local old_state, state = state, "faulty"
    1.75 +          send(string.format("%x\r\n", chunk_bytes))
    1.76 +          for i = 1, #chunk_parts do
    1.77              send(chunk_parts[i])
    1.78              chunk_parts[i] = nil
    1.79            end
    1.80            chunk_bytes = 0
    1.81            send("\r\n")
    1.82 +          state = old_state
    1.83          end
    1.84        end
    1.85        -- function to report an error:
    1.86 @@ -431,7 +436,11 @@
    1.87              request:finish()
    1.88            end)
    1.89            if not error_response_status then
    1.90 -            error("Unexpected error while sending error response: " .. errmsg2)
    1.91 +            if text then
    1.92 +              error("Error while sending error response (" .. status .. " / " .. text .. "): " .. errmsg2)
    1.93 +            else
    1.94 +              error("Error while sending error response (" .. status .. "): " .. errmsg2)
    1.95 +            end
    1.96            end
    1.97          elseif state ~= "faulty" then
    1.98            state = "faulty"
    1.99 @@ -788,7 +797,7 @@
   1.100              end
   1.101            end
   1.102          end
   1.103 -        assert_output(socket:write(key, ": ", value, "\r\n"))
   1.104 +        send(socket:write(key, ": ", value, "\r\n"))
   1.105        end
   1.106        -- function to terminate header section in response, optionally flushing:
   1.107        -- (may be called multiple times unless response is finished)
   1.108 @@ -802,6 +811,7 @@
   1.109            if close_requested and not close_responded then
   1.110              request:send_header("Connection", "close")
   1.111            end
   1.112 +          state = "faulty"
   1.113            send("\r\n")
   1.114            finish()
   1.115            state = "finished"
   1.116 @@ -812,6 +822,7 @@
   1.117            if close_requested and not close_responded then
   1.118              request:send_header("Connection", "close")
   1.119            end
   1.120 +          state = "faulty"
   1.121            send("\r\n")
   1.122            if request.method == "HEAD" then
   1.123              finish()

Impressum / About Us