# HG changeset patch # User jbe # Date 1422491580 -3600 # Node ID 4d7551c962d5d3255e123d976c5475881a618602 # Parent 0449f6a4005f777f342d53320447d91cd108892b New method request:send_text_status_response(...) that was previously only available internally as error_response(...) function diff -r 0449f6a4005f -r 4d7551c962d5 moonbridge_http.lua --- a/moonbridge_http.lua Wed Jan 14 12:32:11 2015 +0100 +++ b/moonbridge_http.lua Thu Jan 29 01:33:00 2015 +0100 @@ -753,6 +753,20 @@ read_body_bytes(request_body_content_length, callback) end input_state = "finished" + end, + -- helper function to send simple status responses, + -- e.g. to send errors for malformed requests, etc.: + send_text_status_response = function(status, text) + request:send_status(status) + request:send_header("Content-Type", "text/plain") + if not connection_close_responded then + request:send_header("Connection", "close") + end + request:send_data(status, "\n") + if text then + request:send_data("\n", text, "\n") + end + request:finish() end } -- initialize tables for GET params in request object: @@ -776,18 +790,11 @@ end end }) - -- low level HTTP error response (for malformed requests, etc.): - local function error_response(status, text) - request:send_status(status) - request:send_header("Content-Type", "text/plain") - if not connection_close_responded then - request:send_header("Connection", "close") - end - request:send_data(status, "\n") - if text then - request:send_data("\n", text, "\n") - end - request:finish() + -- wrapper for request:send_text_status_response(...) method to + -- send an error status for malformed requests, etc., and to + -- return the boolean value of the "survive" variable: + local function error_response(...) + request:send_text_status_response(...) return survive end -- read and parse request line: diff -r 0449f6a4005f -r 4d7551c962d5 reference.txt --- a/reference.txt Wed Jan 14 12:32:11 2015 +0100 +++ b/reference.txt Thu Jan 29 01:33:00 2015 +0100 @@ -393,6 +393,16 @@ the callback without arguments). +### request:send_text_status_response(status_string, text) + +Sends a HTTP status plus a response body of content-type "text/plain" and +finishes the request using request:finish(). The status_string has to be +provided in the same format as expected by request:send_status(...) and will +be included in the response body as plain text. The additional second "text" +argument will be appended to the reponse body (separated by an empty line) if +given. + + ### request.url The requested URL. This value is automatically split up into request.path and