createFromServer() public static method

Creates a new Headers instance from the given $_SERVER-superglobal-like array.
public static createFromServer ( array $server ) : Headers
$server array An array similar or equal to $_SERVER, containing headers in the form of "HTTP_FOO_BAR"
return Headers
Esempio n. 1
0
 /**
  * Constructs a new Request object based on the given environment data.
  *
  * @param array $get Data similar to that which is typically provided by $_GET
  * @param array $post Data similar to that which is typically provided by $_POST
  * @param array $files Data similar to that which is typically provided by $_FILES
  * @param array $server Data similar to that which is typically provided by $_SERVER
  * @see create()
  * @see createFromEnvironment()
  * @api
  */
 public function __construct(array $get, array $post, array $files, array $server)
 {
     $this->headers = Headers::createFromServer($server);
     $this->setAttribute(self::ATTRIBUTE_CLIENT_IP, isset($server['REMOTE_ADDR']) ? $server['REMOTE_ADDR'] : null);
     $method = isset($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : 'GET';
     if ($method === 'POST') {
         if (isset($post['__method'])) {
             $method = $post['__method'];
         } elseif (isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
             $method = $server['HTTP_X_HTTP_METHOD_OVERRIDE'];
         } elseif (isset($server['HTTP_X_HTTP_METHOD'])) {
             $method = $server['HTTP_X_HTTP_METHOD'];
         }
     }
     $this->setMethod($method);
     $protocol = isset($server['SSL_SESSION_ID']) || isset($server['HTTPS']) && ($server['HTTPS'] === 'on' || strcmp($server['HTTPS'], '1') === 0) ? 'https' : 'http';
     $host = isset($server['HTTP_HOST']) ? $server['HTTP_HOST'] : 'localhost';
     $requestUri = isset($server['REQUEST_URI']) ? $server['REQUEST_URI'] : '/';
     if (substr($requestUri, 0, 10) === '/index.php') {
         $requestUri = '/' . ltrim(substr($requestUri, 10), '/');
     }
     $this->uri = new Uri($protocol . '://' . $host . $requestUri);
     if (isset($server['SERVER_PORT'])) {
         $this->uri->setPort($server['SERVER_PORT']);
     }
     $this->server = $server;
     $this->arguments = $this->buildUnifiedArguments($get, $post, $files);
 }
 /**
  * @test
  */
 public function createFromServerSimulatesAuthorizationHeaderIfPHPAuthVariablesArePresent()
 {
     $server = ['PHP_AUTH_USER' => 'robert', 'PHP_AUTH_PW' => 'mysecretpassword, containing a : colon ;-)'];
     $headers = Headers::createFromServer($server);
     $expectedValue = 'Basic ' . base64_encode('robert:mysecretpassword, containing a : colon ;-)');
     $this->assertEquals($expectedValue, $headers->get('Authorization'));
     $this->assertFalse($headers->has('User'));
 }