Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Http / TrustedHostsRequestFactory.php
1 <?php
2
3 namespace Drupal\Core\Http;
4
5 use Symfony\Component\HttpFoundation\Request;
6
7 /**
8  * Provides a request factory for requests using host verification.
9  *
10  * Because the trusted host patterns for requests are stored statically, they
11  * are consulted even for fake request created with Request::create(), whose
12  * host is 'localhost' by default. Such requests would fail host verification
13  * unless 'localhost' matches one of the trusted host patterns. To circumvent
14  * this problem, this factory injects the server variables from the main request
15  * into each newly created request, so that the host is correctly set even for
16  * fake requests and they properly pass host verification.
17  *
18  * @see \Drupal\Core\DrupalKernel::setupTrustedHosts()
19  */
20 class TrustedHostsRequestFactory {
21
22   /**
23    * The host of the main request.
24    *
25    * @var string
26    */
27   protected $host;
28
29   /**
30    * Creates a new TrustedHostsRequestFactory.
31    *
32    * @param string $host
33    *   The host of the main request.
34    */
35   public function __construct($host) {
36     $this->host = (string) $host;
37   }
38
39   /**
40    * Creates a new request object.
41    *
42    * @param array $query
43    *   (optional) The query (GET) or request (POST) parameters.
44    * @param array $request
45    *   (optional) An array of request variables.
46    * @param array $attributes
47    *   (optional) An array of attributes.
48    * @param array $cookies
49    *   (optional) The request cookies ($_COOKIE).
50    * @param array $files
51    *   (optional) The request files ($_FILES).
52    * @param array $server
53    *   (optional) The server parameters ($_SERVER).
54    * @param string $content
55    *   (optional) The raw body data.
56    *
57    * @return \Symfony\Component\HttpFoundation\Request
58    *   A new request object.
59    */
60   public function createRequest(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = NULL) {
61     if (empty($server['HTTP_HOST']) || ($server['HTTP_HOST'] === 'localhost' && $this->host !== 'localhost')) {
62       $server['HTTP_HOST'] = $this->host;
63     }
64     return new Request($query, $request, $attributes, $cookies, $files, $server, $content);
65   }
66
67 }