aboutsummaryrefslogtreecommitdiffstats
path: root/net/http/server.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-04-25 23:08:33 +0100
committerMatthew Wild <mwild1@gmail.com>2012-04-25 23:08:33 +0100
commit7e60956c9e072083e1aa33fce1cbd7f1d80a4b2e (patch)
treeb42095acf88b88c28c7b14a6520304c8a2484428 /net/http/server.lua
parenta8e1a8b7de3f9b9863855d85b8e0fe4954948ebe (diff)
downloadprosody-7e60956c9e072083e1aa33fce1cbd7f1d80a4b2e.tar.gz
prosody-7e60956c9e072083e1aa33fce1cbd7f1d80a4b2e.zip
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)
Diffstat (limited to 'net/http/server.lua')
-rw-r--r--net/http/server.lua14
1 files changed, 14 insertions, 0 deletions
diff --git a/net/http/server.lua b/net/http/server.lua
index dc7c5a06..c108a398 100644
--- a/net/http/server.lua
+++ b/net/http/server.lua
@@ -117,6 +117,11 @@ function listener.onconnect(conn)
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 @@ function handle_request(conn, request, finish_cb)
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 @@ function handle_request(conn, request, finish_cb)
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 @@ function _M.send_response(response, body)
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