src/Net/Url.php
public static function fromHttp(array $overrides = [], $trust_x_forwarded_headers = true)
  {
    $self = new static("");
    $parts = [];

    if($trust_x_forwarded_headers)
    {
      foreach(array(
        'scheme' => "HTTP_X_FORWARDED_PROTO",
        'port' => "HTTP_X_FORWARDED_PORT",
      ) as $key => $header)
      {
        if(!empty($_SERVER[$header]))
        {
          $parts += [$key => $_SERVER[$header]];
        }
      }

      if(!empty($_SERVER["HTTP_X_FORWARDED_HOST"]))
      {
        $parts += $self->_parse_url("//{$_SERVER["HTTP_X_FORWARDED_HOST"]}");
      }
    }

    if(!empty($_SERVER["REQUEST_URI"]))
    {
      $parts += $self->_parse_url($_SERVER["REQUEST_URI"]);
    }

    foreach(array(
      'scheme' => "REQUEST_SCHEME",
      'host' => "SERVER_NAME",
      'port' => "SERVER_PORT",
    ) as $key => $header)
    {
      if(!isset($parts[$key]) && !empty($_SERVER[$header]))
      {
        $parts += [$key => $_SERVER[$header]];
      }
    }

    /** Parse Host: header as host[:port] */
    if(empty($parts['host']) && !empty($_SERVER['HTTP_HOST']))
    {
      $parts += $self->_parse_url("//{$_SERVER['HTTP_HOST']}");
    }

    if(!empty($parts['host']))
    {
      $parts['host'] = idn_to_utf8($parts['host'], 0, INTL_IDNA_VARIANT_UTS46);
    }

    return $self->setParts($overrides + $parts);
  }
public static function fromHttp(array $overrides = [], $trust_x_forwarded_headers = true)
  {
    $self = new static("");
    $parts = [];

    if($trust_x_forwarded_headers)
    {
      foreach(array(
        'scheme' => "HTTP_X_FORWARDED_PROTO",
        'port' => "HTTP_X_FORWARDED_PORT",
      ) as $key => $header)
      {
        if(!empty($_SERVER[$header]))
        {
          $parts += [$key => $_SERVER[$header]];
        }
      }

      if(!empty($_SERVER["HTTP_X_FORWARDED_HOST"]))
      {
        $parts += $self->_parse_url("//{$_SERVER["HTTP_X_FORWARDED_HOST"]}");
      }
    }

    if(!empty($_SERVER["REQUEST_URI"]))
    {
      $parts += $self->_parse_url($_SERVER["REQUEST_URI"]);
    }

    foreach(array(
      'scheme' => "REQUEST_SCHEME",
      'host' => "SERVER_NAME",
      'port' => "SERVER_PORT",
    ) as $key => $header)
    {
      if(!isset($parts[$key]) && !empty($_SERVER[$header]))
      {
        $parts += [$key => $_SERVER[$header]];
      }
    }

    /** Parse Host: header as host[:port] */
    if(empty($parts['host']) && !empty($_SERVER['HTTP_HOST']))
    {
      $parts += $self->_parse_url("//{$_SERVER['HTTP_HOST']}");
    }

    if(!empty($parts['host']))
    {
      $parts['host'] = idn_to_utf8($parts['host'], 0, INTL_IDNA_VARIANT_UTS46);
    }

    return $self->setParts($overrides + $parts);
  }
public static function fromHttp ( array $ overrides = [ ] , $ trust_x_forwarded_headers = true ) { $ self = new static ( "" ) ; $ parts = [ ] ; if ( $ trust_x_forwarded_headers ) { foreach ( array ( 'scheme' => "HTTP_X_FORWARDED_PROTO" , 'port' => "HTTP_X_FORWARDED_PORT" , ) as $ key => $ header ) { if ( ! empty ( $ _SERVER [ $ header ] ) ) { $ parts += [ $ key => $ _SERVER [ $ header ] ] ; } } if ( ! empty ( $ _SERVER [ "HTTP_X_FORWARDED_HOST" ] ) ) { $ parts += $ self -> _parse_url ( "//{$_SERVER[" HTTP_X_FORWARDED_HOST "]}" ) ; } } if ( ! empty ( $ _SERVER [ "REQUEST_URI" ] ) ) { $ parts += $ self -> _parse_url ( $ _SERVER [ "REQUEST_URI" ] ) ; } foreach ( array ( 'scheme' => "REQUEST_SCHEME" , 'host' => "SERVER_NAME" , 'port' => "SERVER_PORT" , ) as $ key => $ header ) { if ( ! isset ( $ parts [ $ key ] ) && ! empty ( $ _SERVER [ $ header ] ) ) { $ parts += [ $ key => $ _SERVER [ $ header ] ] ; } } /** Parse Host: header as host[:port] */ if ( empty ( $ parts [ 'host' ] ) && ! empty ( $ _SERVER [ 'HTTP_HOST' ] ) ) { $ parts += $ self -> _parse_url ( "//{$_SERVER['HTTP_HOST']}" ) ; } if ( ! empty ( $ parts [ 'host' ] ) ) { $ parts [ 'host' ] = idn_to_utf8 ( $ parts [ 'host' ] , 0 , INTL_IDNA_VARIANT_UTS46 ) ; } return $ self -> setParts ( $ overrides + $ parts ) ; }
Creates Url instance from HTTP request metadata, such as $_SERVER[REQUEST_*] and $_SERVER[HTTP_*] variables.

Function uses data available in the request environment to construct the so-called self-reference.

The order in which environment is processed is as follows:

1. X-Forwarded-* headers, if trusted.
2. The entire REQUEST_URI variable as set by SAPI.
3. REQUEST_SCHEME / SERVER_NAME / SERVER_PORT variables from SAPI.
4. The Host header in case host part was not set yet.

If $trust_x_forwarded_headers is true (the default), the following headers are considered:

- X-Forwarded-Proto - original protocol the request received with (f.e. https);
- X-Forwarded-Port - original receiving port;
- X-Forwarded-Host - host (and possible port, if not set by X-Forwarded-Port) the original request received for.

Warning: It is stronly recommended to configure calling webserver to override X-Forwarded-* headers an application
should not trust.

Warning: Application should receive an already sanitized environment it could rely upon.

@param array $override An array of parts to override. {@see \AnrDaemon\Net\Url::setParts() self::setParts()}
@param bool $trust_x_forwarded_headers To trust the X-Forwarded-{Host,Port,Proto,etc.} headers or not.
@return Url A new class instance.
Creates Url instance from HTTP request metadata such as $_SERVER [ REQUEST_ * ] and $_SERVER [ HTTP_ * ] variables .