# HG changeset patch # User Matthew Wild # Date 1247408443 -3600 # Node ID 8214458eacc84d0da0625d6529d766f68acaf0de # Parent 0afc8ae0151565b650691171a9a1617f8f77921e net.httpserver: Add helper function to set up HTTP server according to given config options diff -r 0afc8ae01515 -r 8214458eacc8 net/httpserver.lua --- a/net/httpserver.lua Sun Jul 12 15:18:53 2009 +0100 +++ b/net/httpserver.lua Sun Jul 12 15:20:43 2009 +0100 @@ -17,7 +17,7 @@ local t_insert, t_concat = table.insert, table.concat; local s_match, s_gmatch = string.match, string.gmatch; -local tonumber, tostring, pairs = tonumber, tostring, pairs; +local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type; local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end @@ -250,6 +250,26 @@ end end +function new_from_config(ports, handle_request) + for _, options in ipairs(ports) do + local port, base, ssl, interface = 5280, "http-bind", false, nil; + if type(options) == "number" then + port = options; + elseif type(options) == "table" then + port, base, ssl, interface = options.port or 5280, options.path or "http-bind", options.ssl or false, options.interface; + elseif type(options) == "string" then + base = options; + end + + if ssl then + ssl.mode = "server"; + ssl.protocol = "sslv23"; + end + + new{ port = port, base = base, handler = handle_request, ssl = ssl, type = (ssl and "ssl") or "tcp" } + end +end + _M.request_reader = request_reader; _M.send_response = send_response; _M.urlencode = urlencode;