Diff

net/http.lua @ 7465:f1923f00b7de

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Thu, 07 Jul 2016 23:12:25 +0100
parent 6826:17a4e89a4780
parent 7464:3b7de72e58a9
child 7523:3c40b972260e
line wrap: on
line diff
--- a/net/http.lua	Tue Jun 28 14:54:43 2016 +0100
+++ b/net/http.lua	Thu Jul 07 23:12:25 2016 +0100
@@ -27,6 +27,8 @@
 
 local requests = {}; -- Open requests
 
+local function make_id(req) return (tostring(req):match("%x+$")); end
+
 local listener = { default_port = 80, default_mode = "*a" };
 
 function listener.onconnect(conn)
@@ -113,6 +115,13 @@
 end
 
 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
+local function log_if_failed(id, ret, ...)
+	if not ret then
+		log("error", "Request %s: error in callback: %s", id, tostring((...)));
+	end
+	return ...;
+end
+
 local function request(u, ex, callback)
 	local req = url.parse(u);
 
@@ -125,6 +134,8 @@
 		req.path = "/";
 	end
 
+	req.id = ex and ex.id or make_id(req);
+
 	local method, headers, body;
 
 	local host, port = req.host, req.port;
@@ -161,6 +172,8 @@
 		end
 	end
 
+	log("debug", "Making %s %s request %s to %s", req.scheme, method or "GET", req.id, (ex and ex.suppress_url and host_header) or u);
+
 	-- Attach to request object
 	req.method, req.headers, req.body = method, headers, body;
 
@@ -183,7 +196,10 @@
 	req.handler, req.conn = handler, conn
 	req.write = function (...) return req.handler:write(...); end
 
-	req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end
+	req.callback = function (content, code, request, response)
+		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
 	req.reader = request_reader;
 	req.state = "status";