Commit 4ec96cd6 by Qiang Xue

request WIP.

parent a095d383
...@@ -39,24 +39,35 @@ class Request extends Component ...@@ -39,24 +39,35 @@ class Request extends Component
/** /**
* Returns entry script file path. * Returns entry script file path.
* @return string entry script file path (processed w/ realpath()) * @return string entry script file path (processed w/ realpath())
* @throws InvalidConfigException if the entry script file path cannot be determined automatically.
*/ */
public function getScriptFile() public function getScriptFile()
{ {
if ($this->_scriptFile === null) { if ($this->_scriptFile === null) {
$this->_scriptFile = realpath($_SERVER['SCRIPT_FILENAME']); if (isset($_SERVER['SCRIPT_FILENAME'])) {
$this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
} else {
throw new InvalidConfigException('Unable to determine the entry script file path.');
}
} }
return $this->_scriptFile; return $this->_scriptFile;
} }
/** /**
* Sets the entry script file path. * Sets the entry script file path.
* This can be an absolute or relative file path, or a path alias. * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
* Note that you normally do not have to set the script file path * However, for some server configurations, this may not be correct or feasible.
* as [[getScriptFile()]] can determine it based on `$_SERVER['SCRIPT_FILENAME']`. * This setter is provided so that the entry script file path can be manually specified.
* @param string $value the entry script file * @param string $value the entry script file path. This can be either a file path or a path alias.
* @throws InvalidConfigException if the provided entry script file path is invalid.
*/ */
public function setScriptFile($value) public function setScriptFile($value)
{ {
$this->_scriptFile = realpath(\Yii::getAlias($value)); $scriptFile = realpath(\Yii::getAlias($value));
if ($scriptFile !== false && is_file($scriptFile)) {
$this->_scriptFile = $scriptFile;
} else {
throw new InvalidConfigException('Unable to determine the entry script file path.');
}
} }
} }
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
namespace yii\web; namespace yii\web;
use \yii\base\InvalidConfigException; use Yii;
use yii\base\InvalidConfigException;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -32,16 +33,8 @@ class Request extends \yii\base\Request ...@@ -32,16 +33,8 @@ class Request extends \yii\base\Request
*/ */
public $enableCsrfValidation = false; public $enableCsrfValidation = false;
/** /**
* @var string|boolean the name of the POST parameter that is used to indicate if a request is a PUT or DELETE
* request tunneled through POST. If false, it means disabling REST request tunneled through POST.
* Default to '_method'.
* @see getRequestMethod
* @see getRestParams
*/
public $restPostVar = '_method';
/**
* @var string the name of the token used to prevent CSRF. Defaults to 'YII_CSRF_TOKEN'. * @var string the name of the token used to prevent CSRF. Defaults to 'YII_CSRF_TOKEN'.
* This property is effective only when {@link enableCsrfValidation} is true. * This property is used only when [[enableCsrfValidation]] is true.
*/ */
public $csrfTokenName = 'YII_CSRF_TOKEN'; public $csrfTokenName = 'YII_CSRF_TOKEN';
/** /**
...@@ -50,6 +43,14 @@ class Request extends \yii\base\Request ...@@ -50,6 +43,14 @@ class Request extends \yii\base\Request
* This property is effective only when {@link enableCsrfValidation} is true. * This property is effective only when {@link enableCsrfValidation} is true.
*/ */
public $csrfCookie; public $csrfCookie;
/**
* @var string|boolean the name of the POST parameter that is used to indicate if a request is a PUT or DELETE
* request tunneled through POST. If false, it means disabling REST request tunneled through POST.
* Default to '_method'.
* @see getRequestMethod
* @see getRestParams
*/
public $restVar = '_method';
private $_cookies; private $_cookies;
...@@ -109,14 +110,13 @@ class Request extends \yii\base\Request ...@@ -109,14 +110,13 @@ class Request extends \yii\base\Request
*/ */
public function getRequestMethod() public function getRequestMethod()
{ {
if ($this->restPostVar !== false && isset($_POST[$this->restPostVar])) { if ($this->restVar !== false && isset($_POST[$this->restVar])) {
return strtoupper($_POST[$this->restPostVar]); return strtoupper($_POST[$this->restVar]);
} else { } else {
return isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET'; return isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
} }
} }
/** /**
* Returns whether this is a POST request. * Returns whether this is a POST request.
* @return boolean whether this is a POST request. * @return boolean whether this is a POST request.
...@@ -154,7 +154,7 @@ class Request extends \yii\base\Request ...@@ -154,7 +154,7 @@ class Request extends \yii\base\Request
} }
/** /**
* Returns whether this is an Adobe Flash or Adobe Flex request. * Returns whether this is an Adobe Flash or Flex request.
* @return boolean whether this is an Adobe Flash or Adobe Flex request. * @return boolean whether this is an Adobe Flash or Adobe Flex request.
*/ */
public function getIsFlashRequest() public function getIsFlashRequest()
...@@ -173,7 +173,7 @@ class Request extends \yii\base\Request ...@@ -173,7 +173,7 @@ class Request extends \yii\base\Request
public function getRestParams() public function getRestParams()
{ {
if ($this->_restParams === null) { if ($this->_restParams === null) {
if ($this->restPostVar !== false && isset($_POST[$this->restPostVar])) { if ($this->restVar !== false && isset($_POST[$this->restVar])) {
$this->_restParams = $_POST; $this->_restParams = $_POST;
} else { } else {
$this->_restParams = array(); $this->_restParams = array();
...@@ -197,21 +197,6 @@ class Request extends \yii\base\Request ...@@ -197,21 +197,6 @@ class Request extends \yii\base\Request
} }
/** /**
* Returns the named GET or POST parameter value.
* If the GET or POST parameter does not exist, the second parameter to this method will be returned.
* If both GET and POST contains such a named parameter, the GET parameter takes precedence.
* @param string $name the GET parameter name
* @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
* @return mixed the GET parameter value
* @see getQuery
* @see getPost
*/
public function getParam($name, $defaultValue = null)
{
return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $defaultValue);
}
/**
* Returns the named RESTful parameter value. * Returns the named RESTful parameter value.
* @param string $name the parameter name * @param string $name the parameter name
* @param mixed $defaultValue the default parameter value if the parameter does not exist. * @param mixed $defaultValue the default parameter value if the parameter does not exist.
...@@ -230,9 +215,8 @@ class Request extends \yii\base\Request ...@@ -230,9 +215,8 @@ class Request extends \yii\base\Request
* @param mixed $defaultValue the default parameter value if the GET parameter does not exist. * @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
* @return mixed the GET parameter value * @return mixed the GET parameter value
* @see getPost * @see getPost
* @see getParam
*/ */
public function getQuery($name, $defaultValue = null) public function getParam($name, $defaultValue = null)
{ {
return isset($_GET[$name]) ? $_GET[$name] : $defaultValue; return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
} }
...@@ -244,7 +228,6 @@ class Request extends \yii\base\Request ...@@ -244,7 +228,6 @@ class Request extends \yii\base\Request
* @param mixed $defaultValue the default parameter value if the POST parameter does not exist. * @param mixed $defaultValue the default parameter value if the POST parameter does not exist.
* @return mixed the POST parameter value * @return mixed the POST parameter value
* @see getParam * @see getParam
* @see getQuery
*/ */
public function getPost($name, $defaultValue = null) public function getPost($name, $defaultValue = null)
{ {
...@@ -273,16 +256,6 @@ class Request extends \yii\base\Request ...@@ -273,16 +256,6 @@ class Request extends \yii\base\Request
return $this->getIsPutRequest() ? $this->getRestParam($name, $defaultValue) : null; return $this->getIsPutRequest() ? $this->getRestParam($name, $defaultValue) : null;
} }
/**
* Returns the currently requested URL.
* This is the same as [[requestUri]].
* @return string part of the request URL after the host info.
*/
public function getUrl()
{
return $this->getRequestUri();
}
private $_hostInfo; private $_hostInfo;
/** /**
...@@ -398,7 +371,7 @@ class Request extends \yii\base\Request ...@@ -398,7 +371,7 @@ class Request extends \yii\base\Request
* A path info refers to the part that is after the entry script and before the question mark (query string). * A path info refers to the part that is after the entry script and before the question mark (query string).
* The starting and ending slashes are both removed. * The starting and ending slashes are both removed.
* @return string part of the request URL that is after the entry script and before the question mark. * @return string part of the request URL that is after the entry script and before the question mark.
* Note, the returned path info is decoded. * Note, the returned path info is already URL-decoded.
* @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration
*/ */
public function getPathInfo() public function getPathInfo()
...@@ -476,10 +449,20 @@ class Request extends \yii\base\Request ...@@ -476,10 +449,20 @@ class Request extends \yii\base\Request
} }
} }
/**
* Returns the currently requested URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[requestUri]].
* @return string the currently requested URL.
*/
public function getUrl()
{
return $this->getHostInfo() . $this->getRequestUri();
}
private $_requestUri; private $_requestUri;
/** /**
* Returns the request URI portion for the currently requested URL. * Returns the portion after [[hostInfo]] for the currently requested URL.
* This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any.
* The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework.
* @return string the request URI portion for the currently requested URL. * @return string the request URI portion for the currently requested URL.
...@@ -491,11 +474,21 @@ class Request extends \yii\base\Request ...@@ -491,11 +474,21 @@ class Request extends \yii\base\Request
if ($this->_requestUri === null) { if ($this->_requestUri === null) {
$this->_requestUri = $this->resolveRequestUri(); $this->_requestUri = $this->resolveRequestUri();
} }
return $this->_requestUri; return $this->_requestUri;
} }
/** /**
* Sets the currently requested URI.
* The URI must refer to the portion that is after [[hostInfo]].
* Note that the URI should be URL-encoded.
* @param string $value the request URI to be set
*/
public function setRequestUri($value)
{
$this->_requestUri = $value;
}
/**
* Resolves the request URI portion for the currently requested URL. * Resolves the request URI portion for the currently requested URL.
* This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any.
* The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework.
...@@ -509,11 +502,7 @@ class Request extends \yii\base\Request ...@@ -509,11 +502,7 @@ class Request extends \yii\base\Request
$requestUri = $_SERVER['HTTP_X_REWRITE_URL']; $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
} elseif (isset($_SERVER['REQUEST_URI'])) { } elseif (isset($_SERVER['REQUEST_URI'])) {
$requestUri = $_SERVER['REQUEST_URI']; $requestUri = $_SERVER['REQUEST_URI'];
if (!empty($_SERVER['HTTP_HOST'])) { if ($requestUri !== '' && $requestUri[0] !== '/') {
if (strpos($requestUri, $_SERVER['HTTP_HOST']) !== false) {
$requestUri = preg_replace('/^\w+:\/\/[^\/]+/', '', $requestUri);
}
} else {
$requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri); $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri);
} }
} elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI
...@@ -599,49 +588,6 @@ class Request extends \yii\base\Request ...@@ -599,49 +588,6 @@ class Request extends \yii\base\Request
return isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null; return isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null;
} }
private $_scriptFile;
/**
* Returns entry script file path.
* @return string entry script file path (processed w/ realpath())
* @throws InvalidConfigException if the entry script file path cannot be determined automatically.
*/
public function getScriptFile()
{
if ($this->_scriptFile === null) {
$this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
}
return $this->_scriptFile;
}
/**
* Sets the entry script file path.
* The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
* However, in some server configuration, this may not be correct or feasible.
* This setter is provided so that the entry script file path can be manually specified.
* @param string $value the entry script file path
* @throws InvalidConfigException if the provided entry script file path is invalid.
*/
public function setScriptFile($value)
{
$this->_scriptFile = realpath($value);
if ($this->_scriptFile === false || !is_file($this->_scriptFile)) {
throw new InvalidConfigException('Unable to determine the entry script file path.');
}
}
/**
* Returns information about the capabilities of user browser.
* @param string $userAgent the user agent to be analyzed. Defaults to null, meaning using the
* current User-Agent HTTP header information.
* @return array user browser capabilities.
* @see http://www.php.net/manual/en/function.get-browser.php
*/
public function getBrowser($userAgent = null)
{
return get_browser($userAgent, true);
}
/** /**
* Returns user browser accept types, null if not present. * Returns user browser accept types, null if not present.
* @return string user browser accept types, null if not present * @return string user browser accept types, null if not present
......
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