mod_register: Drop useless depencency on datamanager.
[prosody.git] / net / httpserver.lua
index 40bcd9df0155c84460e411e0c1b01baf581f83fb..59ddbb12494d915ec1d39e52c9ed89799028cb1d 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -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
@@ -200,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,
+                       write = function (...) return handler:write(...); end, state = "request",
                        server = http_servers[handler:serverport()],
                        send = send_response,
                        destroy = destroy_request,
@@ -229,7 +236,7 @@ function destroy_request(request)
                end
                request.handler:close()
                if request.conn then
-                       listener.ondisconnect(request.handler, "closed");
+                       listener.ondisconnect(request.conn, "closed");
                end
        end
 end