moonbridge

changeset 60:14ef90c46e16

New method request:close_after_finish()
author jbe
date Tue Mar 24 01:51:07 2015 +0100 (2015-03-24)
parents fd0fe0adb203
children 54d488774467
files moonbridge_http.lua reference.txt
line diff
     1.1 --- a/moonbridge_http.lua	Tue Mar 24 00:51:13 2015 +0100
     1.2 +++ b/moonbridge_http.lua	Tue Mar 24 01:51:07 2015 +0100
     1.3 @@ -254,9 +254,9 @@
     1.4          then
     1.5            local error_response_status, errmsg2 = pcall(function()
     1.6              request:defer_reading()  -- don't read request body (because of possibly invalid state)
     1.7 +            request:close_after_finish()  -- send "Connection: close" header
     1.8              request:send_status(status)
     1.9              request:send_header("Content-Type", "text/plain")
    1.10 -            request:send_header("Connection", "close")
    1.11              request:send_data(status, "\n")
    1.12              if text then
    1.13                request:send_data("\n", text, "\n")
    1.14 @@ -444,6 +444,17 @@
    1.15            end
    1.16            assert_output(socket:write(key, ": ", value, "\r\n"))
    1.17          end,
    1.18 +        -- method to announce (and enforce) connection close after sending the response:
    1.19 +        close_after_finish = function()
    1.20 +          assert_not_faulty()
    1.21 +          if
    1.22 +            output_state == "headers_sent" or
    1.23 +            output_state == "finished"
    1.24 +          then
    1.25 +            error("All HTTP headers have already been sent")
    1.26 +          end
    1.27 +          connection_close_requested = true
    1.28 +        end,
    1.29          -- method to finish and flush headers:
    1.30          finish_headers = function()
    1.31            assert_not_faulty()
     2.1 --- a/reference.txt	Tue Mar 24 00:51:13 2015 +0100
     2.2 +++ b/reference.txt	Tue Mar 24 01:51:07 2015 +0100
     2.3 @@ -199,6 +199,18 @@
     2.4  invocation of request:stream_request_body(...) impossible.
     2.5  
     2.6  
     2.7 +### request:close_after_finish()
     2.8 +
     2.9 +Closes the connection after answering the request.
    2.10 +
    2.11 +This method can only be called before the HTTP response header section has been
    2.12 +finished (i.e. before request:finish_headers(), request:send_data(...), or
    2.13 +request:finish() were called), but it may be called before a status code has
    2.14 +been sent using request:send_status(...).
    2.15 +
    2.16 +A corresponding "Connection: close" header is automatically sent.
    2.17 +
    2.18 +
    2.19  ### request.cookies
    2.20  
    2.21  A table with all cookies sent by the client.

Impressum / About Us