/
Client.php
438 lines (416 loc) · 12.2 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Network\Http;
use Cake\Core\App;
use Cake\Error;
use Cake\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
/**
* The end user interface for doing HTTP requests.
*
* ### Scoped clients
*
* If you're doing multiple requests to the same hostname its often convienent
* to use the constructor arguments to create a scoped client. This allows you
* to keep your code DRY and not repeat hostnames, authentication, and other options.
*
* ### Doing requests
*
* Once you've created an instance of Client you can do requests
* using several methods. Each corresponds to a different HTTP method.
*
* - get()
* - post()
* - put()
* - delete()
* - patch()
*
* ### Sending request bodies
*
* By default any POST/PUT/PATCH/DELETE request with $data will
* send their data as `multipart/form-data`.
*
* When sending request bodies you can use the `type` option to
* set the Content-Type for the request:
*
* `$http->get('/users', [], ['type' => 'json']);`
*
* The `type` option sets both the `Content-Type` and `Accept` header, to
* the same mime type. When using `type` you can use either a full mime
* type or an alias. If you need different types in the Accept and Content-Type
* headers you should set them manually and not use `type`
*
* ### Using authentication
*
* By using the `auth` key you can use authentication. The type sub option
* can be used to specify which authentication strategy you want to use.
* CakePHP comes with a few built-in strategies:
*
* - Basic
* - Digest
* - Oauth
*
* ### Using proxies
*
* By using the `proxy` key you can set authentication credentials for
* a proxy if you need to use one.. The type sub option can be used to
* specify which authentication strategy you want to use.
* CakePHP comes with a few built-in strategies:
*
* - Basic
* - Digest
*
*/
class Client {
/**
* Stored configuration for the client.
*
* @var array
*/
protected $_config = [
'host' => null,
'port' => null,
'scheme' => 'http',
'timeout' => 30,
'ssl_verify_peer' => true,
'ssl_verify_depth' => 5,
'ssl_verify_host' => true,
'redirect' => false,
];
/**
* Adapter for sending requests. Defaults to
* Cake\Network\Http\Stream
*
* @var Cake\Network\Http\Stream
*/
protected $_adapter;
/**
* Create a new HTTP Client.
*
* ### Config options
*
* You can set the following options when creating a client:
*
* - host - The hostname to do requests on.
* - port - The port to use.
* - scheme - The default scheme/protocol to use. Defaults to http.
* - timeout - The timeout in seconds. Defaults to 30
* - ssl_verify_peer - Whether or not SSL certificates should be validated.
* Defaults to true.
* - ssl_verify_depth - The maximum certificate chain depth to travers.
* Defaults to 5.
* - ssl_verify_host - Verify that the certificate and hostname match.
* Defaults to true.
* - redirect - Number of redirects to follow. Defaults to false.
*
* @param array $config Config options for scoped clients.
*/
public function __construct($config = []) {
$adapter = 'Cake\Network\Http\Adapter\Stream';
if (isset($config['adapter'])) {
$adapter = $config['adapter'];
unset($config['adapter']);
}
$this->config($config);
if (is_string($adapter)) {
$adapter = new $adapter();
}
$this->_adapter = $adapter;
}
/**
* Get or set additional config options.
*
* Setting config will use Hash::merge() for appending into
* the existing configuration.
*
* @param array|null $config Configuration options. null to get.
* @return this|array
*/
public function config($config = null) {
if ($config === null) {
return $this->_config;
}
$this->_config = Hash::merge($this->_config, $config);
return $this;
}
/**
* Do a GET request.
*
* The $data argument supports a special `_content` key
* for providing a request body in a GET request. This is
* generally not used but services like ElasticSearch use
* this feature.
*
* @param string $url The url or path you want to request.
* @param array $data The query data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function get($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$body = [];
if (isset($data['_content'])) {
$body = $data['_content'];
unset($data['_content']);
}
$url = $this->buildUrl($url, $data, $options);
return $this->_doRequest(
Request::METHOD_GET,
$url,
$body,
$options
);
}
/**
* Do a POST request.
*
* @param string $url The url or path you want to request.
* @param array $data The post data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function post($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_POST, $url, $data, $options);
}
/**
* Do a PUT request.
*
* @param string $url The url or path you want to request.
* @param array $data The request data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function put($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_PUT, $url, $data, $options);
}
/**
* Do a PATCH request.
*
* @param string $url The url or path you want to request.
* @param array $data The request data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function patch($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_PATCH, $url, $data, $options);
}
/**
* Do a DELETE request.
*
* @param string $url The url or path you want to request.
* @param array $data The request data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function delete($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_DELETE, $url, $data, $options);
}
/**
* Helper method for doing non-GET requests.
*
* @param string $method HTTP method.
* @param string $url URL to request.
*/
protected function _doRequest($method, $url, $data, $options) {
$request = $this->_createRequest(
$method,
$url,
$data,
$options
);
return $this->send($request, $options);
}
/**
* Does a recursive merge of the parameter with the scope config.
*
* @param array $options Options to merge.
* @return array Options merged with set config.
*/
protected function _mergeOptions($options) {
return Hash::merge($this->_config, $options);
}
/**
* Send a request.
*
* Used internally by other methods, but can also be used to send
* handcrafted Request objects.
*
* @param Cake\Network\Http\Request $request The request to send.
* @param array $options Additional options to use.
* @return Cake\Network\Http\Response
*/
public function send(Request $request, $options = []) {
// TODO possibly implment support for
// holding onto cookies so subsequent requests
// can share cookies.
return $this->_adapter->send($request, $options);
}
/**
* Generate a URL based on the scoped client options.
*
* @param string $url Either a full URL or just the path.
* @param array $query The query data for the URL.
* @param array $options The config options stored with Client::config()
* @return string A complete url with scheme, port, host, path.
*/
public function buildUrl($url, $query = [], $options = []) {
if (empty($options) && empty($query)) {
return $url;
}
if ($query) {
$q = (strpos($url, '?') === false) ? '?' : '&';
$url .= $q . http_build_query($query);
}
if (preg_match('#^https?://#', $url)) {
return $url;
}
$defaults = [
'host' => null,
'port' => null,
'scheme' => 'http',
];
$options += $defaults;
$defaultPorts = [
'http' => 80,
'https' => 443
];
$out = $options['scheme'] . '://' . $options['host'];
if ($options['port'] && $options['port'] != $defaultPorts[$options['scheme']]) {
$out .= ':' . $options['port'];
}
$out .= '/' . ltrim($url, '/');
return $out;
}
/**
* Creates a new request object based on the parameters.
*
* @param string $method HTTP method name.
* @param string $url The url including query string.
* @param mixed $data The request body.
* @param array $options The options to use. Contains auth, proxy etc.
* @return Cake\Network\Http\Request
*/
protected function _createRequest($method, $url, $data, $options) {
$request = new Request();
$request->method($method)
->url($url)
->body($data);
if (isset($options['type'])) {
$request->header($this->_typeHeaders($options['type']));
}
if (isset($options['headers'])) {
$request->header($options['headers']);
}
if (isset($options['cookies'])) {
$request->cookie($options['cookies']);
}
if (isset($options['auth'])) {
$this->_addAuthentication($request, $options);
}
if (isset($options['proxy'])) {
$this->_addProxy($request, $options);
}
return $request;
}
/**
* Returns headers for Accept/Content-Type based on a short type
* or full mime-type.
*
* @param string $type short type alias or full mimetype.
* @return array Headers to set on the request.
*/
protected function _typeHeaders($type) {
if (strpos($type, '/') !== false) {
return [
'Accept' => $type,
'Content-Type' => $type
];
}
$typeMap = [
'json' => 'application/json',
'xml' => 'application/xml',
];
if (!isset($typeMap[$type])) {
throw new Error\Exception(__d('cake_dev', 'Unknown type alias.'));
}
return [
'Accept' => $typeMap[$type],
'Content-Type' => $typeMap[$type],
];
}
/**
* Add authentication headers to the request.
*
* Uses the authentication type to choose the correct strategy
* and use its methods to add headers.
*
* @param Request $request The request to modify.
* @param array $options Array of options containing the 'auth' key.
* @return void
*/
protected function _addAuthentication(Request $request, $options) {
$auth = $options['auth'];
$adapter = $this->_createAuth($auth, $options);
$adapter->authentication($request, $options['auth']);
}
/**
* Add proxy authentication headers.
*
* Uses the authentication type to choose the correct strategy
* and use its methods to add headers.
*
* @param Request $request The request to modify.
* @param array $options Array of options containing the 'proxy' key.
* @return void
*/
protected function _addProxy(Request $request, $options) {
$auth = $options['proxy'];
$adapter = $this->_createAuth($auth, $options);
$adapter->proxyAuthentication($request, $options['proxy']);
}
/**
* Create the authentication strategy.
*
* Use the configuration options to create the correct
* authentication strategy handler.
*
* @param array $auth The authentication options to use.
* @param array $options The overall request options to use.
* @return mixed Authentication strategy instance.
* @throws Cake\Error\Exception when an invalid stratgey is chosen.
*/
protected function _createAuth($auth, $options) {
if (empty($auth['type'])) {
$auth['type'] = 'basic';
}
$name = Inflector::classify($auth['type']);
$class = App::className($name, 'Network/Http/Auth');
if (!$class) {
throw new Error\Exception(
__d('cake_dev', 'Invalid authentication type %s', $name)
);
}
return new $class($this, $options);
}
}