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 |
||
86 | class Request extends \yii\base\Request |
||
87 | { |
||
88 | /** |
||
89 | * The name of the HTTP header for sending CSRF token. |
||
90 | */ |
||
91 | const CSRF_HEADER = 'X-CSRF-Token'; |
||
92 | /** |
||
93 | * The length of the CSRF token mask. |
||
94 | */ |
||
95 | const CSRF_MASK_LENGTH = 8; |
||
96 | |||
97 | /** |
||
98 | * @var bool whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. |
||
99 | * When CSRF validation is enabled, forms submitted to an Yii Web application must be originated |
||
100 | * from the same application. If not, a 400 HTTP exception will be raised. |
||
101 | * |
||
102 | * Note, this feature requires that the user client accepts cookie. Also, to use this feature, |
||
103 | * forms submitted via POST method must contain a hidden input whose name is specified by [[csrfParam]]. |
||
104 | * You may use [[\yii\helpers\Html::beginForm()]] to generate his hidden input. |
||
105 | * |
||
106 | * In JavaScript, you may get the values of [[csrfParam]] and [[csrfToken]] via `yii.getCsrfParam()` and |
||
107 | * `yii.getCsrfToken()`, respectively. The [[\yii\web\YiiAsset]] asset must be registered. |
||
108 | * You also need to include CSRF meta tags in your pages by using [[\yii\helpers\Html::csrfMetaTags()]]. |
||
109 | * |
||
110 | * @see Controller::enableCsrfValidation |
||
111 | * @see http://en.wikipedia.org/wiki/Cross-site_request_forgery |
||
112 | */ |
||
113 | public $enableCsrfValidation = true; |
||
114 | /** |
||
115 | * @var string the name of the token used to prevent CSRF. Defaults to '_csrf'. |
||
116 | * This property is used only when [[enableCsrfValidation]] is true. |
||
117 | */ |
||
118 | public $csrfParam = '_csrf'; |
||
119 | /** |
||
120 | * @var array the configuration for creating the CSRF [[Cookie|cookie]]. This property is used only when |
||
121 | * both [[enableCsrfValidation]] and [[enableCsrfCookie]] are true. |
||
122 | */ |
||
123 | public $csrfCookie = ['httpOnly' => true]; |
||
124 | /** |
||
125 | * @var bool whether to use cookie to persist CSRF token. If false, CSRF token will be stored |
||
126 | * in session under the name of [[csrfParam]]. Note that while storing CSRF tokens in session increases |
||
127 | * security, it requires starting a session for every page, which will degrade your site performance. |
||
128 | */ |
||
129 | public $enableCsrfCookie = true; |
||
130 | /** |
||
131 | * @var bool whether cookies should be validated to ensure they are not tampered. Defaults to true. |
||
132 | */ |
||
133 | public $enableCookieValidation = true; |
||
134 | /** |
||
135 | * @var string a secret key used for cookie validation. This property must be set if [[enableCookieValidation]] is true. |
||
136 | */ |
||
137 | public $cookieValidationKey; |
||
138 | /** |
||
139 | * @var string the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE |
||
140 | * request tunneled through POST. Defaults to '_method'. |
||
141 | * @see getMethod() |
||
142 | * @see getBodyParams() |
||
143 | */ |
||
144 | public $methodParam = '_method'; |
||
145 | /** |
||
146 | * @var array the parsers for converting the raw HTTP request body into [[bodyParams]]. |
||
147 | * The array keys are the request `Content-Types`, and the array values are the |
||
148 | * corresponding configurations for [[Yii::createObject|creating the parser objects]]. |
||
149 | * A parser must implement the [[RequestParserInterface]]. |
||
150 | * |
||
151 | * To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example: |
||
152 | * |
||
153 | * ``` |
||
154 | * [ |
||
155 | * 'application/json' => \yii\web\JsonParser::class, |
||
156 | * ] |
||
157 | * ``` |
||
158 | * |
||
159 | * To register a parser for parsing all request types you can use `'*'` as the array key. |
||
160 | * This one will be used as a fallback in case no other types match. |
||
161 | * |
||
162 | * @see getBodyParams() |
||
163 | */ |
||
164 | public $parsers = []; |
||
165 | |||
166 | /** |
||
167 | * @var CookieCollection Collection of request cookies. |
||
168 | */ |
||
169 | private $_cookies; |
||
170 | /** |
||
171 | * @var HeaderCollection Collection of request headers. |
||
172 | */ |
||
173 | private $_headers; |
||
174 | |||
175 | |||
176 | /** |
||
177 | 1 | * Resolves the current request into a route and the associated parameters. |
|
178 | * @return array the first element is the route, and the second is the associated parameters. |
||
179 | 1 | * @throws NotFoundHttpException if the request cannot be resolved. |
|
180 | 1 | */ |
|
181 | 1 | public function resolve() |
|
196 | |||
197 | /** |
||
198 | 6 | * Returns the header collection. |
|
199 | * The header collection contains incoming HTTP headers. |
||
200 | 6 | * @return HeaderCollection the header collection |
|
201 | 6 | */ |
|
202 | 6 | public function getHeaders() |
|
227 | |||
228 | /** |
||
229 | 15 | * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). |
|
230 | * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. |
||
231 | 15 | * The value returned is turned into upper case. |
|
232 | 4 | */ |
|
233 | public function getMethod() |
||
249 | |||
250 | 2 | /** |
|
251 | * Returns whether this is a GET request. |
||
252 | 2 | * @return bool whether this is a GET request. |
|
253 | */ |
||
254 | public function getIsGet() |
||
258 | |||
259 | /** |
||
260 | * Returns whether this is an OPTIONS request. |
||
261 | * @return bool whether this is a OPTIONS request. |
||
262 | */ |
||
263 | public function getIsOptions() |
||
267 | |||
268 | 6 | /** |
|
269 | * Returns whether this is a HEAD request. |
||
270 | 6 | * @return bool whether this is a HEAD request. |
|
271 | */ |
||
272 | public function getIsHead() |
||
276 | |||
277 | /** |
||
278 | * Returns whether this is a POST request. |
||
279 | * @return bool whether this is a POST request. |
||
280 | */ |
||
281 | public function getIsPost() |
||
285 | |||
286 | /** |
||
287 | * Returns whether this is a DELETE request. |
||
288 | * @return bool whether this is a DELETE request. |
||
289 | */ |
||
290 | public function getIsDelete() |
||
294 | |||
295 | /** |
||
296 | * Returns whether this is a PUT request. |
||
297 | * @return bool whether this is a PUT request. |
||
298 | */ |
||
299 | public function getIsPut() |
||
303 | |||
304 | /** |
||
305 | * Returns whether this is a PATCH request. |
||
306 | * @return bool whether this is a PATCH request. |
||
307 | */ |
||
308 | public function getIsPatch() |
||
312 | |||
313 | /** |
||
314 | * Returns whether this is an AJAX (XMLHttpRequest) request. |
||
315 | * |
||
316 | * Note that jQuery doesn't set the header in case of cross domain |
||
317 | 2 | * requests: https://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header |
|
318 | * |
||
319 | 2 | * @return bool whether this is an AJAX (XMLHttpRequest) request. |
|
320 | */ |
||
321 | public function getIsAjax() |
||
325 | |||
326 | 1 | /** |
|
327 | * Returns whether this is a PJAX request |
||
328 | 1 | * @return bool whether this is a PJAX request |
|
329 | */ |
||
330 | public function getIsPjax() |
||
334 | |||
335 | /** |
||
336 | * Returns whether this is an Adobe Flash or Flex request. |
||
337 | * @return bool whether this is an Adobe Flash or Adobe Flex request. |
||
338 | */ |
||
339 | public function getIsFlash() |
||
344 | |||
345 | private $_rawBody; |
||
346 | |||
347 | /** |
||
348 | * Returns the raw HTTP request body. |
||
349 | * @return string the request body |
||
350 | */ |
||
351 | public function getRawBody() |
||
359 | |||
360 | /** |
||
361 | * Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. |
||
362 | * @param string $rawBody the request body |
||
363 | */ |
||
364 | public function setRawBody($rawBody) |
||
368 | |||
369 | private $_bodyParams; |
||
370 | |||
371 | /** |
||
372 | * Returns the request parameters given in the request body. |
||
373 | * |
||
374 | * Request parameters are determined using the parsers configured in [[parsers]] property. |
||
375 | * If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
||
376 | * to parse the [[rawBody|request body]]. |
||
377 | * @return array the request parameters given in the request body. |
||
378 | * @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
||
379 | 3 | * @see getMethod() |
|
380 | * @see getBodyParam() |
||
381 | 3 | * @see setBodyParams() |
|
382 | 1 | */ |
|
383 | 1 | public function getBodyParams() |
|
423 | |||
424 | /** |
||
425 | * Sets the request body parameters. |
||
426 | 2 | * @param array $values the request body parameters (name-value pairs) |
|
427 | * @see getBodyParam() |
||
428 | 2 | * @see getBodyParams() |
|
429 | 2 | */ |
|
430 | public function setBodyParams($values) |
||
434 | |||
435 | /** |
||
436 | * Returns the named request body parameter value. |
||
437 | * If the parameter does not exist, the second parameter passed to this method will be returned. |
||
438 | * @param string $name the parameter name |
||
439 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
440 | 3 | * @return mixed the parameter value |
|
441 | * @see getBodyParams() |
||
442 | 3 | * @see setBodyParams() |
|
443 | */ |
||
444 | 3 | public function getBodyParam($name, $defaultValue = null) |
|
450 | |||
451 | /** |
||
452 | * Returns POST parameter with a given name. If name isn't specified, returns an array of all POST parameters. |
||
453 | * |
||
454 | * @param string $name the parameter name |
||
455 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
456 | * @return array|mixed |
||
457 | */ |
||
458 | public function post($name = null, $defaultValue = null) |
||
466 | |||
467 | private $_queryParams; |
||
468 | |||
469 | /** |
||
470 | * Returns the request parameters given in the [[queryString]]. |
||
471 | * |
||
472 | 20 | * This method will return the contents of `$_GET` if params where not explicitly set. |
|
473 | * @return array the request GET parameter values. |
||
474 | 20 | * @see setQueryParams() |
|
475 | 18 | */ |
|
476 | public function getQueryParams() |
||
484 | |||
485 | /** |
||
486 | * Sets the request [[queryString]] parameters. |
||
487 | 4 | * @param array $values the request query parameters (name-value pairs) |
|
488 | * @see getQueryParam() |
||
489 | 4 | * @see getQueryParams() |
|
490 | 4 | */ |
|
491 | public function setQueryParams($values) |
||
495 | |||
496 | /** |
||
497 | * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. |
||
498 | * |
||
499 | 11 | * @param string $name the parameter name |
|
500 | * @param mixed $defaultValue the default parameter value if the parameter does not exist. |
||
501 | 11 | * @return array|mixed |
|
502 | */ |
||
503 | public function get($name = null, $defaultValue = null) |
||
511 | |||
512 | /** |
||
513 | * Returns the named GET parameter value. |
||
514 | * If the GET parameter does not exist, the second parameter passed to this method will be returned. |
||
515 | * @param string $name the GET parameter name. |
||
516 | 12 | * @param mixed $defaultValue the default parameter value if the GET parameter does not exist. |
|
517 | * @return mixed the GET parameter value |
||
518 | 12 | * @see getBodyParam() |
|
519 | */ |
||
520 | 12 | public function getQueryParam($name, $defaultValue = null) |
|
526 | |||
527 | private $_hostInfo; |
||
528 | private $_hostName; |
||
529 | |||
530 | /** |
||
531 | * Returns the schema and host part of the current request URL. |
||
532 | * The returned URL does not have an ending slash. |
||
533 | * By default this is determined based on the user request information. |
||
534 | 9 | * You may explicitly specify it by setting the [[setHostInfo()|hostInfo]] property. |
|
535 | * @return string|null schema and hostname part (with port number if needed) of the request URL |
||
536 | 9 | * (e.g. `http://www.yiiframework.com`), null if can't be obtained from `$_SERVER` and wasn't set. |
|
537 | 5 | * @see setHostInfo() |
|
538 | 5 | */ |
|
539 | 5 | public function getHostInfo() |
|
557 | |||
558 | /** |
||
559 | 22 | * Sets the schema and host part of the application URL. |
|
560 | * This setter is provided in case the schema and hostname cannot be determined |
||
561 | 22 | * on certain Web servers. |
|
562 | 22 | * @param string|null $value the schema and host part of the application URL. The trailing slashes will be removed. |
|
563 | */ |
||
564 | public function setHostInfo($value) |
||
569 | |||
570 | /** |
||
571 | * Returns the host part of the current request URL. |
||
572 | * Value is calculated from current [[getHostInfo()|hostInfo]] property. |
||
573 | 118 | * @return string|null hostname part of the request URL (e.g. `www.yiiframework.com`) |
|
574 | * @see getHostInfo() |
||
575 | 118 | * @since 2.0.10 |
|
576 | 117 | */ |
|
577 | 117 | public function getHostName() |
|
585 | |||
586 | private $_baseUrl; |
||
587 | |||
588 | 1 | /** |
|
589 | * Returns the relative URL for the application. |
||
590 | 1 | * This is similar to [[scriptUrl]] except that it does not include the script file name, |
|
591 | 1 | * and the ending slashes are removed. |
|
592 | * @return string the relative URL for the application |
||
593 | * @see setScriptUrl() |
||
594 | */ |
||
595 | public function getBaseUrl() |
||
603 | 119 | ||
604 | 1 | /** |
|
605 | * Sets the relative URL for the application. |
||
606 | * By default the URL is determined based on the entry script URL. |
||
607 | * This setter is provided in case you want to change this behavior. |
||
608 | * @param string $value the relative URL for the application |
||
609 | */ |
||
610 | public function setBaseUrl($value) |
||
614 | |||
615 | private $_scriptUrl; |
||
616 | |||
617 | /** |
||
618 | * Returns the relative URL of the entry script. |
||
619 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
620 | * @return string the relative URL of the entry script. |
||
621 | 118 | * @throws InvalidConfigException if unable to determine the entry script URL |
|
622 | */ |
||
623 | public function getScriptUrl() |
||
645 | 120 | ||
646 | 102 | /** |
|
647 | 18 | * Sets the relative URL for the application entry script. |
|
648 | 16 | * This setter is provided in case the entry script URL cannot be determined |
|
649 | * on certain Web servers. |
||
650 | 2 | * @param string $value the relative URL for the application entry script. |
|
651 | */ |
||
652 | public function setScriptUrl($value) |
||
656 | |||
657 | private $_scriptFile; |
||
|
|||
658 | |||
659 | /** |
||
660 | * Returns the entry script file path. |
||
661 | 102 | * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. |
|
662 | * @return string the entry script file path |
||
663 | 102 | * @throws InvalidConfigException |
|
664 | 102 | */ |
|
665 | public function getScriptFile() |
||
675 | |||
676 | 9 | /** |
|
677 | * Sets the entry script file path. |
||
678 | 9 | * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. |
|
679 | * If your server configuration does not return the correct value, you may configure |
||
680 | * this property to make it right. |
||
681 | * @param string $value the entry script file path. |
||
682 | 9 | */ |
|
683 | public function setScriptFile($value) |
||
687 | |||
688 | private $_pathInfo; |
||
689 | |||
690 | 9 | /** |
|
691 | * Returns the path info of the currently requested URL. |
||
692 | 9 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
|
693 | 9 | * The starting and ending slashes are both removed. |
|
694 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
695 | * Note, the returned path info is already URL-decoded. |
||
696 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
697 | */ |
||
698 | public function getPathInfo() |
||
706 | |||
707 | /** |
||
708 | * Sets the path info of the current request. |
||
709 | * This method is mainly provided for testing purpose. |
||
710 | * @param string $value the path info of the current request |
||
711 | */ |
||
712 | public function setPathInfo($value) |
||
716 | |||
717 | /** |
||
718 | * Resolves the path info part of the currently requested URL. |
||
719 | * A path info refers to the part that is after the entry script and before the question mark (query string). |
||
720 | * The starting slashes are both removed (ending slashes will be kept). |
||
721 | * @return string part of the request URL that is after the entry script and before the question mark. |
||
722 | * Note, the returned path info is decoded. |
||
723 | * @throws InvalidConfigException if the path info cannot be determined due to unexpected server configuration |
||
724 | */ |
||
725 | protected function resolvePathInfo() |
||
769 | 7 | ||
770 | /** |
||
771 | * Returns the currently requested absolute URL. |
||
772 | * This is a shortcut to the concatenation of [[hostInfo]] and [[url]]. |
||
773 | 7 | * @return string the currently requested absolute URL. |
|
774 | */ |
||
775 | public function getAbsoluteUrl() |
||
779 | |||
780 | private $_url; |
||
781 | |||
782 | 16 | /** |
|
783 | * Returns the currently requested relative URL. |
||
784 | 16 | * This refers to the portion of the URL that is after the [[hostInfo]] part. |
|
785 | 16 | * It includes the [[queryString]] part if any. |
|
786 | * @return string the currently requested relative URL. Note that the URI returned is URL-encoded. |
||
787 | * @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration |
||
788 | */ |
||
789 | public function getUrl() |
||
797 | |||
798 | /** |
||
799 | * Sets the currently requested relative URL. |
||
800 | * The URI must refer to the portion that is after [[hostInfo]]. |
||
801 | * Note that the URI should be URL-encoded. |
||
802 | * @param string $value the request URI to be set |
||
803 | */ |
||
804 | public function setUrl($value) |
||
808 | |||
809 | /** |
||
810 | * Resolves the request URI portion for the currently requested URL. |
||
811 | * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. |
||
812 | * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. |
||
813 | * @return string|bool the request URI portion for the currently requested URL. |
||
814 | * Note that the URI returned is URL-encoded. |
||
815 | * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration |
||
816 | */ |
||
817 | protected function resolveRequestUri() |
||
837 | |||
838 | /** |
||
839 | 1 | * Returns part of the request URL that is after the question mark. |
|
840 | * @return string part of the request URL that is after the question mark |
||
841 | 1 | */ |
|
842 | public function getQueryString() |
||
846 | |||
847 | /** |
||
848 | 1 | * Return if the request is sent via secure channel (https). |
|
849 | * @return bool if the request is sent via secure channel (https) |
||
850 | 1 | */ |
|
851 | public function getIsSecureConnection() |
||
856 | |||
857 | /** |
||
858 | * Returns the server name. |
||
859 | * @return string server name, null if not available |
||
860 | */ |
||
861 | public function getServerName() |
||
865 | |||
866 | /** |
||
867 | * Returns the server port number. |
||
868 | * @return int|null server port number, null if not available |
||
869 | */ |
||
870 | public function getServerPort() |
||
874 | |||
875 | 14 | /** |
|
876 | * Returns the URL referrer. |
||
877 | 14 | * @return string|null URL referrer, null if not available |
|
878 | */ |
||
879 | public function getReferrer() |
||
883 | |||
884 | /** |
||
885 | * Returns the user agent. |
||
886 | * @return string|null user agent, null if not available |
||
887 | */ |
||
888 | public function getUserAgent() |
||
892 | 10 | ||
893 | /** |
||
894 | 10 | * Returns the user IP address. |
|
895 | * @return string|null user IP address, null if not available |
||
896 | */ |
||
897 | public function getUserIP() |
||
901 | |||
902 | 10 | /** |
|
903 | * Returns the user host name. |
||
904 | * @return string|null user host name, null if not available |
||
905 | */ |
||
906 | public function getUserHost() |
||
910 | |||
911 | /** |
||
912 | * @return string|null the username sent via HTTP authentication, null if the username is not given |
||
913 | */ |
||
914 | public function getAuthUser() |
||
918 | |||
919 | /** |
||
920 | * @return string|null the password sent via HTTP authentication, null if the password is not given |
||
921 | */ |
||
922 | public function getAuthPassword() |
||
926 | |||
927 | private $_port; |
||
928 | |||
929 | /** |
||
930 | * Returns the port to use for insecure requests. |
||
931 | * Defaults to 80, or the port specified by the server if the current |
||
932 | * request is insecure. |
||
933 | * @return int port number for insecure requests. |
||
934 | * @see setPort() |
||
935 | */ |
||
936 | public function getPort() |
||
944 | |||
945 | /** |
||
946 | * Sets the port to use for insecure requests. |
||
947 | * This setter is provided in case a custom port is necessary for certain |
||
948 | * server configurations. |
||
949 | * @param int $value port number. |
||
950 | */ |
||
951 | public function setPort($value) |
||
958 | |||
959 | private $_securePort; |
||
960 | |||
961 | /** |
||
962 | * Returns the port to use for secure requests. |
||
963 | * Defaults to 443, or the port specified by the server if the current |
||
964 | * request is secure. |
||
965 | * @return int port number for secure requests. |
||
966 | * @see setSecurePort() |
||
967 | */ |
||
968 | public function getSecurePort() |
||
976 | |||
977 | /** |
||
978 | * Sets the port to use for secure requests. |
||
979 | * This setter is provided in case a custom port is necessary for certain |
||
980 | * server configurations. |
||
981 | * @param int $value port number. |
||
982 | */ |
||
983 | public function setSecurePort($value) |
||
990 | |||
991 | 2 | private $_contentTypes; |
|
992 | |||
993 | 2 | /** |
|
994 | 2 | * Returns the content types acceptable by the end user. |
|
995 | 2 | * This is determined by the `Accept` HTTP header. For example, |
|
996 | 2 | * |
|
997 | 1 | * ```php |
|
998 | * $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
999 | 2 | * $types = $request->getAcceptableContentTypes(); |
|
1000 | * print_r($types); |
||
1001 | 2 | * // displays: |
|
1002 | * // [ |
||
1003 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
||
1004 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
||
1005 | * // 'text/plain' => ['q' => 0.5], |
||
1006 | * // ] |
||
1007 | * ``` |
||
1008 | * |
||
1009 | * @return array the content types ordered by the quality score. Types with the highest scores |
||
1010 | * will be returned first. The array keys are the content types, while the array values |
||
1011 | * are the corresponding quality score and other parameters as given in the header. |
||
1012 | */ |
||
1013 | public function getAcceptableContentTypes() |
||
1025 | |||
1026 | /** |
||
1027 | * Sets the acceptable content types. |
||
1028 | * Please refer to [[getAcceptableContentTypes()]] on the format of the parameter. |
||
1029 | * @param array $value the content types that are acceptable by the end user. They should |
||
1030 | * be ordered by the preference level. |
||
1031 | * @see getAcceptableContentTypes() |
||
1032 | * @see parseAcceptHeader() |
||
1033 | */ |
||
1034 | public function setAcceptableContentTypes($value) |
||
1038 | |||
1039 | /** |
||
1040 | * Returns request content-type |
||
1041 | * The Content-Type header field indicates the MIME type of the data |
||
1042 | * contained in [[getRawBody()]] or, in the case of the HEAD method, the |
||
1043 | * media type that would have been sent had the request been a GET. |
||
1044 | * For the MIME-types the user expects in response, see [[acceptableContentTypes]]. |
||
1045 | * @return string request content-type. Null is returned if this information is not available. |
||
1046 | * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17 |
||
1047 | 1 | * HTTP 1.1 header field definitions |
|
1048 | */ |
||
1049 | 1 | public function getContentType() |
|
1060 | |||
1061 | private $_languages; |
||
1062 | |||
1063 | /** |
||
1064 | 1 | * Returns the languages acceptable by the end user. |
|
1065 | * This is determined by the `Accept-Language` HTTP header. |
||
1066 | 1 | * @return array the languages ordered by the preference level. The first element |
|
1067 | 1 | * represents the most preferred language. |
|
1068 | */ |
||
1069 | public function getAcceptableLanguages() |
||
1081 | |||
1082 | /** |
||
1083 | * @param array $value the languages that are acceptable by the end user. They should |
||
1084 | * be ordered by the preference level. |
||
1085 | */ |
||
1086 | public function setAcceptableLanguages($value) |
||
1090 | |||
1091 | /** |
||
1092 | * Parses the given `Accept` (or `Accept-Language`) header. |
||
1093 | 3 | * |
|
1094 | * This method will return the acceptable values with their quality scores and the corresponding parameters |
||
1095 | 3 | * as specified in the given `Accept` header. The array keys of the return value are the acceptable values, |
|
1096 | 3 | * while the array values consisting of the corresponding quality scores and parameters. The acceptable |
|
1097 | 3 | * values with the highest quality scores will be returned first. For example, |
|
1098 | 3 | * |
|
1099 | 1 | * ```php |
|
1100 | * $header = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; |
||
1101 | * $accepts = $request->parseAcceptHeader($header); |
||
1102 | 3 | * print_r($accepts); |
|
1103 | 3 | * // displays: |
|
1104 | 3 | * // [ |
|
1105 | 2 | * // 'application/json' => ['q' => 1, 'version' => '1.0'], |
|
1106 | 2 | * // 'application/xml' => ['q' => 1, 'version' => '2.0'], |
|
1107 | 2 | * // 'text/plain' => ['q' => 0.5], |
|
1108 | 2 | * // ] |
|
1109 | 2 | * ``` |
|
1110 | 1 | * |
|
1111 | * @param string $header the header to be parsed |
||
1112 | 2 | * @return array the acceptable values ordered by their quality score. The values with the highest scores |
|
1113 | 1 | * will be returned first. |
|
1114 | */ |
||
1115 | 3 | public function parseAcceptHeader($header) |
|
1174 | |||
1175 | 1 | /** |
|
1176 | * Returns the user-preferred language that should be used by this application. |
||
1177 | 1 | * The language resolution is based on the user preferred languages and the languages |
|
1178 | 1 | * supported by the application. The method will try to find the best match. |
|
1179 | * @param array $languages a list of the languages supported by the application. If this is empty, the current |
||
1180 | 1 | * application language will be returned without further processing. |
|
1181 | * @return string the language that the application should use. |
||
1182 | */ |
||
1183 | public function getPreferredLanguage(array $languages = []) |
||
1204 | |||
1205 | /** |
||
1206 | * Gets the Etags. |
||
1207 | * |
||
1208 | * @return array The entity tags |
||
1209 | */ |
||
1210 | public function getETags() |
||
1218 | 24 | ||
1219 | 24 | /** |
|
1220 | * Returns the cookie collection. |
||
1221 | 24 | * Through the returned cookie collection, you may access a cookie using the following syntax: |
|
1222 | * |
||
1223 | * ```php |
||
1224 | * $cookie = $request->cookies['name'] |
||
1225 | * if ($cookie !== null) { |
||
1226 | * $value = $cookie->value; |
||
1227 | * } |
||
1228 | * |
||
1229 | 24 | * // alternatively |
|
1230 | * $value = $request->cookies->getValue('name'); |
||
1231 | 24 | * ``` |
|
1232 | 24 | * |
|
1233 | 24 | * @return CookieCollection the cookie collection. |
|
1234 | */ |
||
1235 | public function getCookies() |
||
1245 | |||
1246 | /** |
||
1247 | * Converts `$_COOKIE` into an array of [[Cookie]]. |
||
1248 | * @return array the cookies obtained from request |
||
1249 | * @throws InvalidConfigException if [[cookieValidationKey]] is not set when [[enableCookieValidation]] is true |
||
1250 | */ |
||
1251 | protected function loadCookies() |
||
1287 | 27 | ||
1288 | 27 | private $_csrfToken; |
|
1289 | |||
1290 | 27 | /** |
|
1291 | * Returns the token used to perform CSRF validation. |
||
1292 | * |
||
1293 | * This token is generated in a way to prevent [BREACH attacks](http://breachattack.com/). It may be passed |
||
1294 | * along via a hidden field of an HTML form or an HTTP header value to support CSRF validation. |
||
1295 | * @param bool $regenerate whether to regenerate CSRF token. When this parameter is true, each time |
||
1296 | * this method is called, a new CSRF token will be generated and persisted (in session or cookie). |
||
1297 | * @return string the token used to perform CSRF validation. |
||
1298 | 27 | */ |
|
1299 | public function getCsrfToken($regenerate = false) |
||
1314 | 25 | ||
1315 | 24 | /** |
|
1316 | 24 | * Loads the CSRF token from cookie or session. |
|
1317 | 24 | * @return string the CSRF token loaded from cookie or session. Null is returned if the cookie or session |
|
1318 | 1 | * does not have CSRF token. |
|
1319 | */ |
||
1320 | 25 | protected function loadCsrfToken() |
|
1328 | |||
1329 | /** |
||
1330 | 27 | * Generates an unmasked random token used to perform CSRF validation. |
|
1331 | * @return string the random token for CSRF validation. |
||
1332 | 27 | */ |
|
1333 | 27 | protected function generateCsrfToken() |
|
1344 | |||
1345 | /** |
||
1346 | 3 | * Returns the XOR result of two strings. |
|
1347 | * If the two strings are of different lengths, the shorter one will be padded to the length of the longer one. |
||
1348 | 3 | * @param string $token1 |
|
1349 | 3 | * @param string $token2 |
|
1350 | * @return string the XOR result |
||
1351 | */ |
||
1352 | private function xorTokens($token1, $token2) |
||
1364 | 24 | ||
1365 | /** |
||
1366 | * @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. |
||
1367 | */ |
||
1368 | public function getCsrfTokenFromHeader() |
||
1373 | |||
1374 | /** |
||
1375 | * Creates a cookie with a randomly generated CSRF token. |
||
1376 | * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. |
||
1377 | * @param string $token the CSRF token |
||
1378 | * @return Cookie the generated cookie |
||
1379 | * @see enableCsrfValidation |
||
1380 | */ |
||
1381 | 3 | protected function createCsrfCookie($token) |
|
1388 | |||
1389 | 3 | /** |
|
1390 | * Performs the CSRF validation. |
||
1391 | 3 | * |
|
1392 | 1 | * This method will validate the user-provided CSRF token by comparing it with the one stored in cookie or session. |
|
1393 | * This method is mainly called in [[Controller::beforeAction()]]. |
||
1394 | 3 | * |
|
1395 | 3 | * Note that the method will NOT perform CSRF validation if [[enableCsrfValidation]] is false or the HTTP method |
|
1396 | * is among GET, HEAD or OPTIONS. |
||
1397 | * |
||
1398 | * @param string $token the user-provided CSRF token to be validated. If null, the token will be retrieved from |
||
1399 | * the [[csrfParam]] POST field or HTTP header. |
||
1400 | * This parameter is available since version 2.0.4. |
||
1401 | * @return bool whether CSRF token is valid. If [[enableCsrfValidation]] is false, this method will return true. |
||
1402 | */ |
||
1403 | public function validateCsrfToken($token = null) |
||
1420 | |||
1421 | 3 | /** |
|
1422 | * Validates CSRF token |
||
1423 | * |
||
1424 | * @param string $token |
||
1425 | * @param string $trueToken |
||
1426 | * @return bool |
||
1427 | */ |
||
1428 | private function validateCsrfTokenInternal($token, $trueToken) |
||
1445 | } |
||
1446 |