Takes a $_SERVER data array and extracts HTTP request headers.

@param array $server

@return array
160310
public static function extractRequestHeaders(array $server)
    {
        $headers = array();
        foreach ($server as $key => $value) {
            if (substr($key, 0, 5) == 'HTTP_') {
                $key = substr($key, 5);
                $key = strtolower(str_replace('_', ' ', $key));
                $key = str_replace(' ', '-', ucwords($key));
                $headers[$key] = $value;
            }
        }

        if (isset($server['CONTENT_TYPE'])) {
            $headers['Content-Type'] = $server['CONTENT_TYPE'];
        }
        if (isset($server['CONTENT_LENGTH'])) {
            $headers['Content-Length'] = $server['CONTENT_LENGTH'];
        }

        // Enable HTTP Basic authentication workaround for PHP running in CGI mode with Apache
        // Depending on server configuration the auth header may be in HTTP_AUTHORIZATION or
        // REDIRECT_HTTP_AUTHORIZATION
        $authHeader = null;
        if (isset($headers['Authorization'])) {
            $authHeader = $headers['Authorization'];
        } elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) {
            $authHeader = $server['REDIRECT_HTTP_AUTHORIZATION'];
        }

        // Ensure basic auth is available via headers
        if (isset($server['PHP_AUTH_USER']) && isset($server['PHP_AUTH_PW'])) {
            // Shift PHP_AUTH_* into headers so they are available via request
            $headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER'];
            $headers['PHP_AUTH_PW'] = $server['PHP_AUTH_PW'];
        } elseif ($authHeader && preg_match('/Basic\s+(?<token>.*)$/i', $authHeader, $matches)) {
            list($name, $password) = explode(':', base64_decode($matches['token']));
            $headers['PHP_AUTH_USER'] = $name;
            $headers['PHP_AUTH_PW'] = $password;
        }

        return $headers;
    }
370718
public static function getRequestHeaders()
    {
        if (function_exists('getallheaders')) {
            $headers = array();
            foreach (getallheaders() as $name => $value) {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $name))))] = $value;
            }
            return $headers;
        }
        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
1320725
public static function parse_headers(string $headers): array
    {
        $final_headers = array();
        $list = explode("\n", trim($headers));

        $http = array_shift($list);

        foreach ($list as $header) {
            $parts = explode(':', $header, 2);
            $final_headers[trim($parts[0])] = isset($parts[1]) ? trim($parts[1]) : '';
        }

        return $final_headers;
    }
505998
public static function get_headers(): array
	{
		if (function_exists('apache_request_headers')) {
			// we need this to get the actual Authorization: header
			// because apache tends to tell us it doesn't exist
			$headers = apache_request_headers();

			// sanitize the output of apache_request_headers because
			// we always want the keys to be Cased-Like-This and arh()
			// returns the headers in the same case as they are in the
			// request
			$out = [];
			foreach ($headers as $key => $value) {
				$key = str_replace(
					' ',
					'-',
					ucwords(strtolower(str_replace('-', ' ', $key)))
				);
				$out[$key] = $value;
			}
		} else {
			// otherwise we don't have apache and are just going to have to hope
			// that $_SERVER actually contains what we need
			$out = [];
			if (isset($_SERVER['CONTENT_TYPE'])) {
				$out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
			}
			if (isset($_ENV['CONTENT_TYPE'])) {
				$out['Content-Type'] = $_ENV['CONTENT_TYPE'];
			}

			foreach ($_SERVER as $key => $value) {
				if (substr($key, 0, 5) == 'HTTP_') {
					// this is chaos, basically it is just there to capitalize the first
					// letter of every word that is not an initial HTTP and strip HTTP
					// code from przemek
					$key = str_replace(
						' ',
						'-',
						ucwords(strtolower(str_replace('_', ' ', substr($key, 5))))
					);
					$out[$key] = $value;
				}
			}
		}
		return $out;
	}
6284
public static function getHeadersFromServer(array $server): array
    {
        $headers = [];
        foreach ($server as $key => $value) {
            // Apache prefixes environment variables with REDIRECT_
            // if they are added by rewrite rules
            if (0 === \strpos($key, 'REDIRECT_')) {
                $key = \substr($key, 9);

                // We will not overwrite existing variables with the
                // prefixed versions, though
                if (\array_key_exists($key, $server)) {
                    continue;
                }
            }

            if ($value && 0 === \strpos($key, 'HTTP_')) {
                $name = \strtr(\strtolower(\substr($key, 5)), '_', '-');
                $headers[$name] = $value;

                continue;
            }

            if ($value && 0 === \strpos($key, 'CONTENT_')) {
                $name = 'content-'.\strtolower(\substr($key, 8));
                $headers[$name] = $value;

                continue;
            }
        }

        return $headers;
    }
645637
public function setServer(array $server)
    {
        $this->set('server', $server);

        //if there is no path set
        if (!$this->exists('path') && isset($server['REQUEST_URI'])) {
            $path = $server['REQUEST_URI'];

            //remove ? url queries
            if (strpos($path, '?') !== false) {
                list($path, $tmp) = explode('?', $path, 2);
            }

            $this->setPath($path);
        }

        if (!$this->exists('method') && isset($server['REQUEST_METHOD'])) {
            $this->setMethod($server['REQUEST_METHOD']);
        }

        if (!$this->exists('query') && isset($server['QUERY_STRING'])) {
            $this->setQuery($server['QUERY_STRING']);
        }

        return $this;
    }
678363
public function parseServerString($server)
    {
        $socketTransport = 'unix://';
        if (strpos($server, $socketTransport) === 0) {
            return [substr($server, strlen($socketTransport)), 0];
        }
        if (substr($server, 0, 1) === '[') {
            $position = strpos($server, ']:');
            if ($position !== false) {
                $position++;
            }
        } else {
            $position = strpos($server, ':');
        }
        $port = 11211;
        $host = $server;
        if ($position !== false) {
            $host = substr($server, 0, $position);
            $port = substr($server, $position + 1);
        }

        return [$host, (int)$port];
    }
1000897
private function getHttpHearders(array $headers)
    {
        $http_headers = array();
        foreach ($headers as $key => $value) {
            array_push($http_headers, $key . ":" . $value);
        }
        return $http_headers;
    }
510911
private function determineRemoteIpAddress(array $server): string
    {
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $server) && filter_var($server['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_X_FORWARDED_FOR'];
        } elseif (array_key_exists('HTTP_CLIENT_IP', $server) && filter_var($server['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_CLIENT_IP'];
        } elseif (array_key_exists('HTTP_TRUE_CLIENT_IP', $server) && filter_var($server['HTTP_TRUE_CLIENT_IP'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_TRUE_CLIENT_IP'];
        }

        return $server['REMOTE_ADDR'];
    }
871018
public static function createFromServer(array $server)
    {
        $headerFields = [];
        if (isset($server['PHP_AUTH_USER']) && isset($server['PHP_AUTH_PW'])) {
            $headerFields['Authorization'] = 'Basic ' . base64_encode($server['PHP_AUTH_USER'] . ':' . $server['PHP_AUTH_PW']);
        }

        foreach ($server as $name => $value) {
            if (strpos($name, 'HTTP_') === 0) {
                $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
                $headerFields[$name] = $value;
            } elseif ($name == 'REDIRECT_REMOTE_AUTHORIZATION' && !isset($headerFields['Authorization'])) {
                $headerFields['Authorization'] = $value;
            } elseif (in_array($name, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
                $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))));
                $headerFields[$name] = $value;
            }
        }
        return new self($headerFields);
    }
554090
private function getParams($method, array $server, array $post) : array
    {
        // post data exists
        if ($method === 'post' && ! empty($post)) {
            return $post;
        }

        if (\in_array($method, ['post', 'put', 'patch', 'delete'], true)) {
            return $this->phpInput($server);
        }

        return $post;
    }
1037481
public static function getallheaders()
    {
        if (function_exists('getallheaders')) {
            $return = getallheaders();
        } else {
            $return = array();
            foreach ($_SERVER as $name => $value) {
               if (substr($name, 0, 5) == 'HTTP_') {
                   $return[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
               }
            }
        }
        return !empty($return) ? $return : array();
    }
879617
public function parseRequest(array $server, IHttpRequest $request = null) {
        if ( ! $request instanceof IHttpRequest) {
            $request = $this->createRequest();
        }

        $this->setMethod($request, $server);
        $this->setUrl($request, $server);
        $this->setHeaders($request, $server);
        $this->setProtocol($request, $server);
        $this->setContent($request);
        $this->setServerGlobal($request, $server);

        return $request;
    }
199447
public function createUriFromServer(array $server)
    {
        if (isset($server['HTTPS'])) {
            if (!empty($server['HTTPS']) && $server['HTTPS'] !== 'off') {
                $scheme = 'https';
            }
        }
        if (!isset($scheme)) {
            $scheme = 'http';
        }

        $username = isset($server['PHP_AUTH_USER']) ? $server['PHP_AUTH_USER'] : '';
        $password = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : '';

        $host = isset($server['HTTP_HOST']) ? $server['HTTP_HOST'] : $server['SERVER_NAME'];

        $port = isset($server['SERVER_PORT']) ? (int)$server['SERVER_PORT'] : 80;
        if (preg_match('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/', $host, $matches)) {
            $host = $matches[1];

            if ($matches[2]) {
                $port = (int) substr($matches[2], 1);
            }
        } else {
            $pos = strpos($host, ':');
            if ($pos !== false) {
                $port = (int) substr($host, $pos + 1);
                $host = strstr($host, ':', true);
            }
        }

        $requestScriptName = parse_url($server['SCRIPT_NAME'], PHP_URL_PATH);
        $requestScriptDir = dirname($requestScriptName);

        $requestUri = parse_url('http://example.com' . $server['REQUEST_URI'], PHP_URL_PATH);

        $basePath = '';
        $virtualPath = $requestUri;
        if (stripos($requestUri, $requestScriptName) === 0) {
            $basePath = $requestScriptName;
        } elseif ($requestScriptDir !== '/' && stripos($requestUri, $requestScriptDir) === 0) {
            $basePath = $requestScriptDir;
        }

        if ($basePath) {
            $virtualPath = ltrim(substr($requestUri, strlen($basePath)), '/');
        }

        $queryString = isset($server['QUERY_STRING']) ? $server['QUERY_STRING'] : '';
        if ($queryString === '') {
            $queryString = parse_url('http://example.com' . $server['REQUEST_URI'], PHP_URL_QUERY);
        }

        $fragment = '';

        $uri = new Uri($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password);
        if ($basePath) {
            $uri = $uri->withBasePath($basePath);
        }

        return $uri;
    }
534562
public function getHeaders($names = []) {
        if (is_string($names)) {
            $names = [$names];
        }
        elseif (is_array($names)) {
            foreach ($names as $name) {
                if (!is_string($name)) throw new IllegalTypeException('Illegal argument type in argument $names: '.gettype($name));
            }
        }
        else throw new IllegalTypeException('Illegal type of parameter $names: '.gettype($names));

        // read all headers once
        static $headers = null; if ($headers === null) {
            if (function_exists('apache_request_headers')) {
                $headers = apache_request_headers();
                // TODO: Apache skips REDIRECT_* headers
            }
            else {
                static $fixHeaderNames = ['CDN'=>1, 'DNT'=>2, 'X-CDN'=>3];
                $headers = [];
                foreach ($_SERVER as $name => $value) {
                    while (substr($name, 0, 9) == 'REDIRECT_') {
                        $name = substr($name, 9);
                        if (isset($_SERVER[$name]))
                            continue 2;
                    }
                    if (substr($name, 0, 5) == 'HTTP_') {
                        $name = substr($name, 5);
                        if (!isset($fixHeaderNames[$name]))
                            $name = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower($name))));
                        $headers[$name] = $value;
                    }
                }
            }

            if (!isset($headers['Authorization'])) {
                if (isset($_SERVER['PHP_AUTH_USER'])) {
                    $passwd = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
                    $headers['Authorization'] = 'Basic '. base64_encode($_SERVER['PHP_AUTH_USER'].':'.$passwd);
                }
                elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
                    $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
                }
            }
        }

        // return all or just the specified headers
        if (!$names)
            return $headers;
        return \array_intersect_ukey($headers, \array_flip($names), 'strCaseCmp');
    }
757328
protected static function getWebServiceURL($server)
    {
        $urls = [
            static::USE_PRODUCTION_SERVER  => 'https://api.authorize.net/xml/v1/request.api',
            static::USE_DEVELOPMENT_SERVER => 'https://apitest.authorize.net/xml/v1/request.api',
            static::USE_AKAMAI_SERVER      => 'https://api2.authorize.net/xml/v1/request.api'
        ];
        if (array_key_exists($server, $urls)) {
            return $urls[$server];
        }
        throw new AuthnetInvalidServerException('You did not provide a valid server.');
    }
955044
protected function getHeaders()
    {
        if (function_exists('getallheaders')) {
            return getallheaders();
        }
        $arr_headers = [];
        foreach ($_SERVER as $str_key => $str_value) {
            if (strpos($str_key, 'HTTP_') === 0) {
                $arr_headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($str_key, 5)))))] = $str_value;
            }
        }
        return $arr_headers;
    }