You are here

protected static function Request::parseHeadersFromGlobals in RESTful 7.2

Parses the header names and values from globals.

Return value

HttpHeaderBag The headers.

1 call to Request::parseHeadersFromGlobals()
Request::createFromGlobals in src/Http/Request.php
Creates a new request with values from PHP's super globals.

File

src/Http/Request.php, line 386
Contains \Drupal\restful\Http\Request

Class

Request
Deals with everything coming from the consumer.

Namespace

Drupal\restful\Http

Code

protected static function parseHeadersFromGlobals() {
  $bag = new HttpHeaderBag();
  $headers = array();
  if (function_exists('apache_request_headers')) {
    $headers = apache_request_headers();
  }
  else {
    $content_header_keys = array(
      'CONTENT_TYPE',
      'CONTENT_LENGTH',
    );
    foreach ($_SERVER as $key => $value) {
      if (strpos($key, 'HTTP_') === 0 || in_array($key, $content_header_keys)) {

        // Generate the plausible header name based on the $name.
        // Converts 'HTTP_X_FORWARDED_FOR' to 'X-Forwarded-For'
        $name = preg_replace('/^HTTP_/', '', $key);
        $parts = explode('_', $name);
        $parts = array_map('strtolower', $parts);
        $parts = array_map('ucfirst', $parts);
        $name = implode('-', $parts);
        $headers[$name] = $value;
      }
    }
  }

  // Iterate over the headers and bag them.
  foreach ($headers as $name => $value) {
    $bag
      ->add(HttpHeader::create($name, $value));
  }
  return $bag;
}