class ServerBag

ServerBag is a container for HTTP headers from the $_SERVER variable.

@author Fabien Potencier <fabien@symfony.com> @author Bulat Shakirzyanov <mallluhuct@gmail.com> @author Robert Kiss <kepten@gmail.com>

Hierarchy

  • class \Symfony\Component\HttpFoundation\ParameterBag implements \Symfony\Component\HttpFoundation\IteratorAggregate, \Symfony\Component\HttpFoundation\Countable
    • class \Symfony\Component\HttpFoundation\ServerBag

Expanded class hierarchy of ServerBag

1 file declares its use of ServerBag
ServerBagTest.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/ServerBagTest.php

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ServerBag.php, line 21

Namespace

Symfony\Component\HttpFoundation
View source
class ServerBag extends ParameterBag {

  /**
   * Gets the HTTP headers.
   *
   * @return string
   */
  public function getHeaders() {
    $headers = array();
    foreach ($this->parameters as $key => $value) {
      if (0 === strpos($key, 'HTTP_')) {
        $headers[substr($key, 5)] = $value;
      }
      elseif (in_array($key, array(
        'CONTENT_LENGTH',
        'CONTENT_MD5',
        'CONTENT_TYPE',
      ))) {
        $headers[$key] = $value;
      }
    }
    if (isset($this->parameters['PHP_AUTH_USER'])) {
      $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
      $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] : '';
    }
    else {

      /*
       * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
       * For this workaround to work, add these lines to your .htaccess file:
       * RewriteCond %{HTTP:Authorization} ^(.+)$
       * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
       *
       * A sample .htaccess file:
       * RewriteEngine On
       * RewriteCond %{HTTP:Authorization} ^(.+)$
       * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
       * RewriteCond %{REQUEST_FILENAME} !-f
       * RewriteRule ^(.*)$ app.php [QSA,L]
       */
      $authorizationHeader = null;
      if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
        $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
      }
      elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
        $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
      }

      // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
      if (null !== $authorizationHeader && 0 === stripos($authorizationHeader, 'basic')) {
        $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
        if (count($exploded) == 2) {
          list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
        }
      }
    }

    // PHP_AUTH_USER/PHP_AUTH_PW
    if (isset($headers['PHP_AUTH_USER'])) {
      $headers['AUTHORIZATION'] = 'Basic ' . base64_encode($headers['PHP_AUTH_USER'] . ':' . $headers['PHP_AUTH_PW']);
    }
    return $headers;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParameterBag::$parameters protected property Parameter storage.
ParameterBag::add public function Adds parameters. 2
ParameterBag::all public function Returns the parameters.
ParameterBag::count public function Returns the number of parameters.
ParameterBag::filter public function Filter key.
ParameterBag::get public function Returns a parameter by name.
ParameterBag::getAlnum public function Returns the alphabetic characters and digits of the parameter value.
ParameterBag::getAlpha public function Returns the alphabetic characters of the parameter value.
ParameterBag::getDigits public function Returns the digits of the parameter value.
ParameterBag::getInt public function Returns the parameter value converted to integer.
ParameterBag::getIterator public function Returns an iterator for parameters.
ParameterBag::has public function Returns true if the parameter is defined.
ParameterBag::keys public function Returns the parameter keys.
ParameterBag::remove public function Removes a parameter.
ParameterBag::replace public function Replaces the current parameters by a new set. 1
ParameterBag::set public function Sets a parameter by name. 2
ParameterBag::__construct public function Constructor. 2
ServerBag::getHeaders public function Gets the HTTP headers.