net.dns: Make sure argument to math.randomseed does not overflow a 32 bit unsigned...
[prosody.git] / net / http / parser.lua
index d3f0dc0324a7e72df10b7299d51168f4289c005d..2545b5ac41457b88e345f5a6743650052e43ff40 100644 (file)
@@ -5,7 +5,7 @@ local url_parse = require "socket.url".parse;
 local urldecode = require "net.http".urldecode;
 
 local function preprocess_path(path)
-       path = urldecode(path);
+       path = urldecode((path:gsub("//+", "/")));
        if path:sub(1,1) ~= "/" then
                path = "/"..path;
        end
@@ -77,6 +77,7 @@ function httpstream.new(success_cb, error_cb, parser_type, options_cb)
                                                        end
                                                end
                                        end
+                                       if not first_line then error = true; return error_cb("invalid-status-line"); end
                                        len = tonumber(headers["content-length"]); -- TODO check for invalid len
                                        if client then
                                                -- FIXME handle '100 Continue' response (by skipping it)
@@ -91,9 +92,17 @@ function httpstream.new(success_cb, error_cb, parser_type, options_cb)
                                                        responseheaders = headers;
                                                };
                                        else
-                                               local parsed_url = url_parse(path);
+                                               local parsed_url;
+                                               if path:byte() == 47 then -- starts with /
+                                                       local _path, _query = path:match("([^?]*).?(.*)");
+                                                       if _query == "" then _query = nil; end
+                                                       parsed_url = { path = _path, query = _query };
+                                               else
+                                                       parsed_url = url_parse(path);
+                                                       if not(parsed_url and parsed_url.path) then error = true; return error_cb("invalid-url"); end
+                                               end
                                                path = preprocess_path(parsed_url.path);
-                                               headers.host = parsed_url.host;
+                                               headers.host = parsed_url.host or headers.host;
 
                                                len = len or 0;
                                                packet = {
@@ -131,6 +140,8 @@ function httpstream.new(success_cb, error_cb, parser_type, options_cb)
                                        elseif #buf >= len then
                                                packet.body, buf = buf:sub(1, len), buf:sub(len + 1);
                                                state = nil; success_cb(packet);
+                                       else
+                                               break;
                                        end
                                end
                        end