1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Clients\HTTP; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Network\Client; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package NetworkClients |
8
|
|
|
* @subpackage HTTPClient |
9
|
|
|
* @author Vasily Zorin <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Pool extends Client |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Setting default config options |
15
|
|
|
* Overriden from NetworkClient::getConfigDefaults |
16
|
|
|
* @return array|bool |
17
|
|
|
*/ |
18
|
|
|
protected function getConfigDefaults() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
/* [integer] Default port */ |
|
|
|
|
22
|
|
|
'port' => 80, |
23
|
|
|
|
24
|
|
|
/* [integer] Default SSL port */ |
25
|
|
|
'sslport' => 443, |
26
|
|
|
|
27
|
|
|
/* [boolean] Send User-Agent header? */ |
28
|
|
|
'expose' => 1, |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Perform a HEAD request |
34
|
|
|
* @param string $url |
35
|
|
|
* @param array $params |
36
|
|
|
* @param callable $resultcb |
|
|
|
|
37
|
|
|
* @call ( url $url, array $params ) |
38
|
|
|
* @call ( url $url, callable $resultcb ) |
39
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function head($url, $params) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (is_callable($params)) { |
44
|
|
|
$params = ['resultcb' => $params]; |
45
|
|
|
} |
46
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
47
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
48
|
|
|
} |
49
|
|
|
if (isset($params['connect'])) { |
50
|
|
|
$dest = $params['connect']; |
51
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
52
|
|
|
if ($params['proxy']['type'] === 'http') { |
53
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $params) { |
|
|
|
|
59
|
|
|
if (!$conn->isConnected()) { |
60
|
|
|
$params['resultcb'](false); |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
$conn->head($url, $params); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Perform a GET request |
70
|
|
|
* @param string $url |
71
|
|
|
* @param array $params |
72
|
|
|
* @param callable $resultcb |
|
|
|
|
73
|
|
|
* @call ( url $url, array $params ) |
74
|
|
|
* @call ( url $url, callable $resultcb ) |
75
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function get($url, $params) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (is_callable($params)) { |
80
|
|
|
$params = ['resultcb' => $params]; |
81
|
|
|
} |
82
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
83
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
84
|
|
|
} |
85
|
|
|
if (isset($params['connect'])) { |
86
|
|
|
$dest = $params['connect']; |
87
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
88
|
|
|
if ($params['proxy']['type'] === 'http') { |
89
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $params) { |
|
|
|
|
95
|
|
|
if (!$conn->isConnected()) { |
96
|
|
|
$params['resultcb'](false); |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
$conn->get($url, $params); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Perform a POST request |
105
|
|
|
* @param string $url |
106
|
|
|
* @param array $data |
107
|
|
|
* @param array $params |
108
|
|
|
* @param callable $resultcb |
|
|
|
|
109
|
|
|
* @call ( url $url, array $data, array $params ) |
110
|
|
|
* @call ( url $url, array $data, callable $resultcb ) |
111
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
112
|
|
|
*/ |
113
|
|
|
public function post($url, $data, $params) |
114
|
|
|
{ |
115
|
|
|
if (!$data) { |
|
|
|
|
116
|
|
|
$data = []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (is_callable($params)) { |
120
|
|
|
$params = ['resultcb' => $params]; |
121
|
|
|
} |
122
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
123
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
124
|
|
|
} |
125
|
|
|
if (isset($params['connect'])) { |
126
|
|
|
$dest = $params['connect']; |
127
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
128
|
|
|
if ($params['proxy']['type'] === 'http') { |
129
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $data, $params) { |
|
|
|
|
135
|
|
|
if (!$conn->isConnected()) { |
136
|
|
|
$params['resultcb'](false); |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
$conn->post($url, $data, $params); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Builds URL from array |
145
|
|
|
* @param string $mixed |
146
|
|
|
* @call ( string $str ) |
147
|
|
|
* @call ( array $mixed ) |
148
|
|
|
* @return string|false |
149
|
|
|
*/ |
150
|
|
|
public static function buildUrl($mixed) |
151
|
|
|
{ |
152
|
|
|
if (is_string($mixed)) { |
153
|
|
|
return $mixed; |
154
|
|
|
} |
155
|
|
|
if (!is_array($mixed)) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
$url = ''; |
159
|
|
|
$buf = []; |
160
|
|
|
$queryDelimiter = '?'; |
161
|
|
|
$mixed[] = ''; |
162
|
|
|
foreach ($mixed as $k => $v) { |
163
|
|
|
if (is_int($k) || ctype_digit($k)) { |
164
|
|
|
if (sizeof($buf) > 0) { |
165
|
|
|
if (mb_orig_strpos($url, '?') !== false) { |
166
|
|
|
$queryDelimiter = '&'; |
167
|
|
|
} |
168
|
|
|
$url .= $queryDelimiter . http_build_query($buf); |
169
|
|
|
$queryDelimiter = ''; |
170
|
|
|
} |
171
|
|
|
$url .= $v; |
172
|
|
|
} else { |
173
|
|
|
$buf[$k] = $v; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return $url; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Parse URL |
181
|
|
|
* @param string $mixed Look Pool::buildUrl() |
182
|
|
|
* @call ( string $str ) |
183
|
|
|
* @call ( array $mixed ) |
184
|
|
|
* @return array|bool |
185
|
|
|
*/ |
186
|
|
|
public static function parseUrl($mixed) |
187
|
|
|
{ |
188
|
|
|
$url = static::buildUrl($mixed); |
189
|
|
|
if (false === $url) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
$u = parse_url($url); |
193
|
|
|
$uri = ''; |
194
|
|
|
if (isset($u['path'])) { |
195
|
|
|
$uri .= $u['path']; |
196
|
|
|
if (isset($u['query'])) { |
197
|
|
|
$uri .= '?' . $u['query']; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
return [$u['scheme'], $u['host'], $uri, isset($u['port']) ? $u['port'] : null]; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.