private function parseHeaders($headers): array {
        if (is_string($headers)) {
            $headers = explode("\r\n", $headers);
        }

        if (empty($headers)) {
            return [];
        }

        $result = [];
        foreach ($headers as $key => $line) {
            if (is_numeric($key)) {
                if (strpos($line, 'HTTP/') === 0) {
                    // Strip the status line and restart.
                    $result = [];
                    continue;
                } elseif (strstr($line, ': ')) {
                    list($key, $line) = explode(': ', $line);
                } else {
                    continue;
                }
            }
            if (is_array($line)) {
                $result[$key] = array_merge($result[$key] ?? [], $line);
            } else {
                $result[$key][] = $line;
            }
        }

        return $result;
    }