Commit b1342a59 by Taylor Otwell

Refactoring the Request class.

parent af7ba046
...@@ -35,47 +35,43 @@ class Request { ...@@ -35,47 +35,43 @@ class Request {
elseif (isset($_SERVER['REQUEST_URI'])) elseif (isset($_SERVER['REQUEST_URI']))
{ {
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($uri === false)
{
throw new \Exception("Malformed request URI. Request terminated.");
}
} }
else else
{ {
throw new \Exception('Unable to determine the request URI.'); throw new \Exception('Unable to determine the request URI.');
} }
// ------------------------------------------------------- if ($uri === false)
// Remove the application URL.
// -------------------------------------------------------
$base_url = parse_url(Config::get('application.url'), PHP_URL_PATH);
if (strpos($uri, $base_url) === 0)
{ {
$uri = (string) substr($uri, strlen($base_url)); throw new \Exception("Malformed request URI. Request terminated.");
} }
// ------------------------------------------------------- $uri = static::remove_from_uri($uri, parse_url(Config::get('application.url'), PHP_URL_PATH));
// Remove the application index and any extra slashes. $uri = static::remove_from_uri($uri, '/'.Config::get('application.index'));
// -------------------------------------------------------
$index = Config::get('application.index');
if (strpos($uri, '/'.$index) === 0)
{
$uri = (string) substr($uri, strlen('/'.$index));
}
$uri = trim($uri, '/'); $uri = trim($uri, '/');
// -------------------------------------------------------
// If the requests is to the root of the application, we
// always return a single forward slash.
// -------------------------------------------------------
return ($uri == '') ? '/' : strtolower($uri); return ($uri == '') ? '/' : strtolower($uri);
} }
/** /**
* Remove a string from the beginning of a URI.
*
* @param string $uri
* @param string $value
* @return string
*/
private static function remove_from_uri($uri, $value)
{
if (strpos($uri, $value) === 0)
{
$uri = (string) substr($uri, strlen($value));
}
return $uri;
}
/**
* Get the request method. * Get the request method.
* *
* The request method may be spoofed if a hidden "REQUEST_METHOD" POST element * The request method may be spoofed if a hidden "REQUEST_METHOD" POST element
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment