Helper::parseHeader()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 9
nop 1
crap 6
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class Helper
6
{
7
    /**
8
     * Return scheme from proxy string and remove Scheme From proxy.
9
     *
10
     * @param string $proxy
11
     *
12
     * @return string
13
     */
14 6
    public static function getSchemeFrom(&$proxy)
15
    {
16 6
        if (!preg_match('@^([a-z0-9]*)://@', $proxy, $match)) {
17 6
            return 'http://';
18
        }
19 3
        $scheme = $match[1].'://';
20 3
        $proxy = substr($proxy, strlen($scheme));
21
22 3
        return $scheme;
23
    }
24
25
    /**
26
     * Parse HTTP headers (php HTTP functions but generally, this packet isn't installed).
27
     *
28
     * @source http://www.php.net/manual/en/function.http-parse-headers.php#112917
29
     *
30
     * @param string $raw_headers Contain HTTP headers
31
     *
32
     * @return bool|array an array on success or FALSE on failure
33
     */
34 12
    public static function httpParseHeaders($raw_headers)
35
    {
36 12
        if (function_exists('http_parse_headers')) {
37
            http_parse_headers($raw_headers);
38
        }
39 12
        $headers = [];
40 12
        $key = '';
41 12
        foreach (explode("\n", $raw_headers) as $i => $h) {
42 12
            $h = explode(':', $h, 2);
43 12
            if (isset($h[1])) {
44 6
                if (!isset($headers[$h[0]])) {
45 6
                    $headers[$h[0]] = trim($h[1]);
46
                } elseif (is_array($headers[$h[0]])) {
47
                    $headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
48
                } else {
49
                    $headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
50
                }
51 6
                $key = $h[0];
52
            } else {
53 12
                if ("\t" == substr($h[0], 0, 1)) {
54
                    $headers[$key] .= "\r\n\t".trim($h[0]);
55 12
                } elseif (!$key) {
56 12
                    $headers[0] = trim($h[0]);
57
                }
58 12
                trim($h[0]);
59
            }
60
        }
61
62 12
        return $headers;
63
    }
64
65
    /**
66
     * This is taken from the GuzzleHTTP/PSR7 library,
67
     * see https://github.com/guzzle/psr7 for more info.
68
     *
69
     * Parse an array of header values containing ";" separated data into an
70
     * array of associative arrays representing the header key value pair
71
     * data of the header. When a parameter does not contain a value, but just
72
     * contains a key, this function will inject a key with a '' string value.
73
     *
74
     * @param string|array $header header to parse into components
75
     *
76
     * @return array returns the parsed header values
77
     */
78 6
    public static function parseHeader($header)
79
    {
80 6
        static $trimmed = "\"'  \n\t\r";
81 6
        $params = $matches = [];
82 6
        foreach (self::normalizeHeader($header) as $val) {
83 6
            $part = [];
84 6
            foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
85 6
                if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
86 6
                    $m = $matches[0];
87 6
                    if (isset($m[1])) {
88 6
                        $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed);
89
                    } else {
90 6
                        $part[] = trim($m[0], $trimmed);
91
                    }
92
                }
93
            }
94 6
            if ($part) {
95 6
                $params[] = $part;
96
            }
97
        }
98
99 6
        return $params;
100
    }
101
102
    /**
103
     * This is taken from the GuzzleHTTP/PSR7 library,
104
     * see https://github.com/guzzle/psr7 for more info.
105
     *
106
     * Converts an array of header values that may contain comma separated
107
     * headers into an array of headers with no comma separated values.
108
     *
109
     * @param string|array $header header to normalize
110
     *
111
     * @return array returns the normalized header field values
112
     */
113 6
    protected static function normalizeHeader($header)
114
    {
115 6
        if (!is_array($header)) {
116 6
            return array_map('trim', explode(',', $header));
117
        }
118
        $result = [];
119
        foreach ($header as $value) {
120
            foreach ((array) $value as $v) {
121
                if (false === strpos($v, ',')) {
122
                    $result[] = $v;
123
                    continue;
124
                }
125
                foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $v) as $vv) {
126
                    $result[] = trim($vv);
127
                }
128
            }
129
        }
130
131
        return $result;
132
    }
133
134 15
    public static function checkContentType($line, $expected = 'text/html')
135
    {
136 15
        return 0 === stripos(trim($line), 'content-type') && false !== stripos($line, $expected);
137
    }
138
139 9
    public static function checkStatusCode($line, $expected = 200)
140
    {
141 9
        return 0 === stripos(trim($line), 'http') && false !== stripos($line, ' '.$expected.' ');
142
    }
143
}
144