# HG changeset patch # User jbe # Date 1427158267 -3600 # Node ID 14ef90c46e165cc8a88199e0d4e263143c6050f9 # Parent fd0fe0adb2035d7970b73df169e98cc8cfcba3cf New method request:close_after_finish() diff -r fd0fe0adb203 -r 14ef90c46e16 moonbridge_http.lua --- a/moonbridge_http.lua Tue Mar 24 00:51:13 2015 +0100 +++ b/moonbridge_http.lua Tue Mar 24 01:51:07 2015 +0100 @@ -254,9 +254,9 @@ then local error_response_status, errmsg2 = pcall(function() request:defer_reading() -- don't read request body (because of possibly invalid state) + request:close_after_finish() -- send "Connection: close" header request:send_status(status) request:send_header("Content-Type", "text/plain") - request:send_header("Connection", "close") request:send_data(status, "\n") if text then request:send_data("\n", text, "\n") @@ -444,6 +444,17 @@ end assert_output(socket:write(key, ": ", value, "\r\n")) end, + -- method to announce (and enforce) connection close after sending the response: + close_after_finish = function() + assert_not_faulty() + if + output_state == "headers_sent" or + output_state == "finished" + then + error("All HTTP headers have already been sent") + end + connection_close_requested = true + end, -- method to finish and flush headers: finish_headers = function() assert_not_faulty() diff -r fd0fe0adb203 -r 14ef90c46e16 reference.txt --- a/reference.txt Tue Mar 24 00:51:13 2015 +0100 +++ b/reference.txt Tue Mar 24 01:51:07 2015 +0100 @@ -199,6 +199,18 @@ invocation of request:stream_request_body(...) impossible. +### request:close_after_finish() + +Closes the connection after answering the request. + +This method can only be called before the HTTP response header section has been +finished (i.e. before request:finish_headers(), request:send_data(...), or +request:finish() were called), but it may be called before a status code has +been sent using request:send_status(...). + +A corresponding "Connection: close" header is automatically sent. + + ### request.cookies A table with all cookies sent by the client.