diff options
author | Matthew Wild <mwild1@gmail.com> | 2017-04-21 16:42:06 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2017-04-21 16:42:06 +0100 |
commit | 85bb54060cc8fa9dc8164bda4fb5b97fd288c3b2 (patch) | |
tree | 19f302ed6244db7d23a364834994ae163531aa70 /net | |
parent | 527e6d6086ee82b15e50564dda5249aa9734ce49 (diff) | |
download | prosody-85bb54060cc8fa9dc8164bda4fb5b97fd288c3b2.tar.gz prosody-85bb54060cc8fa9dc8164bda4fb5b97fd288c3b2.zip |
net.http: Fire new events: pre-request, request-connection-error, request, response
Diffstat (limited to 'net')
-rw-r--r-- | net/http.lua | 20 |
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 |