Parses headers if necessary.

- Decodes the status code and reasonphrase.
- Parses and normalizes header names + values.

@param array $headers Headers to parse.
@return void
188557
protected function _parseHeaders($headers)
    {
        foreach ($headers as $key => $value) {
            if (substr($value, 0, 5) === 'HTTP/') {
                preg_match('/HTTP\/([\d.]+) ([0-9]+)(.*)/i', $value, $matches);
                $this->protocol = $matches[1];
                $this->code = (int)$matches[2];
                $this->reasonPhrase = trim($matches[3]);
                continue;
            }
            if (strpos($value, ':') === false) {
                continue;
            }
            list($name, $value) = explode(':', $value, 2);
            $value = trim($value);
            $name = trim($name);

            $normalized = strtolower($name);

            if (isset($this->headers[$name])) {
                $this->headers[$name][] = $value;
            } else {
                $this->headers[$name] = (array)$value;
                $this->headerNames[$normalized] = $name;
            }
        }
    }
1182217
public function setHeaders($headers)
    {
        if (!empty($this->headers)) {
            $this->headers = array_merge($this->headers, $headers);
        } else {
            $this->headers = $headers;
        }
        $this->headers = array_merge($this->headers, $headers);

        if ($this->client instanceof \swoole_http_client) {
            $this->client->setHeaders($this->headers);
        }
        return $this;
    }
1363971
protected function headers($headers)
    {
        foreach($headers as $name => $lines) {
            foreach($lines as $key => $line) {
                $this->header("$name: $line", false);
            }
        }
    }
1098718
private function setRequestHeaders($headers = array()) {
        foreach ($this->headers as $key => $value) {
            $headers[] = $key . ': ' . $value;
        }
        curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
    }
1183336
protected function setResponseHeaders(array $headers)
    {
        // reset headers
        $this->headers = [];

        // set response status header
        $this->headers[] = 'HTTP/1.1 ' . $this->getStatusCode() . ' ' . $this->getStatusText();

        // set content type header
        $this->headers['Content-Type'] = 'application/json';

        // set user supplied headers
        foreach ($headers as $key => $value) {
            $this->headers[$key] = $value;
        }
    }
1330499
function setHeaders($headers) {
      if (is_array($headers)) {
        reset($headers);
        while (list($name, $value) = each($headers)) {
          $this->requestHeaders[$name] = $value;
        }
      }
    }
747750
protected function setHeaders(array $headers = array()) {
        if(count($headers) === 0) {
            return;
        }
        $this->headers = array_merge($this->headers, $headers);

        $curl_headers = [];

        $curl_version = curl_version();

        foreach ($this->headers as $key => $value) {
            $curl_headers[] = $key.': '.$value;
        }

        // add some helpful information about environment
        $server_info = array(
            'curl_version' => (isset($curl_version['version']) ? $curl_version['version'] : null),
            'php_version' => phpversion(),
            'php_sapi' => php_sapi_name()
        );
        $curl_headers[] = 'X-Server-Lang: PHP';
        $curl_headers[] = 'X-Server-Lang-Info: ' . base64_encode(json_encode($server_info));
        $curl_headers[] = 'X-Server-Software: '. (!empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : null);
        $curl_headers[] = 'X-Server-Protocol: '. (!empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : null);
        curl_setopt($this->request, CURLOPT_HTTPHEADER, $curl_headers);
    }
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;
    }
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;
    }
739910
protected function fixHeaders(array $headers)
    {
        $fixedHeaders = array();

        foreach ($headers as $key => $value) {
            if (is_int($key)) {
                $fixedHeaders[] = $value;
            } else {
                $fixedHeaders[] = sprintf('%s:%s', $key, $value);
            }
        }

        return $fixedHeaders;
    }
1279760
protected static function serializeHeaders(array $headers)
    {
        $lines = [];
        foreach ($headers as $header => $values) {
            $normalized = self::filterHeader($header);
            foreach ($values as $value) {
                $lines[] = sprintf('%s: %s', $normalized, $value);
            }
        }

        return implode("\r\n", $lines);
    }
362513
public static function setResponseHeaders($headers)
    {
        foreach ($headers as $name => $headerValues) {
            $header = $name . ": " . implode(", ", $headerValues);
            header($header);
        }
    }
978137
private function parseHeaders(array $headers)
    {
        $indexes = [];
        $metas   = [];

        foreach ($headers as $key => $value) {
            $key = strtolower($key);

            if (strpos($key, 'x-riak-index-') === 0) {
                $name  = substr($key, 13);
                $value = isset($indexes[$name])
                    ? array_merge($indexes[$name], $value)
                    : $value;

                $indexes[$name] = $value;
            }

            if (strpos($key, 'x-riak-meta-') === 0) {
                $name  = strtolower(substr($key, 12));
                $value = is_array($value)
                    ? reset($value)
                    : $value;

                $metas[$name] = $value;
            }
        }

        return [
            'indexes' => $indexes,
            'metas'   => $metas
        ];
    }
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;
    }
756253
protected function parseIncomingCookies($headers)
    {
        $this->incomingTrackerCookies = array();
        
        if (!empty($headers)) {
            $headerName = 'set-cookie:';
            $headerNameLength = strlen($headerName);
            
            foreach($headers as $header) {
                if (strpos(strtolower($header), $headerName) !== 0) {
                    continue;
                }
                $cookies = trim(substr($header, $headerNameLength));
                $posEnd = strpos($cookies, ';');
                if ($posEnd !== false) {
                    $cookies = substr($cookies, 0, $posEnd);
                }
                parse_str($cookies, $this->incomingTrackerCookies);
            }
        }   
    }
361494
protected function normalizeHeaders(array $headers)
    {
        if (empty($headers["User-Agent"]))
            $headers["User-Agent"] = "evias NEM Blockchain Wrapper";

        if (empty($headers["Accept"]))
            $headers["Accept"] = "application/json";

        if (empty($headers["Content-Type"]))
            $headers["Content-Type"] = "application/json";

        return $headers;
    }
1274629
public function setHeaders($headers)
    {
        $headerCollection = $this->getHeaderCollection();
        $headerCollection->removeAll();
        $headerCollection->fromArray($headers);
    }