Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 88 | class Request extends \yii\base\Request |
||
| 89 | { |
||
| 90 | /** |
||
| 91 | * The name of the HTTP header for sending CSRF token. |
||
| 92 | */ |
||
| 93 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
| 94 | /** |
||
| 95 | * The length of the CSRF token mask. |
||
| 96 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
| 97 | */ |
||
| 98 | const CSRF_MASK_LENGTH = 8; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
| 102 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
| 103 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
| 104 | * |
||
| 105 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
| 106 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
| 107 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
| 108 | * |
||
| 109 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
| 110 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
| 111 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
| 112 | * |
||
| 113 | * @see Controller::enableCsrfValidation |
||
| 114 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
| 115 | */ |
||
| 116 | public $enableCsrfValidation = true; |
||
| 117 | /** |
||
| 118 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
| 119 | * This property is used only when [[enableCsrfValidation]] is true. |
||
| 120 | */ |
||
| 121 | public $csrfParam = '_csrf'; |
||
| 122 | /** |
||
| 123 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
| 124 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
| 125 | */ |
||
| 126 | public $csrfCookie = ['httpOnly' => true]; |
||
| 127 | /** |
||
| 128 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
| 129 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
| 130 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
| 131 | */ |
||
| 132 | public $enableCsrfCookie = true; |
||
| 133 | /** |
||
| 134 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
| 135 | */ |
||
| 136 | public $enableCookieValidation = true; |
||
| 137 | /** |
||
| 138 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
| 139 | */ |
||
| 140 | public $cookieValidationKey; |
||
| 141 | /** |
||
| 142 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
| 143 | * request tunneled through POST. Defaults to '_method'. |
||
| 144 | * @see getMethod() |
||
| 145 | * @see getBodyParams() |
||
| 146 | */ |
||
| 147 | public $methodParam = '_method'; |
||
| 148 | /** |
||
| 149 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
| 150 | * The array keys are the request `Content-Types`, and the array values are the |
||
| 151 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
| 152 | * A parser must implement the [[RequestParserInterface]]. |
||
| 153 | * |
||
| 154 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
| 155 | * |
||
| 156 | * ``` |
||
| 157 | * [ |
||
| 158 | * 'application/json' => 'yii\web\JsonParser', |
||
| 159 | * ] |
||
| 160 | * ``` |
||
| 161 | * |
||
| 162 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
| 163 | * This one will be used as a fallback in case no other types match. |
||
| 164 | * |
||
| 165 | * @see getBodyParams() |
||
| 166 | */ |
||
| 167 | public $parsers = []; |
||
| 168 | /** |
||
| 169 | * @var array the configuration for trusted security related headers. |
||
| 170 | * |
||
| 171 | * An array key is a regular expression for matching a client (ususally a proxy) by |
||
| 172 | * host name or ip address. |
||
| 173 | * |
||
| 174 | * An array value is a list of headers to trust. These will be matched against |
||
| 175 | * [[secureHeaders]] to determine which headers are allowed to be sent by a specified host. |
||
| 176 | * The case of the header names must be the same as specified in [[secureHeaders]]. |
||
| 177 | * |
||
| 178 | * To specify a host for which you trust all headers listed in [[secureHeaders]] you can just specify |
||
| 179 | * the regular expression as the array value. |
||
| 180 | * |
||
| 181 | * For example, to trust all headers listed in [[secureHeaders]] |
||
| 182 | * from domains ending in '.trusted.com' use the following: |
||
| 183 | * |
||
| 184 | * ```php |
||
| 185 | * [ |
||
| 186 | * '/\.trusted\.com$/', |
||
| 187 | * ] |
||
| 188 | * ``` |
||
| 189 | * |
||
| 190 | * To trust just the `x-forwarded-for` header from domains ending in `.partial.com` use: |
||
| 191 | * |
||
| 192 | * ``` |
||
| 193 | * [ |
||
| 194 | * '/\.partial\.com$/' => ['X-Forwarded-For'] |
||
| 195 | * ] |
||
| 196 | * ``` |
||
| 197 | * |
||
| 198 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||
| 199 | * Matches are tried in order and searching is stopped when a host or IP matches. |
||
| 200 | * @see $secureHeaders |
||
| 201 | * @since 2.0.13 |
||
| 202 | */ |
||
| 203 | public $trustedHosts = []; |
||
| 204 | /** |
||
| 205 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||
| 206 | * These headers will be filtered unless explicitly allowed in [[$trustedHosts]]. |
||
| 207 | * The match of header names is case-insensitive. |
||
| 208 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
| 209 | * @see $trustedHosts |
||
| 210 | * @since 2.0.13 |
||
| 211 | */ |
||
| 212 | public $secureHeaders = [ |
||
| 213 | 'X-Forwarded-For', |
||
| 214 | 'X-Forwarded-Host', |
||
| 215 | 'X-Forwarded-Proto', |
||
| 216 | 'Front-End-Https', |
||
| 217 | ]; |
||
| 218 | /** |
||
| 219 | * @var string[] List of headers where proxies store the real client IP. |
||
| 220 | * It's not advisable to put insecure headers here. |
||
| 221 | * The match of header names is case-insensitive. |
||
| 222 | * @see $trustedHosts |
||
| 223 | * @see $secureHeaders |
||
| 224 | * @since 2.0.13 |
||
| 225 | */ |
||
| 226 | public $ipHeaders = [ |
||
| 227 | 'X-Forwarded-For', |
||
| 228 | ]; |
||
| 229 | /** |
||
| 230 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||
| 231 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||
| 232 | * The match of header names and values is case-insensitive. |
||
| 233 | * It's not advisable to put insecure headers here. |
||
| 234 | * @see $trustedHosts |
||
| 235 | * @see $secureHeaders |
||
| 236 | * @since 2.0.13 |
||
| 237 | */ |
||
| 238 | public $secureProtocolHeaders = [ |
||
| 239 | 'X-Forwarded-Proto' => ['https'], |
||
| 240 | 'Front-End-Https' => ['on'], |
||
| 241 | ]; |
||
| 242 | /** |
||
| 243 | * @var CookieCollection Collection of request cookies. |
||
| 244 | */ |
||
| 245 | private $_cookies; |
||
| 246 | /** |
||
| 247 | * @var HeaderCollection Collection of request headers. |
||
| 248 | */ |
||
| 249 | private $_headers; |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Resolves the current request into a route and the associated parameters. |
||
| 254 | * @return array the first element is the route, and the second is the associated parameters. |
||
| 255 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
| 256 | */ |
||
| 257 | 1 | public function resolve() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Filters headers according to the [[trustedHosts]]. |
||
| 275 | * @param HeaderCollection $headerCollection |
||
| 276 | * @since 2.0.13 |
||
| 277 | */ |
||
| 278 | 102 | protected function filterHeaders(HeaderCollection $headerCollection) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Returns the header collection. |
||
| 309 | * The header collection contains incoming HTTP headers. |
||
| 310 | * @return HeaderCollection the header collection |
||
| 311 | */ |
||
| 312 | 102 | public function getHeaders() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
||
| 342 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||
| 343 | * The value returned is turned into upper case. |
||
| 344 | */ |
||
| 345 | 22 | public function getMethod() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Returns whether this is a GET request. |
||
| 364 | * @return bool whether this is a GET request. |
||
| 365 | */ |
||
| 366 | 2 | public function getIsGet() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns whether this is an OPTIONS request. |
||
| 373 | * @return bool whether this is a OPTIONS request. |
||
| 374 | */ |
||
| 375 | public function getIsOptions() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns whether this is a HEAD request. |
||
| 382 | * @return bool whether this is a HEAD request. |
||
| 383 | */ |
||
| 384 | 9 | public function getIsHead() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Returns whether this is a POST request. |
||
| 391 | * @return bool whether this is a POST request. |
||
| 392 | */ |
||
| 393 | public function getIsPost() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns whether this is a DELETE request. |
||
| 400 | * @return bool whether this is a DELETE request. |
||
| 401 | */ |
||
| 402 | public function getIsDelete() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns whether this is a PUT request. |
||
| 409 | * @return bool whether this is a PUT request. |
||
| 410 | */ |
||
| 411 | public function getIsPut() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Returns whether this is a PATCH request. |
||
| 418 | * @return bool whether this is a PATCH request. |
||
| 419 | */ |
||
| 420 | public function getIsPatch() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
| 427 | * |
||
| 428 | * Note that jQuery doesn't set the header in case of cross domain |
||
| 429 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
| 430 | * |
||
| 431 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
| 432 | */ |
||
| 433 | 14 | public function getIsAjax() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Returns whether this is a PJAX request |
||
| 440 | * @return bool whether this is a PJAX request |
||
| 441 | */ |
||
| 442 | 3 | public function getIsPjax() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Returns whether this is an Adobe Flash or Flex request. |
||
| 449 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
| 450 | */ |
||
| 451 | public function getIsFlash() |
||
| 457 | |||
| 458 | private $_rawBody; |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Returns the raw HTTP request body. |
||
| 462 | * @return string the request body |
||
| 463 | */ |
||
| 464 | public function getRawBody() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
| 475 | * @param string $rawBody the request body |
||
| 476 | */ |
||
| 477 | public function setRawBody($rawBody) |
||
| 481 | |||
| 482 | private $_bodyParams; |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Returns the request parameters given in the request body. |
||
| 486 | * |
||
| 487 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
| 488 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
| 489 | * to parse the [[rawBody|request body]]. |
||
| 490 | * @return array the request parameters given in the request body. |
||
| 491 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
| 492 | * @see getMethod() |
||
| 493 | * @see getBodyParam() |
||
| 494 | * @see setBodyParams() |
||
| 495 | */ |
||
| 496 | 3 | public function getBodyParams() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Sets the request body parameters. |
||
| 539 | * @param array $values the request body parameters (name-value pairs) |
||
| 540 | * @see getBodyParam() |
||
| 541 | * @see getBodyParams() |
||
| 542 | */ |
||
| 543 | 2 | public function setBodyParams($values) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Returns the named request body parameter value. |
||
| 550 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
| 551 | * @param string $name the parameter name |
||
| 552 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 553 | * @return mixed the parameter value |
||
| 554 | * @see getBodyParams() |
||
| 555 | * @see setBodyParams() |
||
| 556 | */ |
||
| 557 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
| 566 | * |
||
| 567 | * @param string $name the parameter name |
||
| 568 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 569 | * @return array|mixed |
||
| 570 | */ |
||
| 571 | public function post($name = null, $defaultValue = null) |
||
| 579 | |||
| 580 | private $_queryParams; |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns the request parameters given in the [[queryString]]. |
||
| 584 | * |
||
| 585 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
| 586 | * @return array the request GET parameter values. |
||
| 587 | * @see setQueryParams() |
||
| 588 | */ |
||
| 589 | 26 | public function getQueryParams() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Sets the request [[queryString]] parameters. |
||
| 600 | * @param array $values the request query parameters (name-value pairs) |
||
| 601 | * @see getQueryParam() |
||
| 602 | * @see getQueryParams() |
||
| 603 | */ |
||
| 604 | 7 | public function setQueryParams($values) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
| 611 | * |
||
| 612 | * @param string $name the parameter name |
||
| 613 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 614 | * @return array|mixed |
||
| 615 | */ |
||
| 616 | 14 | public function get($name = null, $defaultValue = null) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Returns the named GET parameter value. |
||
| 627 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
| 628 | * @param string $name the GET parameter name. |
||
| 629 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
| 630 | * @return mixed the GET parameter value |
||
| 631 | * @see getBodyParam() |
||
| 632 | */ |
||
| 633 | 17 | public function getQueryParam($name, $defaultValue = null) |
|
| 639 | |||
| 640 | private $_hostInfo; |
||
| 641 | private $_hostName; |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Returns the schema and host part of the current request URL. |
||
| 645 | * |
||
| 646 | * The returned URL does not have an ending slash. |
||
| 647 | * |
||
| 648 | * By default this value is based on the user request information. This method will |
||
| 649 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
| 650 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
| 651 | * for more information on these variables. |
||
| 652 | * |
||
| 653 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
| 654 | * |
||
| 655 | * > Warning: Dependent on the server configuration this information may not be |
||
| 656 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
| 657 | * > If the webserver is configured to serve the same site independent of the value of |
||
| 658 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
| 659 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
| 660 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
| 661 | * > application level in order to protect against such kind of attack. |
||
| 662 | * |
||
| 663 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
| 664 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 665 | * See [[getHostInfo()]] for security related notes on this property. |
||
| 666 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
| 667 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 668 | * @see setHostInfo() |
||
| 669 | */ |
||
| 670 | 24 | public function getHostInfo() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Sets the schema and host part of the application URL. |
||
| 691 | * This setter is provided in case the schema and hostname cannot be determined |
||
| 692 | * on certain Web servers. |
||
| 693 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
| 694 | * @see getHostInfo() for security related notes on this property. |
||
| 695 | */ |
||
| 696 | 57 | public function setHostInfo($value) |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Returns the host part of the current request URL. |
||
| 704 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
| 705 | * |
||
| 706 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
| 707 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
| 708 | * |
||
| 709 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
| 710 | * @see getHostInfo() |
||
| 711 | * @since 2.0.10 |
||
| 712 | */ |
||
| 713 | 11 | public function getHostName() |
|
| 721 | |||
| 722 | private $_baseUrl; |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Returns the relative URL for the application. |
||
| 726 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
| 727 | * and the ending slashes are removed. |
||
| 728 | * @return string the relative URL for the application |
||
| 729 | * @see setScriptUrl() |
||
| 730 | */ |
||
| 731 | 250 | public function getBaseUrl() |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Sets the relative URL for the application. |
||
| 742 | * By default the URL is determined based on the entry script URL. |
||
| 743 | * This setter is provided in case you want to change this behavior. |
||
| 744 | * @param string $value the relative URL for the application |
||
| 745 | */ |
||
| 746 | 1 | public function setBaseUrl($value) |
|
| 750 | |||
| 751 | private $_scriptUrl; |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Returns the relative URL of the entry script. |
||
| 755 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 756 | * @return string the relative URL of the entry script. |
||
| 757 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
| 758 | */ |
||
| 759 | 251 | public function getScriptUrl() |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Sets the relative URL for the application entry script. |
||
| 784 | * This setter is provided in case the entry script URL cannot be determined |
||
| 785 | * on certain Web servers. |
||
| 786 | * @param string $value the relative URL for the application entry script. |
||
| 787 | */ |
||
| 788 | 261 | public function setScriptUrl($value) |
|
| 792 | |||
| 793 | private $_scriptFile; |
||
|
|
|||
| 794 | |||
| 795 | /** |
||
| 796 | * Returns the entry script file path. |
||
| 797 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
| 798 | * @return string the entry script file path |
||
| 799 | * @throws InvalidConfigException |
||
| 800 | */ |
||
| 801 | 252 | public function getScriptFile() |
|
| 813 | |||
| 814 | /** |
||
| 815 | * Sets the entry script file path. |
||
| 816 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
| 817 | * If your server configuration does not return the correct value, you may configure |
||
| 818 | * this property to make it right. |
||
| 819 | * @param string $value the entry script file path. |
||
| 820 | */ |
||
| 821 | 230 | public function setScriptFile($value) |
|
| 825 | |||
| 826 | private $_pathInfo; |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Returns the path info of the currently requested URL. |
||
| 830 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 831 | * The starting and ending slashes are both removed. |
||
| 832 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 833 | * Note, the returned path info is already URL-decoded. |
||
| 834 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 835 | */ |
||
| 836 | 18 | public function getPathInfo() |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Sets the path info of the current request. |
||
| 847 | * This method is mainly provided for testing purpose. |
||
| 848 | * @param string $value the path info of the current request |
||
| 849 | */ |
||
| 850 | 19 | public function setPathInfo($value) |
|
| 854 | |||
| 855 | /** |
||
| 856 | * Resolves the path info part of the currently requested URL. |
||
| 857 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 858 | * The starting slashes are both removed (ending slashes will be kept). |
||
| 859 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 860 | * Note, the returned path info is decoded. |
||
| 861 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 862 | */ |
||
| 863 | protected function resolvePathInfo() |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Returns the currently requested absolute URL. |
||
| 910 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
| 911 | * @return string the currently requested absolute URL. |
||
| 912 | */ |
||
| 913 | public function getAbsoluteUrl() |
||
| 917 | |||
| 918 | private $_url; |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Returns the currently requested relative URL. |
||
| 922 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
| 923 | * It includes the [[queryString]] part if any. |
||
| 924 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
| 925 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
| 926 | */ |
||
| 927 | 11 | public function getUrl() |
|
| 935 | |||
| 936 | /** |
||
| 937 | * Sets the currently requested relative URL. |
||
| 938 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
| 939 | * Note that the URI should be URL-encoded. |
||
| 940 | * @param string $value the request URI to be set |
||
| 941 | */ |
||
| 942 | 24 | public function setUrl($value) |
|
| 946 | |||
| 947 | /** |
||
| 948 | * Resolves the request URI portion for the currently requested URL. |
||
| 949 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
| 950 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 951 | * @return string|bool the request URI portion for the currently requested URL. |
||
| 952 | * Note that the URI returned may be URL-encoded depending on the client. |
||
| 953 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
| 954 | */ |
||
| 955 | 3 | protected function resolveRequestUri() |
|
| 975 | |||
| 976 | /** |
||
| 977 | * Returns part of the request URL that is after the question mark. |
||
| 978 | * @return string part of the request URL that is after the question mark |
||
| 979 | */ |
||
| 980 | public function getQueryString() |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Return if the request is sent via secure channel (https). |
||
| 987 | * @return bool if the request is sent via secure channel (https) |
||
| 988 | */ |
||
| 989 | 37 | public function getIsSecureConnection() |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Returns the server name. |
||
| 1008 | * @return string server name, null if not available |
||
| 1009 | */ |
||
| 1010 | 1 | public function getServerName() |
|
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Returns the server port number. |
||
| 1017 | * @return int|null server port number, null if not available |
||
| 1018 | */ |
||
| 1019 | 1 | public function getServerPort() |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Returns the URL referrer. |
||
| 1026 | * @return string|null URL referrer, null if not available |
||
| 1027 | */ |
||
| 1028 | public function getReferrer() |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Returns the URL origin of a CORS request. |
||
| 1035 | * |
||
| 1036 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
| 1037 | * |
||
| 1038 | * Note that the origin request header indicates where a fetch originates from. |
||
| 1039 | * It doesn't include any path information, but only the server name. |
||
| 1040 | * It is sent with a CORS requests, as well as with POST requests. |
||
| 1041 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
| 1042 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
| 1043 | * |
||
| 1044 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
| 1045 | * @see getHeaders() |
||
| 1046 | * @since 2.0.13 |
||
| 1047 | */ |
||
| 1048 | 1 | public function getOrigin() |
|
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Returns the user agent. |
||
| 1055 | * @return string|null user agent, null if not available |
||
| 1056 | */ |
||
| 1057 | public function getUserAgent() |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Returns the user IP address. |
||
| 1064 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
| 1065 | * @return string|null user IP address, null if not available |
||
| 1066 | */ |
||
| 1067 | 32 | public function getUserIP() |
|
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Returns the user host name. |
||
| 1079 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
| 1080 | * @return string|null user host name, null if not available |
||
| 1081 | */ |
||
| 1082 | public function getUserHost() |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Returns the IP on the other end of this connection. |
||
| 1094 | * This is always the next hop, any headers are ignored. |
||
| 1095 | * @return string|null remote IP address, `null` if not available. |
||
| 1096 | * @since 2.0.13 |
||
| 1097 | */ |
||
| 1098 | 47 | public function getRemoteIP() |
|
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Returns the host name of the other end of this connection. |
||
| 1105 | * This is always the next hop, any headers are ignored. |
||
| 1106 | * @return string|null remote host name, `null` if not available |
||
| 1107 | * @see getUserHost() |
||
| 1108 | * @see getRemoteIP() |
||
| 1109 | * @since 2.0.13 |
||
| 1110 | */ |
||
| 1111 | 19 | public function getRemoteHost() |
|
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
| 1118 | */ |
||
| 1119 | 10 | public function getAuthUser() |
|
| 1123 | |||
| 1124 | /** |
||
| 1125 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
| 1126 | */ |
||
| 1127 | 10 | public function getAuthPassword() |
|
| 1131 | |||
| 1132 | private $_port; |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Returns the port to use for insecure requests. |
||
| 1136 | * Defaults to 80, or the port specified by the server if the current |
||
| 1137 | * request is insecure. |
||
| 1138 | * @return int port number for insecure requests. |
||
| 1139 | * @see setPort() |
||
| 1140 | */ |
||
| 1141 | public function getPort() |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Sets the port to use for insecure requests. |
||
| 1152 | * This setter is provided in case a custom port is necessary for certain |
||
| 1153 | * server configurations. |
||
| 1154 | * @param int $value port number. |
||
| 1155 | */ |
||
| 1156 | public function setPort($value) |
||
| 1163 | |||
| 1164 | private $_securePort; |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Returns the port to use for secure requests. |
||
| 1168 | * Defaults to 443, or the port specified by the server if the current |
||
| 1169 | * request is secure. |
||
| 1170 | * @return int port number for secure requests. |
||
| 1171 | * @see setSecurePort() |
||
| 1172 | */ |
||
| 1173 | public function getSecurePort() |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Sets the port to use for secure requests. |
||
| 1184 | * This setter is provided in case a custom port is necessary for certain |
||
| 1185 | * server configurations. |
||
| 1186 | * @param int $value port number. |
||
| 1187 | */ |
||
| 1188 | public function setSecurePort($value) |
||
| 1195 | |||
| 1196 | private $_contentTypes; |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Returns the content types acceptable by the end user. |
||
| 1200 | * This is determined by the `Accept` HTTP header. For example, |
||
| 1201 | * |
||
| 1202 | * ```php |
||
| 1203 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1204 | * $types = $request->getAcceptableContentTypes(); |
||
| 1205 | * print_r($types); |
||
| 1206 | * // displays: |
||
| 1207 | * // [ |
||
| 1208 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1209 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1210 | * // 'text/plain' => ['q' => 0.5], |
||
| 1211 | * // ] |
||
| 1212 | * ``` |
||
| 1213 | * |
||
| 1214 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
| 1215 | * will be returned first. The array keys are the content types, while the array values |
||
| 1216 | * are the corresponding quality score and other parameters as given in the header. |
||
| 1217 | */ |
||
| 1218 | 2 | public function getAcceptableContentTypes() |
|
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Sets the acceptable content types. |
||
| 1233 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
| 1234 | * @param array $value the content types that are acceptable by the end user. They should |
||
| 1235 | * be ordered by the preference level. |
||
| 1236 | * @see getAcceptableContentTypes() |
||
| 1237 | * @see parseAcceptHeader() |
||
| 1238 | */ |
||
| 1239 | public function setAcceptableContentTypes($value) |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Returns request content-type |
||
| 1246 | * The Content-Type header field indicates the MIME type of the data |
||
| 1247 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
| 1248 | * media type that would have been sent had the request been a GET. |
||
| 1249 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
| 1250 | * @return string request content-type. Null is returned if this information is not available. |
||
| 1251 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
| 1252 | * HTTP 1.1 header field definitions |
||
| 1253 | */ |
||
| 1254 | public function getContentType() |
||
| 1263 | |||
| 1264 | private $_languages; |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * Returns the languages acceptable by the end user. |
||
| 1268 | * This is determined by the `Accept-Language` HTTP header. |
||
| 1269 | * @return array the languages ordered by the preference level. The first element |
||
| 1270 | * represents the most preferred language. |
||
| 1271 | */ |
||
| 1272 | 1 | public function getAcceptableLanguages() |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * @param array $value the languages that are acceptable by the end user. They should |
||
| 1287 | * be ordered by the preference level. |
||
| 1288 | */ |
||
| 1289 | 1 | public function setAcceptableLanguages($value) |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
| 1296 | * |
||
| 1297 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
| 1298 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
| 1299 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
| 1300 | * values with the highest quality scores will be returned first. For example, |
||
| 1301 | * |
||
| 1302 | * ```php |
||
| 1303 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1304 | * $accepts = $request->parseAcceptHeader($header); |
||
| 1305 | * print_r($accepts); |
||
| 1306 | * // displays: |
||
| 1307 | * // [ |
||
| 1308 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1309 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1310 | * // 'text/plain' => ['q' => 0.5], |
||
| 1311 | * // ] |
||
| 1312 | * ``` |
||
| 1313 | * |
||
| 1314 | * @param string $header the header to be parsed |
||
| 1315 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
| 1316 | * will be returned first. |
||
| 1317 | */ |
||
| 1318 | 3 | public function parseAcceptHeader($header) |
|
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Returns the user-preferred language that should be used by this application. |
||
| 1388 | * The language resolution is based on the user preferred languages and the languages |
||
| 1389 | * supported by the application. The method will try to find the best match. |
||
| 1390 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
| 1391 | * application language will be returned without further processing. |
||
| 1392 | * @return string the language that the application should use. |
||
| 1393 | */ |
||
| 1394 | 1 | public function getPreferredLanguage(array $languages = []) |
|
| 1416 | |||
| 1417 | /** |
||
| 1418 | * Gets the Etags. |
||
| 1419 | * |
||
| 1420 | * @return array The entity tags |
||
| 1421 | */ |
||
| 1422 | public function getETags() |
||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Returns the cookie collection. |
||
| 1433 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
| 1434 | * |
||
| 1435 | * ```php |
||
| 1436 | * $cookie = $request->cookies['name'] |
||
| 1437 | * if ($cookie !== null) { |
||
| 1438 | * $value = $cookie->value; |
||
| 1439 | * } |
||
| 1440 | * |
||
| 1441 | * // alternatively |
||
| 1442 | * $value = $request->cookies->getValue('name'); |
||
| 1443 | * ``` |
||
| 1444 | * |
||
| 1445 | * @return CookieCollection the cookie collection. |
||
| 1446 | */ |
||
| 1447 | 31 | public function getCookies() |
|
| 1457 | |||
| 1458 | /** |
||
| 1459 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
| 1460 | * @return array the cookies obtained from request |
||
| 1461 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
| 1462 | */ |
||
| 1463 | 31 | protected function loadCookies() |
|
| 1499 | |||
| 1500 | private $_csrfToken; |
||
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Returns the token used to perform CSRF validation. |
||
| 1504 | * |
||
| 1505 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
| 1506 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
| 1507 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
| 1508 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
| 1509 | * @return string the token used to perform CSRF validation. |
||
| 1510 | */ |
||
| 1511 | 35 | public function getCsrfToken($regenerate = false) |
|
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Loads the CSRF token from cookie or session. |
||
| 1525 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
| 1526 | * does not have CSRF token. |
||
| 1527 | */ |
||
| 1528 | 35 | protected function loadCsrfToken() |
|
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Generates an unmasked random token used to perform CSRF validation. |
||
| 1538 | * @return string the random token for CSRF validation. |
||
| 1539 | */ |
||
| 1540 | 33 | protected function generateCsrfToken() |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
| 1554 | */ |
||
| 1555 | 3 | public function getCsrfTokenFromHeader() |
|
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Creates a cookie with a randomly generated CSRF token. |
||
| 1562 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
| 1563 | * @param string $token the CSRF token |
||
| 1564 | * @return Cookie the generated cookie |
||
| 1565 | * @see enableCsrfValidation |
||
| 1566 | */ |
||
| 1567 | 31 | protected function createCsrfCookie($token) |
|
| 1574 | |||
| 1575 | /** |
||
| 1576 | * Performs the CSRF validation. |
||
| 1577 | * |
||
| 1578 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
| 1579 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
| 1580 | * |
||
| 1581 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
| 1582 | * is among GET, HEAD or OPTIONS. |
||
| 1583 | * |
||
| 1584 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
| 1585 | * the [[csrfParam]] POST field or HTTP header. |
||
| 1586 | * This parameter is available since version 2.0.4. |
||
| 1587 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
| 1588 | */ |
||
| 1589 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Validates CSRF token |
||
| 1609 | * |
||
| 1610 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
| 1611 | * @param string $trueToken The masked true token. |
||
| 1612 | * @return bool |
||
| 1613 | */ |
||
| 1614 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
| 1624 | } |
||
| 1625 |