Comparison

net/httpserver.lua @ 4161:c299726d2b4e

net.httpserver: Catch errors thrown in HTTP handlers.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 23 Feb 2011 02:03:55 +0500
parent 4143:eccd3c87d717
child 4164:843240294be1
comparison
equal deleted inserted replaced
4160:f08f649b898b 4161:c299726d2b4e
15 local connlisteners_get = require "net.connlisteners".get; 15 local connlisteners_get = require "net.connlisteners".get;
16 local listener; 16 local listener;
17 17
18 local t_insert, t_concat = table.insert, table.concat; 18 local t_insert, t_concat = table.insert, table.concat;
19 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type; 19 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
20 local xpcall = xpcall;
21 local debug_traceback = debug.traceback;
20 22
21 local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end 23 local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end
22 24
23 local log = require "util.logger".init("httpserver"); 25 local log = require "util.logger".init("httpserver");
24 26
84 end 86 end
85 87
86 callback = (request.server and request.server.handlers[base]) or default_handler; 88 callback = (request.server and request.server.handlers[base]) or default_handler;
87 end 89 end
88 if callback then 90 if callback then
91 local _callback = callback;
92 function callback(a, b, c)
93 local status, result = xpcall(function() _callback(a, b, c) end, debug_traceback);
94 if status then return result; end
95 log("error", "Error in HTTP server handler: %s", result);
96 end
89 if err then 97 if err then
90 log("debug", "Request error: "..err); 98 log("debug", "Request error: "..err);
91 if not callback(nil, err, request) then 99 if not callback(nil, err, request) then
92 destroy_request(request); 100 destroy_request(request);
93 end 101 end