public static function parseFromGlobals() { //Parse the URI relative to SCRIPT. This is done because you could be on virtual hosting and we only want //The URI relative to the index.php script (the front controller) $path = ""; $uri = explode("/", rtrim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/")); //Remove trailing slash $script = explode("/", $_SERVER["SCRIPT_NAME"]); foreach ($script as $position => $fragment) { if (isset($uri[$position]) && strtolower($uri[$position]) == strtolower($fragment)) { $path .= $fragment; unset($uri[$position]); unset($script[$position]); } } //Disallow empty URI fragments such as: /user//profile. In other words, two backslashes cannot follow each other foreach ($uri as $fragment) { if (empty($fragment) && $fragment != 0) { throw new HTTPException_400(); } } $request = new Request(); $request->setPath("/" . $path); $request->setUri("/" . implode("/", $uri)); $request->setScript("/" . implode("/", $script)); $request->setMethod(strtoupper($_SERVER["REQUEST_METHOD"])); $request->setDomain($_SERVER["HTTP_HOST"]); $request->setQuery($_GET); $request->setProtocol($_SERVER['SERVER_PROTOCOL']); //Get the IP - Guess at best if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $request->setIp($_SERVER['HTTP_CLIENT_IP']); } else { if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $request->setIp($_SERVER['HTTP_X_FORWARDED_FOR']); } else { $request->setIp($_SERVER['REMOTE_ADDR']); } } //Set headers. $headers = Request::getGlobalHeaders(); $request->setHeaders($headers); //The request body is set to POST $request->setBody($_POST); //But we override it if content-type is application/json by reading the raw 'php://input'. $HTTP_RAW_POST_DATA is deprecated and that's why we do this! if (($request->getMethod() == "POST" || $request->getMethod() == "PUT") && isset($headers["Content_Type"]) && strpos($headers["Content_Type"], "application/json") !== false) { $request->setBody(json_decode(file_get_contents('php://input'), true)); } return $request; }