Parse Http Headers in array

@param  string $headers
@return array
8127
public static function parse($headers) {
       $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));

       if(empty($fields)) return [];

       return array_reduce($fields, function($carry, $field) {
           $match = [];
           if (!preg_match('/([^:]+): (.+)/m', $field, $match)) return $carry;

           $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',function($matches) {
               return strtoupper($matches[0]);
           }, strtolower(trim($match[1])));

           if (!isset($carry[$match[1]])) {
               $carry[$match[1]] = trim($match[2]);
               return $carry;
           }
           
           if (is_array($carry[$match[1]])) {
               $carry[$match[1]][] = trim($match[2]);      
               return $carry;
           }
           
           $carry[$match[1]] = [$carry[$match[1]], trim($match[2])];

           return $carry;
       }, []);
    }
258920
protected function transformHeadersToServerVars(array $headers)
    {
        $server = [];
        $prefix = 'HTTP_';

        foreach ($headers as $name => $value) {
            $name = strtr(strtoupper($name), '-', '_');

            if (! Str::startsWith($name, $prefix) && $name != 'CONTENT_TYPE') {
                $name = $prefix.$name;
            }

            $server[$name] = $value;
        }

        return $server;
    }
1364999
private static function parse(string $headers): array
    {
        $header  = [];
        $matches = [];
        preg_match_all(
                '=^(.[^: ]+): ([^\r\n]*)=m',
                $headers,
                $matches,
                PREG_SET_ORDER
        );
        foreach ($matches as $line) {
            $header[$line[1]] = $line[2];
        }

        return $header;
    }
395230
public function headersToCurl(array $headers)
    {
        $this->formatedParameters = [];
        $curlHeaders              = '';

        foreach ($headers as $headerName => $values) {
            foreach ($values as $headerValue) {
                $curlHeaders .= sprintf('--header "%s: %s" ', $headerName, $headerValue);
            }
        }

        return substr($curlHeaders, 0, -1);
    }
361876
private function get_headers_from_curl_response($headers)
    {
        $headers = explode("\r\n", $headers);
        array_shift($headers);
        foreach ($headers as $line) {
            if (strstr($line, ': ')) {
                list($key, $value) = explode(': ', $line);
                $result[$key] = $value;
            }
        }
        return $result;
    }
1352656
public static function parse_headers($headers)
    {
        $a_headers = array();
        $headers   = preg_replace('/\r?\n(\t| )+/', ' ', $headers);
        $lines     = explode("\n", $headers);
        $count     = count($lines);

        for ($i=0; $i<$count; $i++) {
            if ($p = strpos($lines[$i], ': ')) {
                $field = strtolower(substr($lines[$i], 0, $p));
                $value = trim(substr($lines[$i], $p+1));
                if (!empty($value)) {
                    $a_headers[$field] = $value;
                }
            }
        }

        return $a_headers;
    }
236518
protected function getHeadersAsArray($rawHeaders)
    {
        $headers = [];

        foreach (explode("\r\n", (string) $rawHeaders) as $header) {
            if (false !== strpos($header, ':')) {
                list($name, $headerStr) = explode(':', $header, 2);
                $headers[trim($name)] = trim($headerStr);
            }
        }

        return $headers;
    }
692661
protected function getRequestHeaders(array $headers)
    {
        $requestHeaders = '';

        foreach ($this->headers as $name => $value) {
            if (!isset($headers[$name])) {
                $requestHeaders .= "$name: $value\r\n";
            }
        }

        foreach ($headers as $name => $value) {
            if (is_numeric($name)) {
                throw new RuntimeException("Invalid HTTP header name $name");
            }

            $requestHeaders .= "$name: $value\r\n";
        }

        return $requestHeaders;
    }
245463
private function parseHeaders(array $headers)
    {

        // Set the raw headers as we got from the constructor.
        $this->raw_headers = $headers;

        // flatten the headers array so that values are not arrays themselves
        // but rather simple key value pairs.
        $headers = array_map(function ($value) {

            if (! is_array($value))
                return $value;

            return implode(';', $value);
        }, $headers);

        // Set the parsed headers.
        $this->headers = $headers;

        // Check for some header values that might be interesting
        // such as the current error limit and number of pages
        // available.
        $this->hasHeader('X-Esi-Error-Limit-Remain') ?
            $this->error_limit = (int) $this->getHeader('X-Esi-Error-Limit-Remain') : null;

        $this->hasHeader('X-Pages') ? $this->pages = (int) $this->getHeader('X-Pages') : null;
    }
742230
private function parseRateLimitHeaders($headers) {
        if ($headers === null) {
            return null;
        }

        $header_lines = explode("\n", $headers);
        $rate_limit_headers = [ ];
        foreach ($header_lines as $hl) {
            if (strpos($hl, "X-Rate-Limit-Limit") !== FALSE && !isset($rate_limit_headers['limit'])) {
                list($header_name, $header_value) = explode(":", $hl, 2);
                $rate_limit_headers['limit'] = intval($header_value);
            } else if (strpos($hl, "X-Rate-Limit-Remaining") !== FALSE && !isset($rate_limit_headers['remaining'])) {
                list($header_name, $header_value) = explode(":", $hl, 2);
                $rate_limit_headers['remaining'] = intval($header_value);
            } else if (strpos($hl, "X-Rate-Limit-Reset") !== FALSE && !isset($rate_limit_headers['reset'])) {
                list($header_name, $header_value) = explode(":", $hl, 2);
                $rate_limit_headers['reset'] = intval($header_value);
            }

            if (count($rate_limit_headers) === 3) {
                return $rate_limit_headers;
            }
        }

        return null;
    }
357025
protected function getHeaderArray(string $headers): array
    {
        $headerArray  = [];
        $headersArray = explode("\r\n", trim($headers));
        foreach ($headersArray as $line => $header) {
            if ($line > 0) {
                $pair                    = explode(': ', $header);
                $headerArray[$pair[0]][] = $pair[1];
            } else {
                $headerArray['status'][] = $header;
            }
        }

        return $headerArray;
    }
911010
protected function parseHeaders($headers)
    {
        $headers = str_replace("\r\n", "\n", $headers);
        $headers = explode("\n", $headers);

        array_shift($headers);

        $parsedHeaders = [];
        foreach ($headers as $header) {
            list($key, $value) = explode(':', $header, 2);

            $parsedHeaders[$key] = trim($value);
        }

        return $parsedHeaders;
    }
297907
protected function setHeaders(array $headers)
    {
        $this->headers = [];
        
        foreach ($headers as $name => $values) {
            $this->assertHeaderName($name);
            $this->assertHeaderValue($values);
        
            $key = strtolower($name);
            $this->headers[$key] = ['name' => $name, 'values' => (array)$values];
        }
    }
406159
private function parse_headers($headers)
    {
        $result = array();

        if ($headers->subject)
            $result[] = array('Subject', rcube_mime::decode_header($headers->subject));

        // @TODO: List-Id, others?
        foreach (array('From', 'To') as $h) {
            $hl = strtolower($h);
            if ($headers->$hl) {
                $list = rcube_mime::decode_address_list($headers->$hl);
                foreach ($list as $item) {
                    if ($item['mailto']) {
                        $result[] = array($h, $item['mailto']);
                    }
                }
            }
        }

        return $result;
    }
661357
private function parseHttpHeaders($rawHeaders) {
		// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
		$headers = array();
		$key = '';

		foreach (explode("\n", $rawHeaders) as $h) {
			$h = explode(':', $h, 2);

			if (isset($h[1])) {
				if (!isset($headers[$h[0]])) {
					$headers[$h[0]] = trim($h[1]);
				} elseif (is_array($headers[$h[0]])) {
					$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
				} else {
					$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
				}

				$key = $h[0];
			} else {
				if (substr($h[0], 0, 1) === "\t") {
					$headers[$key] .= "\r\n\t".trim($h[0]);
				} elseif (!$key) {
					$headers[0] = trim($h[0]);
				}
				trim($h[0]);
			}
		}

		return $headers;
	}
999039
private function parseHeader($header)
    {
        list($key, $value) = explode(": ", trim($header), 2);
        return array(strtolower($key), $value);
    }
1109569
protected function flattenHeaders(array $headers)
    {
        foreach ($headers as &$value) {
            $value = is_array($value) ? end($value) : $value;
        }
        
        return $headers;
    }