Comparison

net/httpserver.lua @ 1536:8214458eacc8

net.httpserver: Add helper function to set up HTTP server according to given config options
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Jul 2009 15:20:43 +0100
parent 1522:569d58d21612
child 1550:03f89de65d9f
comparison
equal deleted inserted replaced
1535:0afc8ae01515 1536:8214458eacc8
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 s_match, s_gmatch = string.match, string.gmatch; 19 local s_match, s_gmatch = string.match, string.gmatch;
20 local tonumber, tostring, pairs = tonumber, tostring, pairs; 20 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
21 21
22 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end 22 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
23 23
24 local log = require "util.logger".init("httpserver"); 24 local log = require "util.logger".init("httpserver");
25 25
248 if params.base then 248 if params.base then
249 http_server.handlers[params.base] = params.handler; 249 http_server.handlers[params.base] = params.handler;
250 end 250 end
251 end 251 end
252 252
253 function new_from_config(ports, handle_request)
254 for _, options in ipairs(ports) do
255 local port, base, ssl, interface = 5280, "http-bind", false, nil;
256 if type(options) == "number" then
257 port = options;
258 elseif type(options) == "table" then
259 port, base, ssl, interface = options.port or 5280, options.path or "http-bind", options.ssl or false, options.interface;
260 elseif type(options) == "string" then
261 base = options;
262 end
263
264 if ssl then
265 ssl.mode = "server";
266 ssl.protocol = "sslv23";
267 end
268
269 new{ port = port, base = base, handler = handle_request, ssl = ssl, type = (ssl and "ssl") or "tcp" }
270 end
271 end
272
253 _M.request_reader = request_reader; 273 _M.request_reader = request_reader;
254 _M.send_response = send_response; 274 _M.send_response = send_response;
255 _M.urlencode = urlencode; 275 _M.urlencode = urlencode;
256 276
257 return _M; 277 return _M;