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 |
||
| 89 | class Request extends \yii\base\Request |
||
| 90 | { |
||
| 91 | /** |
||
| 92 | * The name of the HTTP header for sending CSRF token. |
||
| 93 | */ |
||
| 94 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
| 95 | /** |
||
| 96 | * The length of the CSRF token mask. |
||
| 97 | * @deprecated 2.0.12 The mask length is now equal to the token length. |
||
| 98 | */ |
||
| 99 | const CSRF_MASK_LENGTH = 8; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
| 103 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
| 104 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
| 105 | * |
||
| 106 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
| 107 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
| 108 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
| 109 | * |
||
| 110 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
| 111 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
| 112 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
| 113 | * |
||
| 114 | * @see Controller::enableCsrfValidation |
||
| 115 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
| 116 | */ |
||
| 117 | public $enableCsrfValidation = true; |
||
| 118 | /** |
||
| 119 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
| 120 | * This property is used only when [[enableCsrfValidation]] is true. |
||
| 121 | */ |
||
| 122 | public $csrfParam = '_csrf'; |
||
| 123 | /** |
||
| 124 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
| 125 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
| 126 | */ |
||
| 127 | public $csrfCookie = ['httpOnly' => true]; |
||
| 128 | /** |
||
| 129 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
| 130 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
| 131 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
| 132 | */ |
||
| 133 | public $enableCsrfCookie = true; |
||
| 134 | /** |
||
| 135 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
| 136 | */ |
||
| 137 | public $enableCookieValidation = true; |
||
| 138 | /** |
||
| 139 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
| 140 | */ |
||
| 141 | public $cookieValidationKey; |
||
| 142 | /** |
||
| 143 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
| 144 | * request tunneled through POST. Defaults to '_method'. |
||
| 145 | * @see getMethod() |
||
| 146 | * @see getBodyParams() |
||
| 147 | */ |
||
| 148 | public $methodParam = '_method'; |
||
| 149 | /** |
||
| 150 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
| 151 | * The array keys are the request `Content-Types`, and the array values are the |
||
| 152 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
| 153 | * A parser must implement the [[RequestParserInterface]]. |
||
| 154 | * |
||
| 155 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
| 156 | * |
||
| 157 | * ``` |
||
| 158 | * [ |
||
| 159 | * 'application/json' => 'yii\web\JsonParser', |
||
| 160 | * ] |
||
| 161 | * ``` |
||
| 162 | * |
||
| 163 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
| 164 | * This one will be used as a fallback in case no other types match. |
||
| 165 | * |
||
| 166 | * @see getBodyParams() |
||
| 167 | */ |
||
| 168 | public $parsers = []; |
||
| 169 | /** |
||
| 170 | * @var array the configuration for trusted security related headers. |
||
| 171 | * |
||
| 172 | * An array key is an IPv4 or IPv6 IP address in CIDR notation for matching a client. |
||
| 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 | * For example, to trust all headers listed in [[secureHeaders]] for IP addresses |
||
| 179 | * in range `192.168.0.0-192.168.0.254` write the following: |
||
| 180 | * |
||
| 181 | * ```php |
||
| 182 | * [ |
||
| 183 | * '192.168.0.0/24', |
||
| 184 | * ] |
||
| 185 | * ``` |
||
| 186 | * |
||
| 187 | * To trust just the `X-Forwarded-For` header from `10.0.0.1`, use: |
||
| 188 | * |
||
| 189 | * ``` |
||
| 190 | * [ |
||
| 191 | * '10.0.0.1' => ['X-Forwarded-For'] |
||
| 192 | * ] |
||
| 193 | * ``` |
||
| 194 | * |
||
| 195 | * Default is to trust all headers except those listed in [[secureHeaders]] from all hosts. |
||
| 196 | * Matches are tried in order and searching is stopped when IP matches. |
||
| 197 | * |
||
| 198 | * > Info: Matching is performed using [[IpValidator]]. |
||
| 199 | * See [[IpValidator::::setRanges()|IpValidator::setRanges()]] |
||
| 200 | * and [[IpValidator::networks]] for advanced matching. |
||
| 201 | * |
||
| 202 | * @see $secureHeaders |
||
| 203 | * @since 2.0.13 |
||
| 204 | */ |
||
| 205 | public $trustedHosts = []; |
||
| 206 | /** |
||
| 207 | * @var array lists of headers that are, by default, subject to the trusted host configuration. |
||
| 208 | * These headers will be filtered unless explicitly allowed in [[trustedHosts]]. |
||
| 209 | * The match of header names is case-insensitive. |
||
| 210 | * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields |
||
| 211 | * @see $trustedHosts |
||
| 212 | * @since 2.0.13 |
||
| 213 | */ |
||
| 214 | public $secureHeaders = [ |
||
| 215 | 'X-Forwarded-For', |
||
| 216 | 'X-Forwarded-Host', |
||
| 217 | 'X-Forwarded-Proto', |
||
| 218 | 'Front-End-Https', |
||
| 219 | 'X-Rewrite-Url', |
||
| 220 | ]; |
||
| 221 | /** |
||
| 222 | * @var string[] List of headers where proxies store the real client IP. |
||
| 223 | * It's not advisable to put insecure headers here. |
||
| 224 | * The match of header names is case-insensitive. |
||
| 225 | * @see $trustedHosts |
||
| 226 | * @see $secureHeaders |
||
| 227 | * @since 2.0.13 |
||
| 228 | */ |
||
| 229 | public $ipHeaders = [ |
||
| 230 | 'X-Forwarded-For', |
||
| 231 | ]; |
||
| 232 | /** |
||
| 233 | * @var array list of headers to check for determining whether the connection is made via HTTPS. |
||
| 234 | * The array keys are header names and the array value is a list of header values that indicate a secure connection. |
||
| 235 | * The match of header names and values is case-insensitive. |
||
| 236 | * It's not advisable to put insecure headers here. |
||
| 237 | * @see $trustedHosts |
||
| 238 | * @see $secureHeaders |
||
| 239 | * @since 2.0.13 |
||
| 240 | */ |
||
| 241 | public $secureProtocolHeaders = [ |
||
| 242 | 'X-Forwarded-Proto' => ['https'], |
||
| 243 | 'Front-End-Https' => ['on'], |
||
| 244 | ]; |
||
| 245 | /** |
||
| 246 | * @var CookieCollection Collection of request cookies. |
||
| 247 | */ |
||
| 248 | private $_cookies; |
||
| 249 | /** |
||
| 250 | * @var HeaderCollection Collection of request headers. |
||
| 251 | */ |
||
| 252 | private $_headers; |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Resolves the current request into a route and the associated parameters. |
||
| 257 | * @return array the first element is the route, and the second is the associated parameters. |
||
| 258 | * @throws NotFoundHttpException if the request cannot be resolved. |
||
| 259 | */ |
||
| 260 | 1 | public function resolve() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Filters headers according to the [[trustedHosts]]. |
||
| 279 | * @param HeaderCollection $headerCollection |
||
| 280 | * @since 2.0.13 |
||
| 281 | */ |
||
| 282 | 130 | protected function filterHeaders(HeaderCollection $headerCollection) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Creates instance of [[IpValidator]]. |
||
| 314 | * You can override this method to adjust validator or implement different matching strategy. |
||
| 315 | * |
||
| 316 | * @return IpValidator |
||
| 317 | * @since 2.0.13 |
||
| 318 | */ |
||
| 319 | 19 | protected function getIpValidator() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the header collection. |
||
| 326 | * The header collection contains incoming HTTP headers. |
||
| 327 | * @return HeaderCollection the header collection |
||
| 328 | */ |
||
| 329 | 130 | public function getHeaders() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
||
| 359 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||
| 360 | * The value returned is turned into upper case. |
||
| 361 | */ |
||
| 362 | 22 | public function getMethod() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Returns whether this is a GET request. |
||
| 381 | * @return bool whether this is a GET request. |
||
| 382 | */ |
||
| 383 | 2 | public function getIsGet() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Returns whether this is an OPTIONS request. |
||
| 390 | * @return bool whether this is a OPTIONS request. |
||
| 391 | */ |
||
| 392 | public function getIsOptions() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns whether this is a HEAD request. |
||
| 399 | * @return bool whether this is a HEAD request. |
||
| 400 | */ |
||
| 401 | 9 | public function getIsHead() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Returns whether this is a POST request. |
||
| 408 | * @return bool whether this is a POST request. |
||
| 409 | */ |
||
| 410 | public function getIsPost() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns whether this is a DELETE request. |
||
| 417 | * @return bool whether this is a DELETE request. |
||
| 418 | */ |
||
| 419 | public function getIsDelete() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Returns whether this is a PUT request. |
||
| 426 | * @return bool whether this is a PUT request. |
||
| 427 | */ |
||
| 428 | public function getIsPut() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns whether this is a PATCH request. |
||
| 435 | * @return bool whether this is a PATCH request. |
||
| 436 | */ |
||
| 437 | public function getIsPatch() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
| 444 | * |
||
| 445 | * Note that jQuery doesn't set the header in case of cross domain |
||
| 446 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
||
| 447 | * |
||
| 448 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
||
| 449 | */ |
||
| 450 | 14 | public function getIsAjax() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Returns whether this is a PJAX request. |
||
| 457 | * @return bool whether this is a PJAX request |
||
| 458 | */ |
||
| 459 | 3 | public function getIsPjax() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns whether this is an Adobe Flash or Flex request. |
||
| 466 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
| 467 | */ |
||
| 468 | public function getIsFlash() |
||
| 474 | |||
| 475 | private $_rawBody; |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the raw HTTP request body. |
||
| 479 | * @return string the request body |
||
| 480 | */ |
||
| 481 | public function getRawBody() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
| 492 | * @param string $rawBody the request body |
||
| 493 | */ |
||
| 494 | public function setRawBody($rawBody) |
||
| 498 | |||
| 499 | private $_bodyParams; |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the request parameters given in the request body. |
||
| 503 | * |
||
| 504 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
| 505 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
| 506 | * to parse the [[rawBody|request body]]. |
||
| 507 | * @return array the request parameters given in the request body. |
||
| 508 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
| 509 | * @see getMethod() |
||
| 510 | * @see getBodyParam() |
||
| 511 | * @see setBodyParams() |
||
| 512 | */ |
||
| 513 | 3 | public function getBodyParams() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Sets the request body parameters. |
||
| 556 | * @param array $values the request body parameters (name-value pairs) |
||
| 557 | * @see getBodyParam() |
||
| 558 | * @see getBodyParams() |
||
| 559 | */ |
||
| 560 | 2 | public function setBodyParams($values) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Returns the named request body parameter value. |
||
| 567 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
| 568 | * @param string $name the parameter name |
||
| 569 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 570 | * @return mixed the parameter value |
||
| 571 | * @see getBodyParams() |
||
| 572 | * @see setBodyParams() |
||
| 573 | */ |
||
| 574 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
| 583 | * |
||
| 584 | * @param string $name the parameter name |
||
| 585 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 586 | * @return array|mixed |
||
| 587 | */ |
||
| 588 | public function post($name = null, $defaultValue = null) |
||
| 596 | |||
| 597 | private $_queryParams; |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Returns the request parameters given in the [[queryString]]. |
||
| 601 | * |
||
| 602 | * This method will return the contents of `$_GET` if params where not explicitly set. |
||
| 603 | * @return array the request GET parameter values. |
||
| 604 | * @see setQueryParams() |
||
| 605 | */ |
||
| 606 | 31 | public function getQueryParams() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Sets the request [[queryString]] parameters. |
||
| 617 | * @param array $values the request query parameters (name-value pairs) |
||
| 618 | * @see getQueryParam() |
||
| 619 | * @see getQueryParams() |
||
| 620 | */ |
||
| 621 | 7 | public function setQueryParams($values) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
| 628 | * |
||
| 629 | * @param string $name the parameter name |
||
| 630 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
| 631 | * @return array|mixed |
||
| 632 | */ |
||
| 633 | 19 | public function get($name = null, $defaultValue = null) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Returns the named GET parameter value. |
||
| 644 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
| 645 | * @param string $name the GET parameter name. |
||
| 646 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
||
| 647 | * @return mixed the GET parameter value |
||
| 648 | * @see getBodyParam() |
||
| 649 | */ |
||
| 650 | 22 | public function getQueryParam($name, $defaultValue = null) |
|
| 656 | |||
| 657 | private $_hostInfo; |
||
| 658 | private $_hostName; |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Returns the schema and host part of the current request URL. |
||
| 662 | * |
||
| 663 | * The returned URL does not have an ending slash. |
||
| 664 | * |
||
| 665 | * By default this value is based on the user request information. This method will |
||
| 666 | * return the value of `$_SERVER['HTTP_HOST']` if it is available or `$_SERVER['SERVER_NAME']` if not. |
||
| 667 | * You may want to check out the [PHP documentation](http://php.net/manual/en/reserved.variables.server.php) |
||
| 668 | * for more information on these variables. |
||
| 669 | * |
||
| 670 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
||
| 671 | * |
||
| 672 | * > Warning: Dependent on the server configuration this information may not be |
||
| 673 | * > reliable and [may be faked by the user sending the HTTP request](https://www.acunetix.com/vulnerabilities/web/host-header-attack). |
||
| 674 | * > If the webserver is configured to serve the same site independent of the value of |
||
| 675 | * > the `Host` header, this value is not reliable. In such situations you should either |
||
| 676 | * > fix your webserver configuration or explicitly set the value by setting the [[setHostInfo()|hostInfo]] property. |
||
| 677 | * > If you don't have access to the server configuration, you can setup [[\yii\filters\HostControl]] filter at |
||
| 678 | * > application level in order to protect against such kind of attack. |
||
| 679 | * |
||
| 680 | * @property string|null schema and hostname part (with port number if needed) of the request URL |
||
| 681 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 682 | * See [[getHostInfo()]] for security related notes on this property. |
||
| 683 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
| 684 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
||
| 685 | * @see setHostInfo() |
||
| 686 | */ |
||
| 687 | 24 | public function getHostInfo() |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Sets the schema and host part of the application URL. |
||
| 708 | * This setter is provided in case the schema and hostname cannot be determined |
||
| 709 | * on certain Web servers. |
||
| 710 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
||
| 711 | * @see getHostInfo() for security related notes on this property. |
||
| 712 | */ |
||
| 713 | 57 | public function setHostInfo($value) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Returns the host part of the current request URL. |
||
| 721 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
| 722 | * |
||
| 723 | * > Warning: The content of this value may not be reliable, dependent on the server |
||
| 724 | * > configuration. Please refer to [[getHostInfo()]] for more information. |
||
| 725 | * |
||
| 726 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
||
| 727 | * @see getHostInfo() |
||
| 728 | * @since 2.0.10 |
||
| 729 | */ |
||
| 730 | 11 | public function getHostName() |
|
| 738 | |||
| 739 | private $_baseUrl; |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Returns the relative URL for the application. |
||
| 743 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
||
| 744 | * and the ending slashes are removed. |
||
| 745 | * @return string the relative URL for the application |
||
| 746 | * @see setScriptUrl() |
||
| 747 | */ |
||
| 748 | 273 | public function getBaseUrl() |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Sets the relative URL for the application. |
||
| 759 | * By default the URL is determined based on the entry script URL. |
||
| 760 | * This setter is provided in case you want to change this behavior. |
||
| 761 | * @param string $value the relative URL for the application |
||
| 762 | */ |
||
| 763 | 1 | public function setBaseUrl($value) |
|
| 767 | |||
| 768 | private $_scriptUrl; |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Returns the relative URL of the entry script. |
||
| 772 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 773 | * @return string the relative URL of the entry script. |
||
| 774 | * @throws InvalidConfigException if unable to determine the entry script URL |
||
| 775 | */ |
||
| 776 | 274 | public function getScriptUrl() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Sets the relative URL for the application entry script. |
||
| 801 | * This setter is provided in case the entry script URL cannot be determined |
||
| 802 | * on certain Web servers. |
||
| 803 | * @param string $value the relative URL for the application entry script. |
||
| 804 | */ |
||
| 805 | 284 | public function setScriptUrl($value) |
|
| 809 | |||
| 810 | private $_scriptFile; |
||
|
|
|||
| 811 | |||
| 812 | /** |
||
| 813 | * Returns the entry script file path. |
||
| 814 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
||
| 815 | * @return string the entry script file path |
||
| 816 | * @throws InvalidConfigException |
||
| 817 | */ |
||
| 818 | 275 | public function getScriptFile() |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Sets the entry script file path. |
||
| 833 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
||
| 834 | * If your server configuration does not return the correct value, you may configure |
||
| 835 | * this property to make it right. |
||
| 836 | * @param string $value the entry script file path. |
||
| 837 | */ |
||
| 838 | 253 | public function setScriptFile($value) |
|
| 842 | |||
| 843 | private $_pathInfo; |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Returns the path info of the currently requested URL. |
||
| 847 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 848 | * The starting and ending slashes are both removed. |
||
| 849 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 850 | * Note, the returned path info is already URL-decoded. |
||
| 851 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 852 | */ |
||
| 853 | 18 | public function getPathInfo() |
|
| 861 | |||
| 862 | /** |
||
| 863 | * Sets the path info of the current request. |
||
| 864 | * This method is mainly provided for testing purpose. |
||
| 865 | * @param string $value the path info of the current request |
||
| 866 | */ |
||
| 867 | 19 | public function setPathInfo($value) |
|
| 871 | |||
| 872 | /** |
||
| 873 | * Resolves the path info part of the currently requested URL. |
||
| 874 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
| 875 | * The starting slashes are both removed (ending slashes will be kept). |
||
| 876 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
| 877 | * Note, the returned path info is decoded. |
||
| 878 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
| 879 | */ |
||
| 880 | protected function resolvePathInfo() |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Returns the currently requested absolute URL. |
||
| 927 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
| 928 | * @return string the currently requested absolute URL. |
||
| 929 | */ |
||
| 930 | public function getAbsoluteUrl() |
||
| 934 | |||
| 935 | private $_url; |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Returns the currently requested relative URL. |
||
| 939 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
||
| 940 | * It includes the [[queryString]] part if any. |
||
| 941 | * @return string the currently requested relative URL. Note that the URI returned may be URL-encoded depending on the client. |
||
| 942 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
| 943 | */ |
||
| 944 | 11 | public function getUrl() |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Sets the currently requested relative URL. |
||
| 955 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
| 956 | * Note that the URI should be URL-encoded. |
||
| 957 | * @param string $value the request URI to be set |
||
| 958 | */ |
||
| 959 | 24 | public function setUrl($value) |
|
| 963 | |||
| 964 | /** |
||
| 965 | * Resolves the request URI portion for the currently requested URL. |
||
| 966 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
| 967 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
| 968 | * @return string|bool the request URI portion for the currently requested URL. |
||
| 969 | * Note that the URI returned may be URL-encoded depending on the client. |
||
| 970 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
| 971 | */ |
||
| 972 | 3 | protected function resolveRequestUri() |
|
| 992 | |||
| 993 | /** |
||
| 994 | * Returns part of the request URL that is after the question mark. |
||
| 995 | * @return string part of the request URL that is after the question mark |
||
| 996 | */ |
||
| 997 | public function getQueryString() |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Return if the request is sent via secure channel (https). |
||
| 1004 | * @return bool if the request is sent via secure channel (https) |
||
| 1005 | */ |
||
| 1006 | 37 | public function getIsSecureConnection() |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Returns the server name. |
||
| 1026 | * @return string server name, null if not available |
||
| 1027 | */ |
||
| 1028 | 1 | public function getServerName() |
|
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Returns the server port number. |
||
| 1035 | * @return int|null server port number, null if not available |
||
| 1036 | */ |
||
| 1037 | 1 | public function getServerPort() |
|
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Returns the URL referrer. |
||
| 1044 | * @return string|null URL referrer, null if not available |
||
| 1045 | */ |
||
| 1046 | public function getReferrer() |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Returns the URL origin of a CORS request. |
||
| 1053 | * |
||
| 1054 | * The return value is taken from the `Origin` [[getHeaders()|header]] sent by the browser. |
||
| 1055 | * |
||
| 1056 | * Note that the origin request header indicates where a fetch originates from. |
||
| 1057 | * It doesn't include any path information, but only the server name. |
||
| 1058 | * It is sent with a CORS requests, as well as with POST requests. |
||
| 1059 | * It is similar to the referer header, but, unlike this header, it doesn't disclose the whole path. |
||
| 1060 | * Please refer to <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin> for more information. |
||
| 1061 | * |
||
| 1062 | * @return string|null URL origin of a CORS request, `null` if not available. |
||
| 1063 | * @see getHeaders() |
||
| 1064 | * @since 2.0.13 |
||
| 1065 | */ |
||
| 1066 | 1 | public function getOrigin() |
|
| 1070 | |||
| 1071 | /** |
||
| 1072 | * Returns the user agent. |
||
| 1073 | * @return string|null user agent, null if not available |
||
| 1074 | */ |
||
| 1075 | public function getUserAgent() |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Returns the user IP address. |
||
| 1082 | * The IP is determined using headers and / or `$_SERVER` variables. |
||
| 1083 | * @return string|null user IP address, null if not available |
||
| 1084 | */ |
||
| 1085 | 45 | public function getUserIP() |
|
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Returns the user host name. |
||
| 1098 | * The HOST is determined using headers and / or `$_SERVER` variables. |
||
| 1099 | * @return string|null user host name, null if not available |
||
| 1100 | */ |
||
| 1101 | public function getUserHost() |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Returns the IP on the other end of this connection. |
||
| 1114 | * This is always the next hop, any headers are ignored. |
||
| 1115 | * @return string|null remote IP address, `null` if not available. |
||
| 1116 | * @since 2.0.13 |
||
| 1117 | */ |
||
| 1118 | 60 | public function getRemoteIP() |
|
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Returns the host name of the other end of this connection. |
||
| 1125 | * This is always the next hop, any headers are ignored. |
||
| 1126 | * @return string|null remote host name, `null` if not available |
||
| 1127 | * @see getUserHost() |
||
| 1128 | * @see getRemoteIP() |
||
| 1129 | * @since 2.0.13 |
||
| 1130 | */ |
||
| 1131 | public function getRemoteHost() |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * @return string|null the username sent via HTTP authentication, `null` if the username is not given |
||
| 1138 | * @see getAuthCredentials() to get both username and password in one call |
||
| 1139 | */ |
||
| 1140 | 9 | public function getAuthUser() |
|
| 1144 | |||
| 1145 | /** |
||
| 1146 | * @return string|null the password sent via HTTP authentication, `null` if the password is not given |
||
| 1147 | * @see getAuthCredentials() to get both username and password in one call |
||
| 1148 | */ |
||
| 1149 | 9 | public function getAuthPassword() |
|
| 1153 | |||
| 1154 | /** |
||
| 1155 | * @return array that contains exactly two elements: |
||
| 1156 | * - 0: the username sent via HTTP authentication, `null` if the username is not given |
||
| 1157 | * - 1: the password sent via HTTP authentication, `null` if the password is not given |
||
| 1158 | * @see getAuthUser() to get only username |
||
| 1159 | * @see getAuthPassword() to get only password |
||
| 1160 | * @since 2.0.13 |
||
| 1161 | */ |
||
| 1162 | 29 | public function getAuthCredentials() |
|
| 1191 | |||
| 1192 | private $_port; |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Returns the port to use for insecure requests. |
||
| 1196 | * Defaults to 80, or the port specified by the server if the current |
||
| 1197 | * request is insecure. |
||
| 1198 | * @return int port number for insecure requests. |
||
| 1199 | * @see setPort() |
||
| 1200 | */ |
||
| 1201 | public function getPort() |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Sets the port to use for insecure requests. |
||
| 1212 | * This setter is provided in case a custom port is necessary for certain |
||
| 1213 | * server configurations. |
||
| 1214 | * @param int $value port number. |
||
| 1215 | */ |
||
| 1216 | public function setPort($value) |
||
| 1223 | |||
| 1224 | private $_securePort; |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Returns the port to use for secure requests. |
||
| 1228 | * Defaults to 443, or the port specified by the server if the current |
||
| 1229 | * request is secure. |
||
| 1230 | * @return int port number for secure requests. |
||
| 1231 | * @see setSecurePort() |
||
| 1232 | */ |
||
| 1233 | public function getSecurePort() |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Sets the port to use for secure requests. |
||
| 1244 | * This setter is provided in case a custom port is necessary for certain |
||
| 1245 | * server configurations. |
||
| 1246 | * @param int $value port number. |
||
| 1247 | */ |
||
| 1248 | public function setSecurePort($value) |
||
| 1255 | |||
| 1256 | private $_contentTypes; |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Returns the content types acceptable by the end user. |
||
| 1260 | * |
||
| 1261 | * This is determined by the `Accept` HTTP header. For example, |
||
| 1262 | * |
||
| 1263 | * ```php |
||
| 1264 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1265 | * $types = $request->getAcceptableContentTypes(); |
||
| 1266 | * print_r($types); |
||
| 1267 | * // displays: |
||
| 1268 | * // [ |
||
| 1269 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1270 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1271 | * // 'text/plain' => ['q' => 0.5], |
||
| 1272 | * // ] |
||
| 1273 | * ``` |
||
| 1274 | * |
||
| 1275 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
| 1276 | * will be returned first. The array keys are the content types, while the array values |
||
| 1277 | * are the corresponding quality score and other parameters as given in the header. |
||
| 1278 | */ |
||
| 1279 | 2 | public function getAcceptableContentTypes() |
|
| 1291 | |||
| 1292 | /** |
||
| 1293 | * Sets the acceptable content types. |
||
| 1294 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
| 1295 | * @param array $value the content types that are acceptable by the end user. They should |
||
| 1296 | * be ordered by the preference level. |
||
| 1297 | * @see getAcceptableContentTypes() |
||
| 1298 | * @see parseAcceptHeader() |
||
| 1299 | */ |
||
| 1300 | public function setAcceptableContentTypes($value) |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Returns request content-type |
||
| 1307 | * The Content-Type header field indicates the MIME type of the data |
||
| 1308 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
| 1309 | * media type that would have been sent had the request been a GET. |
||
| 1310 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
| 1311 | * @return string request content-type. Null is returned if this information is not available. |
||
| 1312 | * @link https://tools.ietf.org/html/rfc2616#section-14.17 |
||
| 1313 | * HTTP 1.1 header field definitions |
||
| 1314 | */ |
||
| 1315 | public function getContentType() |
||
| 1324 | |||
| 1325 | private $_languages; |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Returns the languages acceptable by the end user. |
||
| 1329 | * This is determined by the `Accept-Language` HTTP header. |
||
| 1330 | * @return array the languages ordered by the preference level. The first element |
||
| 1331 | * represents the most preferred language. |
||
| 1332 | */ |
||
| 1333 | 1 | public function getAcceptableLanguages() |
|
| 1345 | |||
| 1346 | /** |
||
| 1347 | * @param array $value the languages that are acceptable by the end user. They should |
||
| 1348 | * be ordered by the preference level. |
||
| 1349 | */ |
||
| 1350 | 1 | public function setAcceptableLanguages($value) |
|
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
| 1357 | * |
||
| 1358 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
| 1359 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
||
| 1360 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
||
| 1361 | * values with the highest quality scores will be returned first. For example, |
||
| 1362 | * |
||
| 1363 | * ```php |
||
| 1364 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
| 1365 | * $accepts = $request->parseAcceptHeader($header); |
||
| 1366 | * print_r($accepts); |
||
| 1367 | * // displays: |
||
| 1368 | * // [ |
||
| 1369 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
| 1370 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
| 1371 | * // 'text/plain' => ['q' => 0.5], |
||
| 1372 | * // ] |
||
| 1373 | * ``` |
||
| 1374 | * |
||
| 1375 | * @param string $header the header to be parsed |
||
| 1376 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
||
| 1377 | * will be returned first. |
||
| 1378 | */ |
||
| 1379 | 3 | public function parseAcceptHeader($header) |
|
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Returns the user-preferred language that should be used by this application. |
||
| 1449 | * The language resolution is based on the user preferred languages and the languages |
||
| 1450 | * supported by the application. The method will try to find the best match. |
||
| 1451 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
| 1452 | * application language will be returned without further processing. |
||
| 1453 | * @return string the language that the application should use. |
||
| 1454 | */ |
||
| 1455 | 1 | public function getPreferredLanguage(array $languages = []) |
|
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Gets the Etags. |
||
| 1480 | * |
||
| 1481 | * @return array The entity tags |
||
| 1482 | */ |
||
| 1483 | public function getETags() |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * Returns the cookie collection. |
||
| 1494 | * |
||
| 1495 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
||
| 1496 | * |
||
| 1497 | * ```php |
||
| 1498 | * $cookie = $request->cookies['name'] |
||
| 1499 | * if ($cookie !== null) { |
||
| 1500 | * $value = $cookie->value; |
||
| 1501 | * } |
||
| 1502 | * |
||
| 1503 | * // alternatively |
||
| 1504 | * $value = $request->cookies->getValue('name'); |
||
| 1505 | * ``` |
||
| 1506 | * |
||
| 1507 | * @return CookieCollection the cookie collection. |
||
| 1508 | */ |
||
| 1509 | 33 | public function getCookies() |
|
| 1519 | |||
| 1520 | /** |
||
| 1521 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
| 1522 | * @return array the cookies obtained from request |
||
| 1523 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
| 1524 | */ |
||
| 1525 | 33 | protected function loadCookies() |
|
| 1563 | |||
| 1564 | private $_csrfToken; |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Returns the token used to perform CSRF validation. |
||
| 1568 | * |
||
| 1569 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
| 1570 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
| 1571 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
| 1572 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
| 1573 | * @return string the token used to perform CSRF validation. |
||
| 1574 | */ |
||
| 1575 | 37 | public function getCsrfToken($regenerate = false) |
|
| 1586 | |||
| 1587 | /** |
||
| 1588 | * Loads the CSRF token from cookie or session. |
||
| 1589 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
||
| 1590 | * does not have CSRF token. |
||
| 1591 | */ |
||
| 1592 | 37 | protected function loadCsrfToken() |
|
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Generates an unmasked random token used to perform CSRF validation. |
||
| 1603 | * @return string the random token for CSRF validation. |
||
| 1604 | */ |
||
| 1605 | 35 | protected function generateCsrfToken() |
|
| 1617 | |||
| 1618 | /** |
||
| 1619 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
| 1620 | */ |
||
| 1621 | 3 | public function getCsrfTokenFromHeader() |
|
| 1625 | |||
| 1626 | /** |
||
| 1627 | * Creates a cookie with a randomly generated CSRF token. |
||
| 1628 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
| 1629 | * @param string $token the CSRF token |
||
| 1630 | * @return Cookie the generated cookie |
||
| 1631 | * @see enableCsrfValidation |
||
| 1632 | */ |
||
| 1633 | 33 | protected function createCsrfCookie($token) |
|
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Performs the CSRF validation. |
||
| 1645 | * |
||
| 1646 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
||
| 1647 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
| 1648 | * |
||
| 1649 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
||
| 1650 | * is among GET, HEAD or OPTIONS. |
||
| 1651 | * |
||
| 1652 | * @param string $clientSuppliedToken the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
| 1653 | * the [[csrfParam]] POST field or HTTP header. |
||
| 1654 | * This parameter is available since version 2.0.4. |
||
| 1655 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
| 1656 | */ |
||
| 1657 | 5 | public function validateCsrfToken($clientSuppliedToken = null) |
|
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Validates CSRF token. |
||
| 1677 | * |
||
| 1678 | * @param string $clientSuppliedToken The masked client-supplied token. |
||
| 1679 | * @param string $trueToken The masked true token. |
||
| 1680 | * @return bool |
||
| 1681 | */ |
||
| 1682 | 3 | private function validateCsrfTokenInternal($clientSuppliedToken, $trueToken) |
|
| 1692 | } |
||
| 1693 |