Changeset

8114:12df41a5a4b1

net.http: Fire new events: pre-request, request-connection-error, request, response
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 Apr 2017 16:42:06 +0100
parents 8113:cfb5ab763384
children 8115:375cf924fce1
files net/http.lua
diffstat 1 files changed, 19 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/net/http.lua	Fri Apr 21 16:41:27 2017 +0100
+++ b/net/http.lua	Fri Apr 21 16:42:06 2017 +0100
@@ -137,6 +137,15 @@
 
 	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 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 @@
 	req.state = "status";
 
 	requests[req.handler] = req;
+
+	self.events.fire_event("request", { http = self, request = req, url = u });
 	return req;
 end