Updated Symfony HttpFoundation to 2.1.6.

This commit is contained in:
Taylor Otwell
2013-01-06 13:58:32 -06:00
parent f754e1fa55
commit cb567c5e4d
55 changed files with 1758 additions and 805 deletions

View File

@@ -16,13 +16,42 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Request represents an HTTP request.
*
* The methods dealing with URL accept / return a raw path (% encoded):
* * getBasePath
* * getBaseUrl
* * getPathInfo
* * getRequestUri
* * getUri
* * getUriForPath
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class Request
{
static protected $trustProxy = false;
const HEADER_CLIENT_IP = 'client_ip';
const HEADER_CLIENT_HOST = 'client_host';
const HEADER_CLIENT_PROTO = 'client_proto';
const HEADER_CLIENT_PORT = 'client_port';
protected static $trustProxy = false;
protected static $trustedProxies = array();
/**
* Names for headers that can be trusted when
* using trusted proxies.
*
* The default names are non-standard, but widely used
* by popular reverse proxies (like Apache mod_proxy or Amazon EC2).
*/
protected static $trustedHeaders = array(
self::HEADER_CLIENT_IP => 'X_FORWARDED_FOR',
self::HEADER_CLIENT_HOST => 'X_FORWARDED_HOST',
self::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
self::HEADER_CLIENT_PORT => 'X_FORWARDED_PORT',
);
/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
@@ -79,17 +108,17 @@ class Request
protected $content;
/**
* @var string
* @var array
*/
protected $languages;
/**
* @var string
* @var array
*/
protected $charsets;
/**
* @var string
* @var array
*/
protected $acceptableContentTypes;
@@ -139,9 +168,9 @@ class Request
protected $defaultLocale = 'en';
/**
* @var string
* @var array
*/
static protected $formats;
protected static $formats;
/**
* Constructor.
@@ -205,11 +234,11 @@ class Request
*
* @api
*/
static public function createFromGlobals()
public static function createFromGlobals()
{
$request = new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
if (0 === strpos($request->server->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
) {
parse_str($request->getContent(), $data);
@@ -224,7 +253,7 @@ class Request
*
* @param string $uri The URI
* @param string $method The HTTP method
* @param array $parameters The request (GET) or query (POST) parameters
* @param array $parameters The query (GET) or request (POST) parameters
* @param array $cookies The request cookies ($_COOKIE)
* @param array $files The request files ($_FILES)
* @param array $server The server parameters ($_SERVER)
@@ -234,7 +263,7 @@ class Request
*
* @api
*/
static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
public static function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
{
$defaults = array(
'SERVER_NAME' => 'localhost',
@@ -278,7 +307,7 @@ class Request
}
if (!isset($components['path'])) {
$components['path'] = '';
$components['path'] = '/';
}
switch (strtoupper($method)) {
@@ -297,15 +326,12 @@ class Request
}
if (isset($components['query'])) {
$queryString = html_entity_decode($components['query']);
parse_str($queryString, $qs);
if (is_array($qs)) {
$query = array_replace($qs, $query);
}
parse_str(html_entity_decode($components['query']), $qs);
$query = array_replace($qs, $query);
}
$queryString = http_build_query($query);
$queryString = http_build_query($query, '', '&');
$uri = $components['path'].($queryString ? '?'.$queryString : '');
$uri = $components['path'].('' !== $queryString ? '?'.$queryString : '');
$server = array_replace($defaults, $server, array(
'REQUEST_METHOD' => strtoupper($method),
@@ -327,6 +353,8 @@ class Request
* @param array $files The FILES parameters
* @param array $server The SERVER parameters
*
* @return Request The duplicated request
*
* @api
*/
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
@@ -397,7 +425,8 @@ class Request
/**
* Overrides the PHP global variables according to this request instance.
*
* It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE, and $_FILES.
* It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE.
* $_FILES is never override, see rfc1867
*
* @api
*/
@@ -407,7 +436,6 @@ class Request
$_POST = $this->request->all();
$_SERVER = $this->server->all();
$_COOKIE = $this->cookies->all();
// FIXME: populate $_FILES
foreach ($this->headers->all() as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
@@ -418,22 +446,64 @@ class Request
}
}
// FIXME: should read variables_order and request_order
// to know which globals to merge and in which order
$_REQUEST = array_merge($_GET, $_POST);
$request = array('g' => $_GET, 'p' => $_POST, 'c' => $_COOKIE);
$requestOrder = ini_get('request_order') ?: ini_get('variable_order');
$requestOrder = preg_replace('#[^cgp]#', '', strtolower($requestOrder)) ?: 'gp';
$_REQUEST = array();
foreach (str_split($requestOrder) as $order) {
$_REQUEST = array_merge($_REQUEST, $request[$order]);
}
}
/**
* Trusts $_SERVER entries coming from proxies.
*
* You should only call this method if your application
* is hosted behind a reverse proxy that you manage.
* @deprecated Deprecated since version 2.0, to be removed in 2.3. Use setTrustedProxies instead.
*/
public static function trustProxyData()
{
self::$trustProxy = true;
}
/**
* Sets a list of trusted proxies.
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies A list of trusted proxies
*
* @api
*/
static public function trustProxyData()
public static function setTrustedProxies(array $proxies)
{
self::$trustProxy = true;
self::$trustedProxies = $proxies;
self::$trustProxy = $proxies ? true : false;
}
/**
* Sets the name for trusted headers.
*
* The following header keys are supported:
*
* * Request::HEADER_CLIENT_IP: defaults to X-Forwarded-For (see getClientIp())
* * Request::HEADER_CLIENT_HOST: defaults to X-Forwarded-Host (see getClientHost())
* * Request::HEADER_CLIENT_PORT: defaults to X-Forwarded-Port (see getClientPort())
* * Request::HEADER_CLIENT_PROTO: defaults to X-Forwarded-Proto (see getScheme() and isSecure())
*
* Setting an empty value allows to disable the trusted header for the given key.
*
* @param string $key The header key
* @param string $value The header name
*/
public static function setTrustedHeaderName($key, $value)
{
if (!array_key_exists($key, self::$trustedHeaders)) {
throw new \InvalidArgumentException(sprintf('Unable to set the trusted header name for key "%s".', $key));
}
self::$trustedHeaders[$key] = $value;
}
/**
@@ -442,29 +512,72 @@ class Request
*
* @return boolean
*/
static public function isProxyTrusted()
public static function isProxyTrusted()
{
return self::$trustProxy;
}
/**
* Normalizes a query string.
*
* It builds a normalized query string, where keys/value pairs are alphabetized,
* have consistent escaping and unneeded delimiters are removed.
*
* @param string $qs Query string
*
* @return string A normalized query string for the Request
*/
public static function normalizeQueryString($qs)
{
if ('' == $qs) {
return '';
}
$parts = array();
$order = array();
foreach (explode('&', $qs) as $param) {
if ('' === $param || '=' === $param[0]) {
// Ignore useless delimiters, e.g. "x=y&".
// Also ignore pairs with empty key, even if there was a value, e.g. "=value", as such nameless values cannot be retrieved anyway.
// PHP also does not include them when building _GET.
continue;
}
$keyValuePair = explode('=', $param, 2);
// GET parameters, that are submitted from a HTML form, encode spaces as "+" by default (as defined in enctype application/x-www-form-urlencoded).
// PHP also converts "+" to spaces when filling the global _GET or when using the function parse_str. This is why we use urldecode and then normalize to
// RFC 3986 with rawurlencode.
$parts[] = isset($keyValuePair[1]) ?
rawurlencode(urldecode($keyValuePair[0])).'='.rawurlencode(urldecode($keyValuePair[1])) :
rawurlencode(urldecode($keyValuePair[0]));
$order[] = urldecode($keyValuePair[0]);
}
array_multisort($order, SORT_ASC, $parts);
return implode('&', $parts);
}
/**
* Gets a "parameter" value.
*
* This method is mainly useful for libraries that want to provide some flexibility.
*
* Order of precedence: GET, PATH, POST, COOKIE
* Order of precedence: GET, PATH, POST
*
* Avoid using this method in controllers:
*
* * slow
* * prefer to get from a "named" source
*
* It is better to explicity get request parameters from the appropriate
* public property instead (query, request, attributes, ...).
* It is better to explicitly get request parameters from the appropriate
* public property instead (query, attributes, request).
*
* @param string $key the key
* @param mixed $default the default value
* @param type $deep is parameter deep in multidimensional array
* @param string $key the key
* @param mixed $default the default value
* @param Boolean $deep is parameter deep in multidimensional array
*
* @return mixed
*/
@@ -489,22 +602,24 @@ class Request
* Whether the request contains a Session which was started in one of the
* previous requests.
*
* @return boolean
* @return Boolean
*
* @api
*/
public function hasPreviousSession()
{
// the check for $this->session avoids malicious users trying to fake a session cookie with proper name
$sessionName = $this->hasSession() ? $this->session->getName() : null;
return $this->cookies->has($sessionName) && $this->hasSession();
return $this->hasSession() && $this->cookies->has($this->session->getName());
}
/**
* Whether the request contains a Session object.
*
* @return boolean
* This method does not give any information about the state of the session object,
* like whether the session is started or not. It is just a way to check if this Request
* is associated with a Session instance.
*
* @return Boolean true when the Request contains a Session object, false otherwise
*
* @api
*/
@@ -528,23 +643,43 @@ class Request
/**
* Returns the client IP address.
*
* This method can read the client IP address from the "X-Forwarded-For" header
* when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
* header value is a comma+space separated list of IP addresses, the left-most
* being the original client, and each successive proxy that passed the request
* adding the IP address where it received the request from.
*
* If your reverse proxy uses a different header name than "X-Forwarded-For",
* ("Client-Ip" for instance), configure it via "setTrustedHeaderName()" with
* the "client-ip" key.
*
* @return string The client IP address
*
* @see http://en.wikipedia.org/wiki/X-Forwarded-For
*
* @deprecated The proxy argument is deprecated since version 2.0 and will be removed in 2.3. Use setTrustedProxies instead.
*
* @api
*/
public function getClientIp()
{
if (self::$trustProxy) {
if ($this->server->has('HTTP_CLIENT_IP')) {
return $this->server->get('HTTP_CLIENT_IP');
} elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
$clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'), 2);
$ip = $this->server->get('REMOTE_ADDR');
return isset($clientIp[0]) ? trim($clientIp[0]) : '';
}
if (!self::$trustProxy) {
return $ip;
}
return $this->server->get('REMOTE_ADDR');
if (!self::$trustedHeaders[self::HEADER_CLIENT_IP] || !$this->headers->has(self::$trustedHeaders[self::HEADER_CLIENT_IP])) {
return $ip;
}
$clientIps = array_map('trim', explode(',', $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_IP])));
$clientIps[] = $ip;
$trustedProxies = self::$trustProxy && !self::$trustedProxies ? array($ip) : self::$trustedProxies;
$clientIps = array_diff($clientIps, $trustedProxies);
return array_pop($clientIps);
}
/**
@@ -568,9 +703,10 @@ class Request
*
* * http://localhost/mysite returns an empty string
* * http://localhost/mysite/about returns '/about'
* * htpp://localhost/mysite/enco%20ded returns '/enco%20ded'
* * http://localhost/mysite/about?var=1 returns '/about'
*
* @return string
* @return string The raw path (i.e. not urldecoded)
*
* @api
*/
@@ -588,11 +724,12 @@ class Request
*
* Suppose that an index.php file instantiates this request object:
*
* * http://localhost/index.php returns an empty string
* * http://localhost/index.php/page returns an empty string
* * http://localhost/web/index.php return '/web'
* * http://localhost/index.php returns an empty string
* * http://localhost/index.php/page returns an empty string
* * http://localhost/web/index.php returns '/web'
* * http://localhost/we%20b/index.php returns '/we%20b'
*
* @return string
* @return string The raw path (i.e. not urldecoded)
*
* @api
*/
@@ -613,7 +750,7 @@ class Request
* This is similar to getBasePath(), except that it also includes the
* script filename (e.g. index.php) if one exists.
*
* @return string
* @return string The raw url (i.e. not urldecoded)
*
* @api
*/
@@ -641,17 +778,25 @@ class Request
/**
* Returns the port on which the request is made.
*
* This method can read the client port from the "X-Forwarded-Port" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Port" header must contain the client port.
*
* If your reverse proxy uses a different header name than "X-Forwarded-Port",
* configure it via "setTrustedHeaderName()" with the "client-port" key.
*
* @return string
*
* @api
*/
public function getPort()
{
if (self::$trustProxy && $this->headers->has('X-Forwarded-Port')) {
return $this->headers->get('X-Forwarded-Port');
} else {
return $this->server->get('SERVER_PORT');
if (self::$trustProxy && self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
return $port;
}
return $this->server->get('SERVER_PORT');
}
/**
@@ -674,6 +819,23 @@ class Request
return $this->server->get('PHP_AUTH_PW');
}
/**
* Gets the user info.
*
* @return string A user name and, optionally, scheme-specific information about how to gain authorization to access the server
*/
public function getUserInfo()
{
$userinfo = $this->getUser();
$pass = $this->getPassword();
if ('' != $pass) {
$userinfo .= ":$pass";
}
return $userinfo;
}
/**
* Returns the HTTP host being requested.
*
@@ -698,7 +860,7 @@ class Request
/**
* Returns the requested URI.
*
* @return string
* @return string The raw URI (i.e. not urldecoded)
*
* @api
*/
@@ -711,6 +873,19 @@ class Request
return $this->requestUri;
}
/**
* Gets the scheme and HTTP host.
*
* If the URL was called with basic authentication, the user
* and the password are not added to the generated string.
*
* @return string The scheme and HTTP host
*/
public function getSchemeAndHttpHost()
{
return $this->getScheme().'://'.$this->getHttpHost();
}
/**
* Generates a normalized URI for the Request.
*
@@ -727,20 +902,7 @@ class Request
$qs = '?'.$qs;
}
$auth = '';
if ($user = $this->getUser()) {
$auth = $user;
}
if ($pass = $this->getPassword()) {
$auth .= ":$pass";
}
if ('' !== $auth) {
$auth .= '@';
}
return $this->getScheme().'://'.$auth.$this->getHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
}
/**
@@ -754,7 +916,7 @@ class Request
*/
public function getUriForPath($path)
{
return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
}
/**
@@ -769,71 +931,76 @@ class Request
*/
public function getQueryString()
{
if (!$qs = $this->server->get('QUERY_STRING')) {
return null;
}
$qs = static::normalizeQueryString($this->server->get('QUERY_STRING'));
$parts = array();
$order = array();
foreach (explode('&', $qs) as $segment) {
if (false === strpos($segment, '=')) {
$parts[] = $segment;
$order[] = $segment;
} else {
$tmp = explode('=', rawurldecode($segment), 2);
$parts[] = rawurlencode($tmp[0]).'='.rawurlencode($tmp[1]);
$order[] = $tmp[0];
}
}
array_multisort($order, SORT_ASC, $parts);
return implode('&', $parts);
return '' === $qs ? null : $qs;
}
/**
* Checks whether the request is secure or not.
*
* This method can read the client port from the "X-Forwarded-Proto" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
*
* If your reverse proxy uses a different header name than "X-Forwarded-Proto"
* ("SSL_HTTPS" for instance), configure it via "setTrustedHeaderName()" with
* the "client-proto" key.
*
* @return Boolean
*
* @api
*/
public function isSecure()
{
return (
(strtolower($this->server->get('HTTPS')) == 'on' || $this->server->get('HTTPS') == 1)
||
(self::$trustProxy && strtolower($this->headers->get('SSL_HTTPS')) == 'on' || $this->headers->get('SSL_HTTPS') == 1)
||
(self::$trustProxy && strtolower($this->headers->get('X_FORWARDED_PROTO')) == 'https')
);
if (self::$trustProxy && self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && $proto = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO])) {
return in_array(strtolower($proto), array('https', 'on', '1'));
}
return 'on' == strtolower($this->server->get('HTTPS')) || 1 == $this->server->get('HTTPS');
}
/**
* Returns the host name.
*
* This method can read the client port from the "X-Forwarded-Host" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Host" header must contain the client host name.
*
* If your reverse proxy uses a different header name than "X-Forwarded-Host",
* configure it via "setTrustedHeaderName()" with the "client-host" key.
*
* @return string
*
* @throws \UnexpectedValueException when the host name is invalid
*
* @api
*/
public function getHost()
{
if (self::$trustProxy && $host = $this->headers->get('X_FORWARDED_HOST')) {
if (self::$trustProxy && self::$trustedHeaders[self::HEADER_CLIENT_HOST] && $host = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_HOST])) {
$elements = explode(',', $host);
$host = trim($elements[count($elements) - 1]);
} else {
if (!$host = $this->headers->get('HOST')) {
if (!$host = $this->server->get('SERVER_NAME')) {
$host = $this->server->get('SERVER_ADDR', '');
}
$host = $elements[count($elements) - 1];
} elseif (!$host = $this->headers->get('HOST')) {
if (!$host = $this->server->get('SERVER_NAME')) {
$host = $this->server->get('SERVER_ADDR', '');
}
}
// Remove port number from host
$host = preg_replace('/:\d+$/', '', $host);
// trim and remove port number from host
// host is lowercase as per RFC 952/2181
$host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
return trim($host);
// as the host can come from the user (HTTP_HOST and depending on the configuration, SERVER_NAME too can come from the user)
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
if ($host && !preg_match('/^\[?(?:[a-zA-Z0-9-:\]_]+\.?)+$/', $host)) {
throw new \UnexpectedValueException('Invalid Host');
}
return $host;
}
/**
@@ -863,7 +1030,7 @@ class Request
if (null === $this->method) {
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
if ('POST' === $this->method) {
$this->method = strtoupper($this->headers->get('X-HTTP-METHOD-OVERRIDE', $this->request->get('_method', 'POST')));
$this->method = strtoupper($this->headers->get('X-HTTP-METHOD-OVERRIDE', $this->request->get('_method', $this->query->get('_method', 'POST'))));
}
}
@@ -873,7 +1040,7 @@ class Request
/**
* Gets the mime type associated with the format.
*
* @param string $format The format
* @param string $format The format
*
* @return string The associated mime type (null if not found)
*
@@ -891,9 +1058,9 @@ class Request
/**
* Gets the format associated with the mime type.
*
* @param string $mimeType The associated mime type
* @param string $mimeType The associated mime type
*
* @return string The format (null if not found)
* @return string|null The format (null if not found)
*
* @api
*/
@@ -919,8 +1086,8 @@ class Request
/**
* Associates a format with mime types.
*
* @param string $format The format
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
* @param string $format The format
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
*
* @api
*/
@@ -942,7 +1109,7 @@ class Request
* * _format request parameter
* * $default
*
* @param string $default The default format
* @param string $default The default format
*
* @return string The request format
*
@@ -972,13 +1139,13 @@ class Request
/**
* Gets the format associated with the request.
*
* @return string The format (null if no content type is present)
* @return string|null The format (null if no content type is present)
*
* @api
*/
public function getContentType()
{
return $this->getFormat($this->server->get('CONTENT_TYPE'));
return $this->getFormat($this->headers->get('CONTENT_TYPE'));
}
/**
@@ -990,7 +1157,11 @@ class Request
*/
public function setDefaultLocale($locale)
{
$this->setPhpDefaultLocale($this->defaultLocale = $locale);
$this->defaultLocale = $locale;
if (null === $this->locale) {
$this->setPhpDefaultLocale($locale);
}
}
/**
@@ -1015,6 +1186,18 @@ class Request
return null === $this->locale ? $this->defaultLocale : $this->locale;
}
/**
* Checks if the request method is of specified type.
*
* @param string $method Uppercase request method (GET, POST etc).
*
* @return Boolean
*/
public function isMethod($method)
{
return $this->getMethod() === strtoupper($method);
}
/**
* Checks whether the method is safe or not.
*
@@ -1030,7 +1213,7 @@ class Request
/**
* Returns the request body content.
*
* @param Boolean $asResource If true, a resource will be returned
* @param Boolean $asResource If true, a resource will be returned
*
* @return string|resource The request body content or a resource to read the body stream.
*/
@@ -1063,6 +1246,9 @@ class Request
return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, PREG_SPLIT_NO_EMPTY);
}
/**
* @return Boolean
*/
public function isNoCache()
{
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
@@ -1071,7 +1257,7 @@ class Request
/**
* Returns the preferred language.
*
* @param array $locales An array of ordered available locales
* @param array $locales An array of ordered available locales
*
* @return string|null The preferred locale
*
@@ -1186,7 +1372,7 @@ class Request
/**
* Splits an Accept-* HTTP header.
*
* @param string $header Header to split
* @param string $header Header to split
*
* @return array Array indexed by the values of the Accept-* header in preferred order
*/
@@ -1197,22 +1383,30 @@ class Request
}
$values = array();
$groups = array();
foreach (array_filter(explode(',', $header)) as $value) {
// Cut off any q-value that might come after a semi-colon
if (preg_match('/;\s*(q=.*$)/', $value, $match)) {
$q = (float) substr(trim($match[1]), 2);
$q = substr(trim($match[1]), 2);
$value = trim(substr($value, 0, -strlen($match[0])));
} else {
$q = 1;
}
if (0 < $q) {
$values[trim($value)] = $q;
}
$groups[$q][] = $value;
}
arsort($values);
reset($values);
krsort($groups);
foreach ($groups as $q => $items) {
$q = (float) $q;
if (0 < $q) {
foreach ($items as $value) {
$values[trim($value)] = $q;
}
}
}
return $values;
}
@@ -1229,8 +1423,11 @@ class Request
{
$requestUri = '';
if ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) {
// check this first so IIS will catch
if ($this->headers->has('X_ORIGINAL_URL') && false !== stripos(PHP_OS, 'WIN')) {
// IIS with Microsoft Rewrite Module
$requestUri = $this->headers->get('X_ORIGINAL_URL');
} elseif ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) {
// IIS with ISAPI_Rewrite
$requestUri = $this->headers->get('X_REWRITE_URL');
} elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') {
// IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
@@ -1238,14 +1435,14 @@ class Request
} elseif ($this->server->has('REQUEST_URI')) {
$requestUri = $this->server->get('REQUEST_URI');
// HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
$schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
$schemeAndHttpHost = $this->getSchemeAndHttpHost();
if (strpos($requestUri, $schemeAndHttpHost) === 0) {
$requestUri = substr($requestUri, strlen($schemeAndHttpHost));
}
} elseif ($this->server->has('ORIG_PATH_INFO')) {
// IIS 5.0, PHP as CGI
$requestUri = $this->server->get('ORIG_PATH_INFO');
if ($this->server->get('QUERY_STRING')) {
if ('' != $this->server->get('QUERY_STRING')) {
$requestUri .= '?'.$this->server->get('QUERY_STRING');
}
}
@@ -1288,14 +1485,14 @@ class Request
// Does the baseUrl have anything in common with the request_uri?
$requestUri = $this->getRequestUri();
if ($baseUrl && 0 === strpos($requestUri, $baseUrl)) {
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
// full $baseUrl matches
return $baseUrl;
return $prefix;
}
if ($baseUrl && 0 === strpos($requestUri, dirname($baseUrl))) {
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, dirname($baseUrl))) {
// directory portion of $baseUrl matches
return rtrim(dirname($baseUrl), '/');
return rtrim($prefix, '/');
}
$truncatedRequestUri = $requestUri;
@@ -1304,7 +1501,7 @@ class Request
}
$basename = basename($baseUrl);
if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
// no match whatsoever; set it blank
return '';
}
@@ -1365,7 +1562,7 @@ class Request
$requestUri = substr($requestUri, 0, $pos);
}
if ((null !== $baseUrl) && (false === ($pathInfo = substr(urldecode($requestUri), strlen(urldecode($baseUrl)))))) {
if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) {
// If substr() returns false then PATH_INFO is set to an empty string
return '/';
} elseif (null === $baseUrl) {
@@ -1378,7 +1575,7 @@ class Request
/**
* Initializes HTTP request formats.
*/
static protected function initializeFormats()
protected static function initializeFormats()
{
static::$formats = array(
'html' => array('text/html', 'application/xhtml+xml'),
@@ -1410,4 +1607,28 @@ class Request
} catch (\Exception $e) {
}
}
/*
* Returns the prefix as encoded in the string when the string starts with
* the given prefix, false otherwise.
*
* @param string $string The urlencoded string
* @param string $prefix The prefix not encoded
*
* @return string|false The prefix as it is encoded in $string, or false
*/
private function getUrlencodedPrefix($string, $prefix)
{
if (0 !== strpos(rawurldecode($string), $prefix)) {
return false;
}
$len = strlen($prefix);
if (preg_match("#^(%[[:xdigit:]]{2}|.){{$len}}#", $string, $match)) {
return $match[0];
}
return false;
}
}