moonbridge

changeset 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 b6619de6f494
children f2efab1ba3d0
files moonbridge.c moonbridge_http.lua
line diff
     1.1 --- a/moonbridge.c	Sun Mar 08 00:53:35 2015 +0100
     1.2 +++ b/moonbridge.c	Sun Mar 08 01:09:28 2015 +0100
     1.3 @@ -993,7 +993,8 @@
     1.4    } else {
     1.5      luaL_argerror(L, 1, "Not a connection socket");
     1.6    }
     1.7 -  return 0;
     1.8 +  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
     1.9 +  return 1;
    1.10  }
    1.11  
    1.12  /* Lua function to close both input and output stream from/to peer */
    1.13 @@ -1010,7 +1011,8 @@
    1.14    if (moonbr_child_close_peersocket(timeout)) {
    1.15      moonbr_child_lua_errno_error(L, "Could not close socket connection with peer");
    1.16    }
    1.17 -  return 0;
    1.18 +  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
    1.19 +  return 1;
    1.20  }
    1.21  
    1.22  /* Lua function to close both input and output stream from/to peer */
    1.23 @@ -1021,7 +1023,8 @@
    1.24    if (moonbr_child_cancel_peersocket()) {
    1.25      moonbr_child_lua_errno_error(L, "Could not cancel socket connection with peer");
    1.26    }
    1.27 -  return 0;
    1.28 +  lua_pushboolean(L, 1);  // TODO: return nil or false on error instead of throwing error
    1.29 +  return 1;
    1.30  }
    1.31  
    1.32  /* Methods of (bidirectional) socket object passed to handler */
    1.33 @@ -2242,7 +2245,7 @@
    1.34    while (maxlen > 0 ? maxlen-- : maxlen) {
    1.35      byte = fgetc(file);
    1.36      if (byte == EOF) {
    1.37 -      if (ferror(file)) {
    1.38 +      if (ferror(file)) {  // TODO: return nil or false on error instead of throwing error
    1.39          char errmsg[MOONBR_MAXSTRERRORLEN];
    1.40          strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN);  /* use thread-safe call in case child created threads */
    1.41          luaL_error(L, "%s", errmsg);
     2.1 --- a/moonbridge_http.lua	Sun Mar 08 00:53:35 2015 +0100
     2.2 +++ b/moonbridge_http.lua	Sun Mar 08 01:09:28 2015 +0100
     2.3 @@ -245,22 +245,22 @@
     2.4              while socket.input:read(input_chunk_size) do end
     2.5            end)
     2.6            -- fully close socket:
     2.7 -          socket:close()
     2.8 +          assert(socket:close())
     2.9          else
    2.10 -          socket:flush()
    2.11 +          assert(socket:flush())
    2.12            request:stream_request_body()
    2.13          end
    2.14        end
    2.15        -- writes out buffered chunks (without flushing the socket):
    2.16        local function send_chunk()
    2.17          if chunk_bytes > 0 then
    2.18 -          socket:write(string.format("%x\r\n", chunk_bytes))
    2.19 +          assert(socket:write(string.format("%x\r\n", chunk_bytes)))
    2.20            for i = 1, #chunk_parts do
    2.21 -            socket:write(chunk_parts[i])
    2.22 +            assert(socket:write(chunk_parts[i]))
    2.23            end
    2.24            chunk_parts = {}
    2.25            chunk_bytes = 0
    2.26 -          socket:write("\r\n")
    2.27 +          assert(socket:write("\r\n"))
    2.28          end
    2.29        end
    2.30        -- terminate header section in response, optionally flushing:
    2.31 @@ -271,28 +271,28 @@
    2.32          elseif output_state == "finished" then
    2.33            error("Response has already been finished")
    2.34          elseif output_state == "info_status_sent" then
    2.35 -          socket:write("\r\n")
    2.36 -          socket:flush()
    2.37 +          assert(socket:write("\r\n"))
    2.38 +          assert(socket:flush())
    2.39            output_state = "no_status_sent"
    2.40          elseif output_state == "bodyless_status_sent" then
    2.41            if connection_close_requested and not connection_close_responded then
    2.42              request:send_header("Connection", "close")
    2.43            end
    2.44 -          socket:write("\r\n")
    2.45 +          assert(socket:write("\r\n"))
    2.46            finish_response()
    2.47            output_state = "finished"
    2.48          elseif output_state == "status_sent" then
    2.49            if not content_length then
    2.50 -            socket:write("Transfer-Encoding: chunked\r\n")
    2.51 +            assert(socket:write("Transfer-Encoding: chunked\r\n"))
    2.52            end
    2.53            if connection_close_requested and not connection_close_responded then
    2.54              request:send_header("Connection", "close")
    2.55            end
    2.56 -          socket:write("\r\n")
    2.57 +          assert(socket:write("\r\n"))
    2.58            if request.method == "HEAD" then
    2.59              finish_response()
    2.60            elseif flush then
    2.61 -            socket:flush()
    2.62 +            assert(socket:flush())
    2.63            end
    2.64            output_state = "headers_sent"
    2.65          elseif output_state ~= "headers_sent" then
    2.66 @@ -311,14 +311,14 @@
    2.67              request:process_request_body()
    2.68            end
    2.69            if output_state == "info_status_sent" then
    2.70 -            socket:write("\r\n")
    2.71 -            socket:flush()
    2.72 +            assert(socket:write("\r\n"))
    2.73 +            assert(socket:flush())
    2.74            elseif output_state ~= "no_status_sent" then
    2.75              error("HTTP status has already been sent")
    2.76            end
    2.77            local status1 = string.sub(value, 1, 1)
    2.78            local status3 = string.sub(value, 1, 3)
    2.79 -          socket:write("HTTP/1.1 ", value, "\r\n", preamble)
    2.80 +          assert(socket:write("HTTP/1.1 ", value, "\r\n", preamble))
    2.81            local without_response_body = status_without_response_body[status3]
    2.82            if without_response_body then
    2.83              output_state = "bodyless_status_sent"
    2.84 @@ -371,7 +371,7 @@
    2.85                end
    2.86              end
    2.87            end
    2.88 -          socket:write(key, ": ", value, "\r\n")
    2.89 +          assert(socket:write(key, ": ", value, "\r\n"))
    2.90          end,
    2.91          -- method to finish and flush headers:
    2.92          finish_headers = function()
    2.93 @@ -397,11 +397,11 @@
    2.94                if content_length then
    2.95                  local bytes_to_send = #str
    2.96                  if bytes_sent + bytes_to_send > content_length then
    2.97 -                  socket:write(string.sub(str, 1, content_length - bytes_sent))
    2.98 +                  assert(socket:write(string.sub(str, 1, content_length - bytes_sent)))
    2.99                    bytes_sent = content_length
   2.100                    error("Content length exceeded")
   2.101                  else
   2.102 -                  socket:write(str)
   2.103 +                  assert(socket:write(str))
   2.104                    bytes_sent = bytes_sent + bytes_to_send
   2.105                  end
   2.106                else
   2.107 @@ -417,7 +417,7 @@
   2.108          -- flush output buffer:
   2.109          flush = function(self)
   2.110            send_chunk()
   2.111 -          socket:flush()
   2.112 +          assert(socket:flush())
   2.113          end,
   2.114          -- finish response:
   2.115          finish = function(self)
   2.116 @@ -435,7 +435,7 @@
   2.117                  end
   2.118                else
   2.119                  send_chunk()
   2.120 -                socket:write("0\r\n\r\n")
   2.121 +                assert(socket:write("0\r\n\r\n"))
   2.122                end
   2.123                finish_response()
   2.124              end

Impressum / About Us