moonbridge

diff moonbridge_http.lua @ 42:0bb356c04f6b

Methods :close() and :cancel() return true value on success; Added assert(...) calls to moonbridge_http.lua on writing to client
author jbe
date Sun Mar 08 01:09:28 2015 +0100 (2015-03-08)
parents 7b3707ba603e
children e835cda61478
line diff
     1.1 --- a/moonbridge_http.lua	Sun Mar 08 00:53:35 2015 +0100
     1.2 +++ b/moonbridge_http.lua	Sun Mar 08 01:09:28 2015 +0100
     1.3 @@ -245,22 +245,22 @@
     1.4              while socket.input:read(input_chunk_size) do end
     1.5            end)
     1.6            -- fully close socket:
     1.7 -          socket:close()
     1.8 +          assert(socket:close())
     1.9          else
    1.10 -          socket:flush()
    1.11 +          assert(socket:flush())
    1.12            request:stream_request_body()
    1.13          end
    1.14        end
    1.15        -- writes out buffered chunks (without flushing the socket):
    1.16        local function send_chunk()
    1.17          if chunk_bytes > 0 then
    1.18 -          socket:write(string.format("%x\r\n", chunk_bytes))
    1.19 +          assert(socket:write(string.format("%x\r\n", chunk_bytes)))
    1.20            for i = 1, #chunk_parts do
    1.21 -            socket:write(chunk_parts[i])
    1.22 +            assert(socket:write(chunk_parts[i]))
    1.23            end
    1.24            chunk_parts = {}
    1.25            chunk_bytes = 0
    1.26 -          socket:write("\r\n")
    1.27 +          assert(socket:write("\r\n"))
    1.28          end
    1.29        end
    1.30        -- terminate header section in response, optionally flushing:
    1.31 @@ -271,28 +271,28 @@
    1.32          elseif output_state == "finished" then
    1.33            error("Response has already been finished")
    1.34          elseif output_state == "info_status_sent" then
    1.35 -          socket:write("\r\n")
    1.36 -          socket:flush()
    1.37 +          assert(socket:write("\r\n"))
    1.38 +          assert(socket:flush())
    1.39            output_state = "no_status_sent"
    1.40          elseif output_state == "bodyless_status_sent" then
    1.41            if connection_close_requested and not connection_close_responded then
    1.42              request:send_header("Connection", "close")
    1.43            end
    1.44 -          socket:write("\r\n")
    1.45 +          assert(socket:write("\r\n"))
    1.46            finish_response()
    1.47            output_state = "finished"
    1.48          elseif output_state == "status_sent" then
    1.49            if not content_length then
    1.50 -            socket:write("Transfer-Encoding: chunked\r\n")
    1.51 +            assert(socket:write("Transfer-Encoding: chunked\r\n"))
    1.52            end
    1.53            if connection_close_requested and not connection_close_responded then
    1.54              request:send_header("Connection", "close")
    1.55            end
    1.56 -          socket:write("\r\n")
    1.57 +          assert(socket:write("\r\n"))
    1.58            if request.method == "HEAD" then
    1.59              finish_response()
    1.60            elseif flush then
    1.61 -            socket:flush()
    1.62 +            assert(socket:flush())
    1.63            end
    1.64            output_state = "headers_sent"
    1.65          elseif output_state ~= "headers_sent" then
    1.66 @@ -311,14 +311,14 @@
    1.67              request:process_request_body()
    1.68            end
    1.69            if output_state == "info_status_sent" then
    1.70 -            socket:write("\r\n")
    1.71 -            socket:flush()
    1.72 +            assert(socket:write("\r\n"))
    1.73 +            assert(socket:flush())
    1.74            elseif output_state ~= "no_status_sent" then
    1.75              error("HTTP status has already been sent")
    1.76            end
    1.77            local status1 = string.sub(value, 1, 1)
    1.78            local status3 = string.sub(value, 1, 3)
    1.79 -          socket:write("HTTP/1.1 ", value, "\r\n", preamble)
    1.80 +          assert(socket:write("HTTP/1.1 ", value, "\r\n", preamble))
    1.81            local without_response_body = status_without_response_body[status3]
    1.82            if without_response_body then
    1.83              output_state = "bodyless_status_sent"
    1.84 @@ -371,7 +371,7 @@
    1.85                end
    1.86              end
    1.87            end
    1.88 -          socket:write(key, ": ", value, "\r\n")
    1.89 +          assert(socket:write(key, ": ", value, "\r\n"))
    1.90          end,
    1.91          -- method to finish and flush headers:
    1.92          finish_headers = function()
    1.93 @@ -397,11 +397,11 @@
    1.94                if content_length then
    1.95                  local bytes_to_send = #str
    1.96                  if bytes_sent + bytes_to_send > content_length then
    1.97 -                  socket:write(string.sub(str, 1, content_length - bytes_sent))
    1.98 +                  assert(socket:write(string.sub(str, 1, content_length - bytes_sent)))
    1.99                    bytes_sent = content_length
   1.100                    error("Content length exceeded")
   1.101                  else
   1.102 -                  socket:write(str)
   1.103 +                  assert(socket:write(str))
   1.104                    bytes_sent = bytes_sent + bytes_to_send
   1.105                  end
   1.106                else
   1.107 @@ -417,7 +417,7 @@
   1.108          -- flush output buffer:
   1.109          flush = function(self)
   1.110            send_chunk()
   1.111 -          socket:flush()
   1.112 +          assert(socket:flush())
   1.113          end,
   1.114          -- finish response:
   1.115          finish = function(self)
   1.116 @@ -435,7 +435,7 @@
   1.117                  end
   1.118                else
   1.119                  send_chunk()
   1.120 -                socket:write("0\r\n\r\n")
   1.121 +                assert(socket:write("0\r\n\r\n"))
   1.122                end
   1.123                finish_response()
   1.124              end

Impressum / About Us