3 namespace Drupal\Core\Http;
5 use Symfony\Component\HttpFoundation\Request;
8 * Provides a request factory for requests using host verification.
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.
18 * @see \Drupal\Core\DrupalKernel::setupTrustedHosts()
20 class TrustedHostsRequestFactory {
23 * The host of the main request.
30 * Creates a new TrustedHostsRequestFactory.
33 * The host of the main request.
35 public function __construct($host) {
36 $this->host = (string) $host;
40 * Creates a new request object.
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).
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.
57 * @return \Symfony\Component\HttpFoundation\Request
58 * A new request object.
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;
64 return new Request($query, $request, $attributes, $cookies, $files, $server, $content);