Automated merge with http://waqas.ath.cx:8000/
[prosody.git] / net / httpserver.lua
index 3a3c34b4084af8d1a3b1cba97045a31ac178a162..af23a91d95fb73113edfc40bbb41bfc99147e03a 100644 (file)
@@ -11,8 +11,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 urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
-local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
+local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
 
 local log = require "util.logger".init("httpserver");
 
@@ -30,7 +29,7 @@ local function send_response(request, response)
        -- Write status line
        local resp;
        if response.body then
-               log("debug", "Sending response to %s: %s", request.id, response.body);
+               log("debug", "Sending response to %s", request.id);
                resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
                local h = response.headers;
                if h then
@@ -96,10 +95,10 @@ local function call_callback(request, err)
                
                local response = callback(request.method, request.body and t_concat(request.body), request);
                if response then
-                       if response == true then
+                       if response == true and not request.destroyed then
                                -- Keep connection open, we will reply later
                                log("warn", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy));
-                       else
+                       elseif response ~= true then
                                -- Assume response
                                send_response(request, response);
                                destroy_request(request);
@@ -126,7 +125,7 @@ local function request_reader(request, data, startpos)
        end
        if request.state == "body" then
                log("debug", "Reading body...")
-               if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end
+               if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.headers["content-length"]); end
                if startpos then
                        data = data:sub(startpos, -1)
                end
@@ -141,7 +140,7 @@ local function request_reader(request, data, startpos)
        elseif request.state == "headers" then
                log("debug", "Reading headers...")
                local pos = startpos;
-               local headers = request.responseheaders or {};
+               local headers = request.headers or {};
                for line in data:gmatch("(.-)\r\n") do
                        startpos = (startpos or 1) + #line + 2;
                        local k, v = line:match("(%S+): (.+)");
@@ -149,7 +148,7 @@ local function request_reader(request, data, startpos)
                                headers[k:lower()] = v;
 --                             log("debug", "Header: "..k:lower().." = "..v);
                        elseif #line == 0 then
-                               request.responseheaders = headers;
+                               request.headers = headers;
                                break;
                        else
                                log("debug", "Unhandled header line: "..line);