moonbridge
changeset 7:4d7551c962d5
New method request:send_text_status_response(...) that was previously only available internally as error_response(...) function
author | jbe |
---|---|
date | Thu Jan 29 01:33:00 2015 +0100 (2015-01-29) |
parents | 0449f6a4005f |
children | 32e0838d16e6 |
files | moonbridge_http.lua reference.txt |
line diff
1.1 --- a/moonbridge_http.lua Wed Jan 14 12:32:11 2015 +0100 1.2 +++ b/moonbridge_http.lua Thu Jan 29 01:33:00 2015 +0100 1.3 @@ -753,6 +753,20 @@ 1.4 read_body_bytes(request_body_content_length, callback) 1.5 end 1.6 input_state = "finished" 1.7 + end, 1.8 + -- helper function to send simple status responses, 1.9 + -- e.g. to send errors for malformed requests, etc.: 1.10 + send_text_status_response = function(status, text) 1.11 + request:send_status(status) 1.12 + request:send_header("Content-Type", "text/plain") 1.13 + if not connection_close_responded then 1.14 + request:send_header("Connection", "close") 1.15 + end 1.16 + request:send_data(status, "\n") 1.17 + if text then 1.18 + request:send_data("\n", text, "\n") 1.19 + end 1.20 + request:finish() 1.21 end 1.22 } 1.23 -- initialize tables for GET params in request object: 1.24 @@ -776,18 +790,11 @@ 1.25 end 1.26 end 1.27 }) 1.28 - -- low level HTTP error response (for malformed requests, etc.): 1.29 - local function error_response(status, text) 1.30 - request:send_status(status) 1.31 - request:send_header("Content-Type", "text/plain") 1.32 - if not connection_close_responded then 1.33 - request:send_header("Connection", "close") 1.34 - end 1.35 - request:send_data(status, "\n") 1.36 - if text then 1.37 - request:send_data("\n", text, "\n") 1.38 - end 1.39 - request:finish() 1.40 + -- wrapper for request:send_text_status_response(...) method to 1.41 + -- send an error status for malformed requests, etc., and to 1.42 + -- return the boolean value of the "survive" variable: 1.43 + local function error_response(...) 1.44 + request:send_text_status_response(...) 1.45 return survive 1.46 end 1.47 -- read and parse request line:
2.1 --- a/reference.txt Wed Jan 14 12:32:11 2015 +0100 2.2 +++ b/reference.txt Thu Jan 29 01:33:00 2015 +0100 2.3 @@ -393,6 +393,16 @@ 2.4 the callback without arguments). 2.5 2.6 2.7 +### request:send_text_status_response(status_string, text) 2.8 + 2.9 +Sends a HTTP status plus a response body of content-type "text/plain" and 2.10 +finishes the request using request:finish(). The status_string has to be 2.11 +provided in the same format as expected by request:send_status(...) and will 2.12 +be included in the response body as plain text. The additional second "text" 2.13 +argument will be appended to the reponse body (separated by an empty line) if 2.14 +given. 2.15 + 2.16 + 2.17 ### request.url 2.18 2.19 The requested URL. This value is automatically split up into request.path and