# HG changeset patch # User jbe # Date 1425773368 -3600 # Node ID 0bb356c04f6bdd398f9f3ba140163744643a9e21 # Parent b6619de6f49421e7d6456af34aeea271d8e64277 Methods :close() and :cancel() return true value on success; Added assert(...) calls to moonbridge_http.lua on writing to client diff -r b6619de6f494 -r 0bb356c04f6b moonbridge.c --- a/moonbridge.c Sun Mar 08 00:53:35 2015 +0100 +++ b/moonbridge.c Sun Mar 08 01:09:28 2015 +0100 @@ -993,7 +993,8 @@ } else { luaL_argerror(L, 1, "Not a connection socket"); } - return 0; + lua_pushboolean(L, 1); // TODO: return nil or false on error instead of throwing error + return 1; } /* Lua function to close both input and output stream from/to peer */ @@ -1010,7 +1011,8 @@ if (moonbr_child_close_peersocket(timeout)) { moonbr_child_lua_errno_error(L, "Could not close socket connection with peer"); } - return 0; + lua_pushboolean(L, 1); // TODO: return nil or false on error instead of throwing error + return 1; } /* Lua function to close both input and output stream from/to peer */ @@ -1021,7 +1023,8 @@ if (moonbr_child_cancel_peersocket()) { moonbr_child_lua_errno_error(L, "Could not cancel socket connection with peer"); } - return 0; + lua_pushboolean(L, 1); // TODO: return nil or false on error instead of throwing error + return 1; } /* Methods of (bidirectional) socket object passed to handler */ @@ -2242,7 +2245,7 @@ while (maxlen > 0 ? maxlen-- : maxlen) { byte = fgetc(file); if (byte == EOF) { - if (ferror(file)) { + if (ferror(file)) { // TODO: return nil or false on error instead of throwing error char errmsg[MOONBR_MAXSTRERRORLEN]; strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */ luaL_error(L, "%s", errmsg); diff -r b6619de6f494 -r 0bb356c04f6b moonbridge_http.lua --- a/moonbridge_http.lua Sun Mar 08 00:53:35 2015 +0100 +++ b/moonbridge_http.lua Sun Mar 08 01:09:28 2015 +0100 @@ -245,22 +245,22 @@ while socket.input:read(input_chunk_size) do end end) -- fully close socket: - socket:close() + assert(socket:close()) else - socket:flush() + assert(socket:flush()) request:stream_request_body() end end -- writes out buffered chunks (without flushing the socket): local function send_chunk() if chunk_bytes > 0 then - socket:write(string.format("%x\r\n", chunk_bytes)) + assert(socket:write(string.format("%x\r\n", chunk_bytes))) for i = 1, #chunk_parts do - socket:write(chunk_parts[i]) + assert(socket:write(chunk_parts[i])) end chunk_parts = {} chunk_bytes = 0 - socket:write("\r\n") + assert(socket:write("\r\n")) end end -- terminate header section in response, optionally flushing: @@ -271,28 +271,28 @@ elseif output_state == "finished" then error("Response has already been finished") elseif output_state == "info_status_sent" then - socket:write("\r\n") - socket:flush() + assert(socket:write("\r\n")) + assert(socket:flush()) output_state = "no_status_sent" elseif output_state == "bodyless_status_sent" then if connection_close_requested and not connection_close_responded then request:send_header("Connection", "close") end - socket:write("\r\n") + assert(socket:write("\r\n")) finish_response() output_state = "finished" elseif output_state == "status_sent" then if not content_length then - socket:write("Transfer-Encoding: chunked\r\n") + assert(socket:write("Transfer-Encoding: chunked\r\n")) end if connection_close_requested and not connection_close_responded then request:send_header("Connection", "close") end - socket:write("\r\n") + assert(socket:write("\r\n")) if request.method == "HEAD" then finish_response() elseif flush then - socket:flush() + assert(socket:flush()) end output_state = "headers_sent" elseif output_state ~= "headers_sent" then @@ -311,14 +311,14 @@ request:process_request_body() end if output_state == "info_status_sent" then - socket:write("\r\n") - socket:flush() + assert(socket:write("\r\n")) + assert(socket:flush()) elseif output_state ~= "no_status_sent" then error("HTTP status has already been sent") end local status1 = string.sub(value, 1, 1) local status3 = string.sub(value, 1, 3) - socket:write("HTTP/1.1 ", value, "\r\n", preamble) + assert(socket:write("HTTP/1.1 ", value, "\r\n", preamble)) local without_response_body = status_without_response_body[status3] if without_response_body then output_state = "bodyless_status_sent" @@ -371,7 +371,7 @@ end end end - socket:write(key, ": ", value, "\r\n") + assert(socket:write(key, ": ", value, "\r\n")) end, -- method to finish and flush headers: finish_headers = function() @@ -397,11 +397,11 @@ if content_length then local bytes_to_send = #str if bytes_sent + bytes_to_send > content_length then - socket:write(string.sub(str, 1, content_length - bytes_sent)) + assert(socket:write(string.sub(str, 1, content_length - bytes_sent))) bytes_sent = content_length error("Content length exceeded") else - socket:write(str) + assert(socket:write(str)) bytes_sent = bytes_sent + bytes_to_send end else @@ -417,7 +417,7 @@ -- flush output buffer: flush = function(self) send_chunk() - socket:flush() + assert(socket:flush()) end, -- finish response: finish = function(self) @@ -435,7 +435,7 @@ end else send_chunk() - socket:write("0\r\n\r\n") + assert(socket:write("0\r\n\r\n")) end finish_response() end