Comparison

net/http/server.lua @ 8362:c7d6c2558a24

net.http.server: Include response object in most http-error events
author Kim Alvefur <zash@zash.se>
date Fri, 27 Oct 2017 14:08:28 +0200
parent 7582:e080b8b4f3cb
child 9335:2dc7490899ae
comparison
equal deleted inserted replaced
8361:6af950b834a6 8362:c7d6c2558a24
227 end 227 end
228 end 228 end
229 229
230 if err then 230 if err then
231 response.status_code = err_code; 231 response.status_code = err_code;
232 response:send(events.fire_event("http-error", { code = err_code, message = err })); 232 response:send(events.fire_event("http-error", { code = err_code, message = err, response = response }));
233 return; 233 return;
234 end 234 end
235 235
236 local event = request.method.." "..host..request.path:match("[^?]*"); 236 local event = request.method.." "..host..request.path:match("[^?]*");
237 local payload = { request = request, response = response }; 237 local payload = { request = request, response = response };
242 local body; 242 local body;
243 local result_type = type(result); 243 local result_type = type(result);
244 if result_type == "number" then 244 if result_type == "number" then
245 response.status_code = result; 245 response.status_code = result;
246 if result >= 400 then 246 if result >= 400 then
247 body = events.fire_event("http-error", { code = result }); 247 payload.code = result;
248 body = events.fire_event("http-error", payload);
248 end 249 end
249 elseif result_type == "string" then 250 elseif result_type == "string" then
250 body = result; 251 body = result;
251 elseif result_type == "table" then 252 elseif result_type == "table" then
252 for k, v in pairs(result) do 253 for k, v in pairs(result) do
264 return; 265 return;
265 end 266 end
266 267
267 -- if handler not called, return 404 268 -- if handler not called, return 404
268 response.status_code = 404; 269 response.status_code = 404;
269 response:send(events.fire_event("http-error", { code = 404 })); 270 payload.code = 404;
271 response:send(events.fire_event("http-error", payload));
270 end 272 end
271 local function prepare_header(response) 273 local function prepare_header(response)
272 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); 274 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
273 local headers = response.headers; 275 local headers = response.headers;
274 local output = { status_line }; 276 local output = { status_line };