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
|
|
|
private static function getSchemeFrom(&$proxy) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
if (!preg_match('@^([a-z0-9]*)://@', $proxy, $match)) { |
17
|
|
|
return 'http://'; |
18
|
|
|
} |
19
|
|
|
$scheme = $match[1].'://'; |
20
|
|
|
$proxy = str_replace($scheme, '', $proxy); |
21
|
|
|
|
22
|
|
|
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
|
|
|
public static function httpParseHeaders($raw_headers) |
35
|
|
|
{ |
36
|
|
|
if (function_exists('http_parse_headers')) { |
37
|
|
|
http_parse_headers($raw_headers); |
38
|
|
|
} |
39
|
|
|
$headers = []; |
40
|
|
|
$key = ''; |
41
|
|
|
foreach (explode("\n", $raw_headers) as $i => $h) { |
42
|
|
|
$h = explode(':', $h, 2); |
43
|
|
|
if (isset($h[1])) { |
44
|
|
|
if (!isset($headers[$h[0]])) { |
45
|
|
|
$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
|
|
|
$key = $h[0]; |
52
|
|
|
} else { |
53
|
|
|
if ("\t" == substr($h[0], 0, 1)) { |
54
|
|
|
$headers[$key] .= "\r\n\t".trim($h[0]); |
55
|
|
|
} elseif (!$key) { |
56
|
|
|
$headers[0] = trim($h[0]); |
57
|
|
|
} |
58
|
|
|
trim($h[0]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $headers; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function checkContentType($line, $expected) |
66
|
|
|
{ |
67
|
|
|
if (0 === stripos(trim($line), 'content-type') && false !== stripos($line, $expected)) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/* |
75
|
|
|
* No need anymore |
76
|
|
|
* Decode a string |
77
|
|
|
* |
78
|
|
|
* @param string $str String to decode |
79
|
|
|
* |
80
|
|
|
* @return string or FALSE if an error occured |
81
|
|
|
* |
82
|
|
|
public static function gzdecode(string $str) |
83
|
|
|
{ |
84
|
|
|
return gzinflate(substr($str, 10, -8)); |
85
|
|
|
} |
86
|
|
|
/**/ |
87
|
|
|
} |
88
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.