aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2017-04-21 16:42:06 +0100
committerMatthew Wild <mwild1@gmail.com>2017-04-21 16:42:06 +0100
commit66a3d68d562fb51a8b5b8918bf2e4d6a76e0519b (patch)
tree19f302ed6244db7d23a364834994ae163531aa70
parent04d3816b69a847be365f41d5a625e224108b2c76 (diff)
downloadprosody-66a3d68d562fb51a8b5b8918bf2e4d6a76e0519b.tar.gz
prosody-66a3d68d562fb51a8b5b8918bf2e4d6a76e0519b.zip
net.http: Fire new events: pre-request, request-connection-error, request, response
-rw-r--r--net/http.lua20
1 files changed, 19 insertions, 1 deletions
diff --git a/net/http.lua b/net/http.lua
index 986b6dc4..6d32b110 100644
--- a/net/http.lua
+++ b/net/http.lua
@@ -137,6 +137,15 @@ local function request(self, u, ex, callback)
req.id = ex and ex.id or make_id(req);
+ do
+ local event = { http = self, url = u, request = req, options = ex, callback = callback };
+ local ret = self.events.fire_event("pre-request", event);
+ if ret then
+ return ret;
+ end
+ req, u, ex, callback = event.request, event.url, event.options, event.callback;
+ end
+
local method, headers, body;
local host, port = req.host, req.port;
@@ -191,13 +200,20 @@ local function request(self, u, ex, callback)
local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx)
if not handler then
+ self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn });
callback(conn, 0, req);
return nil, conn;
end
req.handler, req.conn = handler, conn
req.write = function (...) return req.handler:write(...); end
- req.callback = function (content, code, request, response)
+ req.callback = function (content, code, response, request)
+ do
+ local event = { http = self, url = u, request = req, response = response, content = content, code = code, callback = callback };
+ self.events.fire_event("response", event);
+ content, code, response = event.content, event.code, event.response;
+ end
+
log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---");
return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr));
end
@@ -205,6 +221,8 @@ local function request(self, u, ex, callback)
req.state = "status";
requests[req.handler] = req;
+
+ self.events.fire_event("request", { http = self, request = req, url = u });
return req;
end