當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Request::setTrustedHosts方法代碼示例

本文整理匯總了PHP中Symfony\Component\HttpFoundation\Request::setTrustedHosts方法的典型用法代碼示例。如果您正苦於以下問題:PHP Request::setTrustedHosts方法的具體用法?PHP Request::setTrustedHosts怎麽用?PHP Request::setTrustedHosts使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\HttpFoundation\Request的用法示例。


在下文中一共展示了Request::setTrustedHosts方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testTrustedHosts

 public function testTrustedHosts()
 {
     // create a request
     $request = Request::create('/');
     // no trusted host set -> no host check
     $request->headers->set('host', 'evil.com');
     $this->assertEquals('evil.com', $request->getHost());
     // add a trusted domain and all its subdomains
     Request::setTrustedHosts(array('^([a-z]{9}\\.)?trusted\\.com$'));
     // untrusted host
     $request->headers->set('host', 'evil.com');
     try {
         $request->getHost();
         $this->fail('Request::getHost() should throw an exception when host is not trusted.');
     } catch (\UnexpectedValueException $e) {
         $this->assertEquals('Untrusted Host "evil.com"', $e->getMessage());
     }
     // trusted hosts
     $request->headers->set('host', 'trusted.com');
     $this->assertEquals('trusted.com', $request->getHost());
     $this->assertEquals(80, $request->getPort());
     $request->server->set('HTTPS', true);
     $request->headers->set('host', 'trusted.com');
     $this->assertEquals('trusted.com', $request->getHost());
     $this->assertEquals(443, $request->getPort());
     $request->server->set('HTTPS', false);
     $request->headers->set('host', 'trusted.com:8000');
     $this->assertEquals('trusted.com', $request->getHost());
     $this->assertEquals(8000, $request->getPort());
     $request->headers->set('host', 'subdomain.trusted.com');
     $this->assertEquals('subdomain.trusted.com', $request->getHost());
     // reset request for following tests
     Request::setTrustedHosts(array());
 }
開發者ID:anderson-abc,項目名稱:sf2_kowee,代碼行數:34,代碼來源:RequestTest.php

示例2: boot

 public function boot()
 {
     if ($trustedProxies = $this->container->getParameter('kernel.trusted_proxies')) {
         Request::setTrustedProxies($trustedProxies);
     } elseif ($this->container->getParameter('kernel.trust_proxy_headers')) {
         Request::trustProxyData();
         // @deprecated, to be removed in 2.3
     }
     if ($trustedHosts = $this->container->getParameter('kernel.trusted_hosts')) {
         Request::setTrustedHosts($trustedHosts);
     }
 }
開發者ID:joan16v,項目名稱:symfony2_test,代碼行數:12,代碼來源:FrameworkBundle.php

示例3: boot

 public function boot()
 {
     if ($trustedProxies = $this->container->getParameter('kernel.trusted_proxies')) {
         Request::setTrustedProxies($trustedProxies);
     }
     if ($this->container->getParameter('kernel.http_method_override')) {
         Request::enableHttpMethodParameterOverride();
     }
     if ($trustedHosts = $this->container->getParameter('kernel.trusted_hosts')) {
         Request::setTrustedHosts($trustedHosts);
     }
 }
開發者ID:TuxCoffeeCorner,項目名稱:tcc,代碼行數:12,代碼來源:FrameworkBundle.php

示例4: boot

 public function boot()
 {
     ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
     if ($trustedProxies = $this->container->getParameter('kernel.trusted_proxies')) {
         Request::setTrustedProxies($trustedProxies);
     }
     if ($this->container->getParameter('kernel.http_method_override')) {
         Request::enableHttpMethodParameterOverride();
     }
     if ($trustedHosts = $this->container->getParameter('kernel.trusted_hosts')) {
         Request::setTrustedHosts($trustedHosts);
     }
 }
開發者ID:Dren-x,項目名稱:mobit,代碼行數:13,代碼來源:FrameworkBundle.php

示例5: setupTrustedHosts

 /**
  * Sets up the lists of trusted HTTP Host headers.
  *
  * Since the HTTP Host header can be set by the user making the request, it
  * is possible to create an attack vectors against a site by overriding this.
  * Symfony provides a mechanism for creating a list of trusted Host values.
  *
  * Host patterns (as regular expressions) can be configured throught
  * settings.php for multisite installations, sites using ServerAlias without
  * canonical redirection, or configurations where the site responds to default
  * requests. For example,
  *
  * @code
  * $settings['trusted_host_patterns'] = array(
  *   '^example\.com$',
  *   '^*.example\.com$',
  * );
  * @endcode
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request object.
  * @param array $host_patterns
  *   The array of trusted host patterns.
  *
  * @return boolean
  *   TRUE if the Host header is trusted, FALSE otherwise.
  *
  * @see https://www.drupal.org/node/1992030
  * @see \Drupal\Core\Http\TrustedHostsRequestFactory
  */
 protected static function setupTrustedHosts(Request $request, $host_patterns)
 {
     $request->setTrustedHosts($host_patterns);
     // Get the host, which will validate the current request.
     try {
         $host = $request->getHost();
         // Fake requests created through Request::create() without passing in the
         // server variables from the main request have a default host of
         // 'localhost'. If 'localhost' does not match any of the trusted host
         // patterns these fake requests would fail the host verification. Instead,
         // TrustedHostsRequestFactory makes sure to pass in the server variables
         // from the main request.
         $request_factory = new TrustedHostsRequestFactory($host);
         Request::setFactory([$request_factory, 'createRequest']);
     } catch (\UnexpectedValueException $e) {
         return FALSE;
     }
     return TRUE;
 }
開發者ID:Nikola-xiii,項目名稱:d8intranet,代碼行數:49,代碼來源:DrupalKernel.php


注:本文中的Symfony\Component\HttpFoundation\Request::setTrustedHosts方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。