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