# HG changeset patch # User Matthew Wild # Date 1335391713 -3600 # Node ID a164fc7057aef311d6ec3505b55925fa16b8cd79 # Parent 55f690fdc91561d00e77838077921c2c346eb3a9 net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed) diff -r 55f690fdc915 -r a164fc7057ae net/http/server.lua --- a/net/http/server.lua Wed Apr 25 19:57:46 2012 +0100 +++ b/net/http/server.lua Wed Apr 25 23:08:33 2012 +0100 @@ -117,6 +117,11 @@ end function listener.ondisconnect(conn) + local open_response = conn._http_open_response; + if open_response and open_response.on_destroy then + open_response.finished = true; + open_response:on_destroy(); + end sessions[conn] = nil; end @@ -154,6 +159,7 @@ send = _M.send_response; finish_cb = finish_cb; }; + conn._http_open_response = response; if not request.headers.host then response.status_code = 400; @@ -195,6 +201,10 @@ end end function _M.send_response(response, body) + if response.finished then return; end + response.finished = true; + response.conn._http_open_response = nil; + local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); local headers = response.headers; body = body or ""; @@ -208,6 +218,10 @@ t_insert(output, body); response.conn:write(t_concat(output)); + if response.on_destroy then + response:on_destroy(); + response.on_destroy = nil; + end if headers.connection == "Keep-Alive" then response:finish_cb(); else