Passed
Push — master ( 609429...64b5cb )
by Dev
02:13
created

Helper::httpParseHeaders()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 14
nop 1
dl 0
loc 29
ccs 15
cts 20
cp 0.75
crap 9
rs 8.4444
c 0
b 0
f 0
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 9
    public static function httpParseHeaders($raw_headers)
35
    {
36 9
        if (function_exists('http_parse_headers')) {
37
            http_parse_headers($raw_headers);
38
        }
39 9
        $headers = [];
40 9
        $key = '';
41 9
        foreach (explode("\n", $raw_headers) as $i => $h) {
42 9
            $h = explode(':', $h, 2);
43 9
            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 9
                if ("\t" == substr($h[0], 0, 1)) {
54
                    $headers[$key] .= "\r\n\t".trim($h[0]);
55 9
                } elseif (!$key) {
56 9
                    $headers[0] = trim($h[0]);
57
                }
58 9
                trim($h[0]);
59
            }
60
        }
61
62 9
        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 3
    public static function parseHeader($header)
79
    {
80 3
        static $trimmed = "\"'  \n\t\r";
81 3
        $params = $matches = [];
82 3
        foreach (self::normalizeHeader($header) as $val) {
83 3
            $part = [];
84 3
            foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
85 3
                if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
86 3
                    $m = $matches[0];
87 3
                    if (isset($m[1])) {
88 3
                        $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed);
89
                    } else {
90 3
                        $part[] = trim($m[0], $trimmed);
91
                    }
92
                }
93
            }
94 3
            if ($part) {
95 3
                $params[] = $part;
96
            }
97
        }
98
99 3
        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 3
    protected static function normalizeHeader($header)
114
    {
115 3
        if (!is_array($header)) {
116 3
            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