Comparison

net/http.lua @ 7464:3b7de72e58a9

net.http: Add log messages for requests, including their id (so "calling callback" and tracebacks can be traced back to their initial request)
author Matthew Wild <mwild1@gmail.com>
date Thu, 07 Jul 2016 23:08:47 +0100
parent 7463:3b6e7ce9431f
child 7465:f1923f00b7de
child 7520:fc6c24cb3599
comparison
equal deleted inserted replaced
7463:3b6e7ce9431f 7464:3b7de72e58a9
113 end 113 end
114 request.parser:feed(data); 114 request.parser:feed(data);
115 end 115 end
116 116
117 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end 117 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
118 local function log_if_failed(id, ret, ...)
119 if not ret then
120 log("error", "Request %s: error in callback: %s", id, tostring((...)));
121 end
122 return ...;
123 end
124
118 local function request(u, ex, callback) 125 local function request(u, ex, callback)
119 local req = url.parse(u); 126 local req = url.parse(u);
120 127
121 if not (req and req.host) then 128 if not (req and req.host) then
122 callback(nil, 0, req); 129 callback(nil, 0, req);
163 headers[k] = v; 170 headers[k] = v;
164 end 171 end
165 end 172 end
166 end 173 end
167 174
175 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);
176
168 -- Attach to request object 177 -- Attach to request object
169 req.method, req.headers, req.body = method, headers, body; 178 req.method, req.headers, req.body = method, headers, body;
170 179
171 local using_https = req.scheme == "https"; 180 local using_https = req.scheme == "https";
172 if using_https and not ssl_available then 181 if using_https and not ssl_available then
185 return nil, conn; 194 return nil, conn;
186 end 195 end
187 req.handler, req.conn = handler, conn 196 req.handler, req.conn = handler, conn
188 req.write = function (...) return req.handler:write(...); end 197 req.write = function (...) return req.handler:write(...); end
189 198
190 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 199 req.callback = function (content, code, request, response)
200 log("debug", "request %s: Calling callback, status %s", req.id, code or "---");
201 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr));
202 end
191 req.reader = request_reader; 203 req.reader = request_reader;
192 req.state = "status"; 204 req.state = "status";
193 205
194 requests[req.handler] = req; 206 requests[req.handler] = req;
195 return req; 207 return req;