|
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
|
|
|
* Perform a HEAD request |
|
15
|
|
|
* @param string $url |
|
16
|
|
|
* @param array $params |
|
17
|
|
|
* @param callable $resultcb |
|
|
|
|
|
|
18
|
|
|
* @call ( url $url, array $params ) |
|
19
|
|
|
* @call ( url $url, callable $resultcb ) |
|
20
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
|
21
|
|
|
*/ |
|
22
|
|
View Code Duplication |
public function head($url, $params) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
if (is_callable($params)) { |
|
25
|
|
|
$params = ['resultcb' => $params]; |
|
26
|
|
|
} |
|
27
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
|
28
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
|
29
|
|
|
} |
|
30
|
|
|
if (isset($params['connect'])) { |
|
31
|
|
|
$dest = $params['connect']; |
|
32
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
|
33
|
|
|
if ($params['proxy']['type'] === 'http') { |
|
34
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
|
35
|
|
|
} |
|
36
|
|
|
} else { |
|
37
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
38
|
|
|
} |
|
39
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $params) { |
|
|
|
|
|
|
40
|
|
|
if (!$conn->isConnected()) { |
|
41
|
|
|
$params['resultcb'](false); |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
$conn->head($url, $params); |
|
45
|
|
|
}); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Parse URL |
|
50
|
|
|
* @param string $mixed Look Pool::buildUrl() |
|
51
|
|
|
* @call ( string $str ) |
|
52
|
|
|
* @call ( array $mixed ) |
|
53
|
|
|
* @return array|bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function parseUrl($mixed) |
|
56
|
|
|
{ |
|
57
|
|
|
$url = static::buildUrl($mixed); |
|
58
|
|
|
if (false === $url) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
$u = parse_url($url); |
|
62
|
|
|
$uri = ''; |
|
63
|
|
|
if (!isset($u['path']) && isset($u['query'])) { |
|
64
|
|
|
$u['path'] = '/'; |
|
65
|
|
|
} |
|
66
|
|
|
if (isset($u['path'])) { |
|
67
|
|
|
$uri .= $u['path']; |
|
68
|
|
|
if (isset($u['query'])) { |
|
69
|
|
|
$uri .= '?' . $u['query']; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return [$u['scheme'], $u['host'], $uri, isset($u['port']) ? $u['port'] : null]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Builds URL from array |
|
77
|
|
|
* @param string $mixed |
|
78
|
|
|
* @call ( string $str ) |
|
79
|
|
|
* @call ( array $mixed ) |
|
80
|
|
|
* @return string|false |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function buildUrl($mixed) |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_string($mixed)) { |
|
85
|
|
|
return $mixed; |
|
86
|
|
|
} |
|
87
|
|
|
if (!is_array($mixed)) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
$url = ''; |
|
91
|
|
|
$buf = []; |
|
92
|
|
|
$queryDelimiter = '?'; |
|
93
|
|
|
$mixed[] = ''; |
|
94
|
|
|
foreach ($mixed as $k => $v) { |
|
95
|
|
|
if (is_int($k) || ctype_digit($k)) { |
|
96
|
|
|
if (sizeof($buf) > 0) { |
|
97
|
|
|
if (mb_orig_strpos($url, '?') !== false) { |
|
98
|
|
|
$queryDelimiter = '&'; |
|
99
|
|
|
} |
|
100
|
|
|
$url .= $queryDelimiter . http_build_query($buf); |
|
101
|
|
|
$queryDelimiter = ''; |
|
102
|
|
|
} |
|
103
|
|
|
$url .= $v; |
|
104
|
|
|
} else { |
|
105
|
|
|
$buf[$k] = $v; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
return $url; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Perform a GET request |
|
113
|
|
|
* @param string $url |
|
114
|
|
|
* @param array $params |
|
115
|
|
|
* @param callable $resultcb |
|
|
|
|
|
|
116
|
|
|
* @call ( url $url, array $params ) |
|
117
|
|
|
* @call ( url $url, callable $resultcb ) |
|
118
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
|
119
|
|
|
*/ |
|
120
|
|
View Code Duplication |
public function get($url, $params) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
if (is_callable($params)) { |
|
123
|
|
|
$params = ['resultcb' => $params]; |
|
124
|
|
|
} |
|
125
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
|
126
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
|
127
|
|
|
} |
|
128
|
|
|
if (isset($params['connect'])) { |
|
129
|
|
|
$dest = $params['connect']; |
|
130
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
|
131
|
|
|
if ($params['proxy']['type'] === 'http') { |
|
132
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
|
133
|
|
|
} |
|
134
|
|
|
} else { |
|
135
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
136
|
|
|
} |
|
137
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $params) { |
|
|
|
|
|
|
138
|
|
|
if (!$conn->isConnected()) { |
|
139
|
|
|
$params['resultcb'](false); |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
$conn->get($url, $params); |
|
143
|
|
|
}); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Perform a POST request |
|
148
|
|
|
* @param string $url |
|
149
|
|
|
* @param array $data |
|
150
|
|
|
* @param array $params |
|
151
|
|
|
* @param callable $resultcb |
|
|
|
|
|
|
152
|
|
|
* @param \Closure $beforeConnect |
|
|
|
|
|
|
153
|
|
|
* @call ( url $url, array $data, array $params ) |
|
154
|
|
|
* @call ( url $url, array $data, callable $resultcb ) |
|
155
|
|
|
* @callback $resultcb ( Connection $conn, boolean $success ) |
|
156
|
|
|
*/ |
|
157
|
|
|
public function post($url, $data, $params, \Closure $beforeConnect = null) |
|
158
|
|
|
{ |
|
159
|
|
|
if (!$data) { |
|
|
|
|
|
|
160
|
|
|
$data = []; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (is_callable($params)) { |
|
164
|
|
|
$params = ['resultcb' => $params]; |
|
165
|
|
|
} |
|
166
|
|
|
if (!isset($params['uri']) || !isset($params['host'])) { |
|
167
|
|
|
list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url); |
|
168
|
|
|
} |
|
169
|
|
|
if (isset($params['connect'])) { |
|
170
|
|
|
$dest = $params['connect']; |
|
171
|
|
|
} elseif (isset($params['proxy']) && $params['proxy']) { |
|
172
|
|
|
if ($params['proxy']['type'] === 'http') { |
|
173
|
|
|
$dest = 'tcp://' . $params['proxy']['addr']; |
|
174
|
|
|
} |
|
175
|
|
|
} else { |
|
176
|
|
|
$dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : ''); |
|
177
|
|
|
} |
|
178
|
|
|
$this->getConnection($dest, function ($conn) use ($url, $data, $params) { |
|
|
|
|
|
|
179
|
|
|
if (!$conn->isConnected()) { |
|
180
|
|
|
$params['resultcb'](false); |
|
181
|
|
|
return; |
|
182
|
|
|
} |
|
183
|
|
|
$conn->post($url, $data, $params); |
|
184
|
|
|
}, 0, $beforeConnect); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Setting default config options |
|
189
|
|
|
* Overriden from NetworkClient::getConfigDefaults |
|
190
|
|
|
* @return array|bool |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function getConfigDefaults() |
|
193
|
|
|
{ |
|
194
|
|
|
return [ |
|
195
|
|
|
/* [integer] Default port */ |
|
196
|
|
|
'port' => 80, |
|
197
|
|
|
|
|
198
|
|
|
/* [integer] Default SSL port */ |
|
199
|
|
|
'sslport' => 443, |
|
200
|
|
|
|
|
201
|
|
|
/* [boolean] Send User-Agent header? */ |
|
202
|
|
|
'expose' => 1, |
|
203
|
|
|
]; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.