Comparison

net/http/server.lua @ 9562:acf74ad0b795

Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
author Matthew Wild <mwild1@gmail.com>
date Fri, 26 Oct 2018 19:32:00 +0100
parent 9379:866cba3689f4
child 9624:cc9dff0212f4
comparison
equal deleted inserted replaced
9561:cfc7b2f7251e 9562:acf74ad0b795
6 local log = require "util.logger".init("http.server"); 6 local log = require "util.logger".init("http.server");
7 local os_date = os.date; 7 local os_date = os.date;
8 local pairs = pairs; 8 local pairs = pairs;
9 local s_upper = string.upper; 9 local s_upper = string.upper;
10 local setmetatable = setmetatable; 10 local setmetatable = setmetatable;
11 local xpcall = xpcall; 11 local xpcall = require "util.xpcall".xpcall;
12 local traceback = debug.traceback; 12 local traceback = debug.traceback;
13 local tostring = tostring; 13 local tostring = tostring;
14 local cache = require "util.cache"; 14 local cache = require "util.cache";
15 local codes = require "net.http.codes"; 15 local codes = require "net.http.codes";
16 local blocksize = 2^16; 16 local blocksize = 2^16;
86 rawset(handlers, curr_event, handlers_array); 86 rawset(handlers, curr_event, handlers_array);
87 end; 87 end;
88 }); 88 });
89 89
90 local handle_request; 90 local handle_request;
91 local _1, _2, _3;
92 local function _handle_request() return handle_request(_1, _2, _3); end
93 91
94 local last_err; 92 local last_err;
95 local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end 93 local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end
96 events.add_handler("http-error", function (error) 94 events.add_handler("http-error", function (error)
97 return "Error processing request: "..codes[error.code]..". Check your error log for more information."; 95 return "Error processing request: "..codes[error.code]..". Check your error log for more information.";
105 if waiting then return; end -- log("debug", "can't process_next, waiting"); 103 if waiting then return; end -- log("debug", "can't process_next, waiting");
106 waiting = true; 104 waiting = true;
107 while sessions[conn] and #pending > 0 do 105 while sessions[conn] and #pending > 0 do
108 local request = t_remove(pending); 106 local request = t_remove(pending);
109 --log("debug", "process_next: %s", request.path); 107 --log("debug", "process_next: %s", request.path);
110 --handle_request(conn, request, process_next); 108 if not xpcall(handle_request, _traceback_handler, conn, request, process_next) then
111 _1, _2, _3 = conn, request, process_next;
112 if not xpcall(_handle_request, _traceback_handler) then
113 conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err })); 109 conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err }));
114 conn:close(); 110 conn:close();
115 end 111 end
116 end 112 end
117 --log("debug", "ready for more"); 113 --log("debug", "ready for more");