net.httpserver: Changed an unnecessary global access.
[prosody.git] / net / httpserver.lua
index b6a080b684ff7f10c87075462d067a7e596340b7..6fd9b65eb63c008cb175612c7299a63a917a15a5 100644 (file)
@@ -20,7 +20,7 @@ local t_insert, t_concat = table.insert, table.concat;
 local s_match, s_gmatch = string.match, string.gmatch;
 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
+local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end
 
 local log = require "util.logger".init("httpserver");
 
@@ -31,7 +31,7 @@ module "httpserver"
 local default_handler;
 
 local function expectbody(reqt)
-    return reqt.method == "POST";
+       return reqt.method == "POST";
 end
 
 local function send_response(request, response)
@@ -119,6 +119,7 @@ local function request_reader(request, data, startpos)
                local function success_cb(r)
                        for k,v in pairs(r) do request[k] = v; end
                        request.url = url_parse(request.path);
+                       request.url.path = request.url.path and request.url.path:gsub("%%(%x%x)", function(x) return x.char(tonumber(x, 16)) end);
                        request.body = { request.body };
                        call_callback(request);
                end
@@ -190,6 +191,7 @@ function new_from_config(ports, handle_request, default_options)
                log("warn", "Old syntax of httpserver.new_from_config being used to register %s", handle_request);
                handle_request, default_options = default_options, { base = handle_request };
        end
+       ports = ports or {5280};
        for _, options in ipairs(ports) do
                local port = default_options.port or 5280;
                local base = default_options.base;
@@ -212,8 +214,8 @@ function new_from_config(ports, handle_request, default_options)
                        ssl.options = "no_sslv2";
                end
                
-               new{ port = port, interface = interface, 
-                       base = base, handler = handle_request, 
+               new{ port = port, interface = interface,
+                       base = base, handler = handle_request,
                        ssl = ssl, type = (ssl and "ssl") or "tcp" };
        end
 end