net.httpserver: Trailing whitespace
[prosody.git] / net / httpserver.lua
index dec13a0bafebc6df8f80e3e52780267e38ff137c..6ebfcb769293e19c02786bc50b4a185a7dfb3713 100644 (file)
@@ -36,8 +36,8 @@ end
 local function send_response(request, response)
        -- Write status line
        local resp;
-       if response.body then
-               local body = tostring(response.body);
+       if response.body or response.headers then
+               local body = response.body and tostring(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;
@@ -46,12 +46,12 @@ local function send_response(request, response)
                                t_insert(resp, k..": "..v.."\r\n");
                        end
                end
-               if not (h and h["Content-Length"]) then
+               if body and not (h and h["Content-Length"]) then
                        t_insert(resp, "Content-Length: "..#body.."\r\n");
                end
                t_insert(resp, "\r\n");
                
-               if request.method ~= "HEAD" then
+               if body and request.method ~= "HEAD" then
                        t_insert(resp, body);
                end
                request.write(t_concat(resp));
@@ -142,22 +142,29 @@ local function request_reader(request, data, startpos)
        elseif request.state == "headers" then
                log("debug", "Reading headers...")
                local pos = startpos;
-               local headers = request.headers or {};
+               local headers, headers_complete = request.headers;
+               if not headers then
+                       headers = {};
+                       request.headers = headers;
+               end
+               
                for line in data:gmatch("(.-)\r\n") do
                        startpos = (startpos or 1) + #line + 2;
                        local k, v = line:match("(%S+): (.+)");
                        if k and v then
                                headers[k:lower()] = v;
---                             log("debug", "Header: "..k:lower().." = "..v);
+                               --log("debug", "Header: '"..k:lower().."' = '"..v.."'");
                        elseif #line == 0 then
-                               request.headers = headers;
+                               headers_complete = true;
                                break;
                        else
                                log("debug", "Unhandled header line: "..line);
                        end
                end
                
-               if not expectbody(request) then 
+               if not headers_complete then return; end
+               
+               if not expectbody(request) then
                        call_callback(request);
                        return;
                end
@@ -171,7 +178,10 @@ local function request_reader(request, data, startpos)
                log("debug", "Reading request line...")
                local method, path, http, linelen = data:match("^(%S+) (%S+) HTTP/(%S+)\r\n()", startpos);
                if not method then
-                       return call_callback(request, "invalid-status-line");
+                       log("warn", "Invalid HTTP status line, telling callback then closing");
+                       local ret = call_callback(request, "invalid-status-line");
+                       request:destroy();
+                       return ret;
                end
                
                request.method, request.path, request.httpversion = method, path, http;
@@ -197,15 +207,15 @@ end
 -- The default handler for requests
 default_handler = function (method, body, request)
        log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
-       return { status = "404 Not Found", 
+       return { status = "404 Not Found",
                        headers = { ["Content-Type"] = "text/html" },
                        body = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
 end
 
 
 function new_request(handler)
-       return { handler = handler, conn = handler.socket, 
-                       write = function (...) return handler:write(...); end, state = "request", 
+       return { handler = handler, conn = handler.socket,
+                       write = function (...) return handler:write(...); end, state = "request",
                        server = http_servers[handler:serverport()],
                        send = send_response,
                        destroy = destroy_request,