-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.php
747 lines (680 loc) · 25 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http;
use Cake\Core\App;
use Cake\Core\Exception\CakeException;
use Cake\Core\InstanceConfigTrait;
use Cake\Http\Client\Adapter\Curl;
use Cake\Http\Client\Adapter\Mock as MockAdapter;
use Cake\Http\Client\Adapter\Stream;
use Cake\Http\Client\AdapterInterface;
use Cake\Http\Client\Request;
use Cake\Http\Client\Response;
use Cake\Http\Cookie\CookieCollection;
use Cake\Http\Cookie\CookieInterface;
use Cake\Utility\Hash;
use InvalidArgumentException;
use Laminas\Diactoros\Uri;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* The end user interface for doing HTTP requests.
*
* ### Scoped clients
*
* If you're doing multiple requests to the same hostname it's often convenient
* 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()
*
* ### Cookie management
*
* Client will maintain cookies from the responses done with
* a client instance. These cookies will be automatically added
* to future requests to matching hosts. Cookies will respect the
* `Expires`, `Path` and `Domain` attributes. You can get the client's
* CookieCollection using cookies()
*
* You can use the 'cookieJar' constructor option to provide a custom
* cookie jar instance you've restored from cache/disk. By default,
* an empty instance of {@link \Cake\Http\Client\CookieCollection} will be created.
*
* ### Sending request bodies
*
* By default, any POST/PUT/PATCH/DELETE request with $data will
* send their data as `application/x-www-form-urlencoded` unless
* there are attached files. In that case `multipart/form-data`
* will be used.
*
* 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 built-in support for basic authentication.
*/
class Client implements ClientInterface
{
use InstanceConfigTrait;
/**
* Default configuration for the client.
*
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'auth' => null,
'adapter' => null,
'host' => null,
'port' => null,
'scheme' => 'http',
'basePath' => '',
'timeout' => 30,
'ssl_verify_peer' => true,
'ssl_verify_peer_name' => true,
'ssl_verify_depth' => 5,
'ssl_verify_host' => true,
'redirect' => false,
'protocolVersion' => '1.1',
];
/**
* List of cookies from responses made with this client.
*
* Cookies are indexed by the cookie's domain or
* request host name.
*
* @var \Cake\Http\Cookie\CookieCollection
*/
protected $_cookies;
/**
* Mock adapter for stubbing requests in tests.
*
* @var \Cake\Http\Client\Adapter\Mock|null
*/
protected static $_mockAdapter;
/**
* Adapter for sending requests.
*
* @var \Cake\Http\Client\AdapterInterface
*/
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.
* - basePath - A path to append to the domain to use. (/api/v1/)
* - timeout - The timeout in seconds. Defaults to 30
* - ssl_verify_peer - Whether SSL certificates should be validated.
* Defaults to true.
* - ssl_verify_peer_name - Whether peer names should be validated.
* Defaults to true.
* - ssl_verify_depth - The maximum certificate chain depth to traverse.
* 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.
* - adapter - The adapter class name or instance. Defaults to
* \Cake\Http\Client\Adapter\Curl if `curl` extension is loaded else
* \Cake\Http\Client\Adapter\Stream.
* - protocolVersion - The HTTP protocol version to use. Defaults to 1.1
* - auth - The authentication credentials to use. If a `username` and `password`
* key are provided without a `type` key Basic authentication will be assumed.
* You can use the `type` key to define the authentication adapter classname
* to use. Short class names are resolved to the `Http\Client\Auth` namespace.
*
* @param array<string, mixed> $config Config options for scoped clients.
* @throws \InvalidArgumentException
*/
public function __construct(array $config = [])
{
$this->setConfig($config);
$adapter = $this->_config['adapter'];
if ($adapter === null) {
$adapter = Curl::class;
if (!extension_loaded('curl')) {
$adapter = Stream::class;
}
} else {
$this->setConfig('adapter', null);
}
if (is_string($adapter)) {
$adapter = new $adapter();
}
if (!$adapter instanceof AdapterInterface) {
throw new InvalidArgumentException('Adapter must be an instance of Cake\Http\Client\AdapterInterface');
}
$this->_adapter = $adapter;
if (!empty($this->_config['cookieJar'])) {
$this->_cookies = $this->_config['cookieJar'];
$this->setConfig('cookieJar', null);
} else {
$this->_cookies = new CookieCollection();
}
}
/**
* Client instance returned is scoped to the domain, port, and scheme parsed from the passed URL string. The passed
* string must have a scheme and a domain. Optionally, if a port is included in the string, the port will be scoped
* too. If a path is included in the URL, the client instance will build urls with it prepended.
* Other parts of the url string are ignored.
*
* @param string $url A string URL e.g. https://example.com
* @return static
* @throws \InvalidArgumentException
*/
public static function createFromUrl(string $url)
{
$parts = parse_url($url);
if ($parts === false) {
throw new InvalidArgumentException('String ' . $url . ' did not parse');
}
$config = array_intersect_key($parts, ['scheme' => '', 'port' => '', 'host' => '', 'path' => '']);
if (empty($config['scheme']) || empty($config['host'])) {
throw new InvalidArgumentException('The URL was parsed but did not contain a scheme or host');
}
if (isset($config['path'])) {
$config['basePath'] = $config['path'];
unset($config['path']);
}
return new static($config);
}
/**
* Get the cookies stored in the Client.
*
* @return \Cake\Http\Cookie\CookieCollection
*/
public function cookies(): CookieCollection
{
return $this->_cookies;
}
/**
* Adds a cookie to the Client collection.
*
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
* @return $this
* @throws \InvalidArgumentException
*/
public function addCookie(CookieInterface $cookie)
{
if (!$cookie->getDomain() || !$cookie->getPath()) {
throw new InvalidArgumentException('Cookie must have a domain and a path set.');
}
$this->_cookies = $this->_cookies->add($cookie);
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|string $data The query data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function get(string $url, $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$body = null;
if (is_array($data) && 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 mixed $data The post data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function post(string $url, $data = [], array $options = []): Response
{
$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 mixed $data The request data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function put(string $url, $data = [], array $options = []): Response
{
$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 mixed $data The request data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function patch(string $url, $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_PATCH, $url, $data, $options);
}
/**
* Do an OPTIONS request.
*
* @param string $url The url or path you want to request.
* @param mixed $data The request data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function options(string $url, $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_OPTIONS, $url, $data, $options);
}
/**
* Do a TRACE request.
*
* @param string $url The url or path you want to request.
* @param mixed $data The request data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function trace(string $url, $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_TRACE, $url, $data, $options);
}
/**
* Do a DELETE request.
*
* @param string $url The url or path you want to request.
* @param mixed $data The request data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function delete(string $url, $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
return $this->_doRequest(Request::METHOD_DELETE, $url, $data, $options);
}
/**
* Do a HEAD request.
*
* @param string $url The url or path you want to request.
* @param array $data The query string data you want to send.
* @param array<string, mixed> $options Additional options for the request.
* @return \Cake\Http\Client\Response
*/
public function head(string $url, array $data = [], array $options = []): Response
{
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, $data, $options);
return $this->_doRequest(Request::METHOD_HEAD, $url, '', $options);
}
/**
* Helper method for doing non-GET requests.
*
* @param string $method HTTP method.
* @param string $url URL to request.
* @param mixed $data The request body.
* @param array<string, mixed> $options The options to use. Contains auth, proxy, etc.
* @return \Cake\Http\Client\Response
*/
protected function _doRequest(string $method, string $url, $data, $options): Response
{
$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<string, mixed> $options Options to merge.
* @return array Options merged with set config.
*/
protected function _mergeOptions(array $options): array
{
return Hash::merge($this->_config, $options);
}
/**
* Sends a PSR-7 request and returns a PSR-7 response.
*
* @param \Psr\Http\Message\RequestInterface $request Request instance.
* @return \Psr\Http\Message\ResponseInterface Response instance.
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->send($request, $this->_config);
}
/**
* Send a request.
*
* Used internally by other methods, but can also be used to send
* handcrafted Request objects.
*
* @param \Psr\Http\Message\RequestInterface $request The request to send.
* @param array<string, mixed> $options Additional options to use.
* @return \Cake\Http\Client\Response
*/
public function send(RequestInterface $request, array $options = []): Response
{
$redirects = 0;
if (isset($options['redirect'])) {
$redirects = (int)$options['redirect'];
unset($options['redirect']);
}
do {
$response = $this->_sendRequest($request, $options);
$handleRedirect = $response->isRedirect() && $redirects-- > 0;
if ($handleRedirect) {
$url = $request->getUri();
$location = $response->getHeaderLine('Location');
$locationUrl = $this->buildUrl($location, [], [
'host' => $url->getHost(),
'port' => $url->getPort(),
'scheme' => $url->getScheme(),
'protocolRelative' => true,
]);
$request = $request->withUri(new Uri($locationUrl));
$request = $this->_cookies->addToRequest($request, []);
}
} while ($handleRedirect);
return $response;
}
/**
* Clear all mocked responses
*
* @return void
*/
public static function clearMockResponses(): void
{
static::$_mockAdapter = null;
}
/**
* Add a mocked response.
*
* Mocked responses are stored in an adapter that is called
* _before_ the network adapter is called.
*
* ### Matching Requests
*
* TODO finish this.
*
* ### Options
*
* - `match` An additional closure to match requests with.
*
* @param string $method The HTTP method being mocked.
* @param string $url The URL being matched. See above for examples.
* @param \Cake\Http\Client\Response $response The response that matches the request.
* @param array<string, mixed> $options See above.
* @return void
*/
public static function addMockResponse(string $method, string $url, Response $response, array $options = []): void
{
if (!static::$_mockAdapter) {
static::$_mockAdapter = new MockAdapter();
}
$request = new Request($url, $method);
static::$_mockAdapter->addResponse($request, $response, $options);
}
/**
* Send a request without redirection.
*
* @param \Psr\Http\Message\RequestInterface $request The request to send.
* @param array<string, mixed> $options Additional options to use.
* @return \Cake\Http\Client\Response
*/
protected function _sendRequest(RequestInterface $request, array $options): Response
{
if (static::$_mockAdapter) {
$responses = static::$_mockAdapter->send($request, $options);
}
if (empty($responses)) {
$responses = $this->_adapter->send($request, $options);
}
foreach ($responses as $response) {
$this->_cookies = $this->_cookies->addFromResponse($response, $request);
}
return array_pop($responses);
}
/**
* Generate a URL based on the scoped client options.
*
* @param string $url Either a full URL or just the path.
* @param array|string $query The query data for the URL.
* @param array<string, mixed> $options The config options stored with Client::config()
* @return string A complete url with scheme, port, host, and path.
*/
public function buildUrl(string $url, $query = [], array $options = []): string
{
if (empty($options) && empty($query)) {
return $url;
}
$defaults = [
'host' => null,
'port' => null,
'scheme' => 'http',
'basePath' => '',
'protocolRelative' => false,
];
$options += $defaults;
if ($query) {
$q = strpos($url, '?') === false ? '?' : '&';
$url .= $q;
$url .= is_string($query) ? $query : http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
if ($options['protocolRelative'] && preg_match('#^//#', $url)) {
$url = $options['scheme'] . ':' . $url;
}
if (preg_match('#^https?://#', $url)) {
return $url;
}
$defaultPorts = [
'http' => 80,
'https' => 443,
];
$out = $options['scheme'] . '://' . $options['host'];
if ($options['port'] && (int)$options['port'] !== $defaultPorts[$options['scheme']]) {
$out .= ':' . $options['port'];
}
if (!empty($options['basePath'])) {
$out .= '/' . trim($options['basePath'], '/');
}
$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<string, mixed> $options The options to use. Contains auth, proxy, etc.
* @return \Cake\Http\Client\Request
*/
protected function _createRequest(string $method, string $url, $data, $options): Request
{
/** @var array<non-empty-string, non-empty-string> $headers */
$headers = (array)($options['headers'] ?? []);
if (isset($options['type'])) {
$headers = array_merge($headers, $this->_typeHeaders($options['type']));
}
if (is_string($data) && !isset($headers['Content-Type']) && !isset($headers['content-type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
$request = new Request($url, $method, $headers, $data);
$request = $request->withProtocolVersion($this->getConfig('protocolVersion'));
$cookies = $options['cookies'] ?? [];
/** @var \Cake\Http\Client\Request $request */
$request = $this->_cookies->addToRequest($request, $cookies);
if (isset($options['auth'])) {
$request = $this->_addAuthentication($request, $options);
}
if (isset($options['proxy'])) {
$request = $this->_addProxy($request, $options);
}
return $request;
}
/**
* Returns headers for Accept/Content-Type based on a short type
* or full mime-type.
*
* @phpstan-param non-empty-string $type
* @param string $type short type alias or full mimetype.
* @return array<string, string> Headers to set on the request.
* @throws \Cake\Core\Exception\CakeException When an unknown type alias is used.
* @psalm-return array<non-empty-string, non-empty-string>
*/
protected function _typeHeaders(string $type): array
{
if (strpos($type, '/') !== false) {
return [
'Accept' => $type,
'Content-Type' => $type,
];
}
$typeMap = [
'json' => 'application/json',
'xml' => 'application/xml',
];
if (!isset($typeMap[$type])) {
throw new CakeException("Unknown type alias '$type'.");
}
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 \Cake\Http\Client\Request $request The request to modify.
* @param array<string, mixed> $options Array of options containing the 'auth' key.
* @return \Cake\Http\Client\Request The updated request object.
*/
protected function _addAuthentication(Request $request, array $options): Request
{
$auth = $options['auth'];
/** @var \Cake\Http\Client\Auth\Basic $adapter */
$adapter = $this->_createAuth($auth, $options);
return $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 \Cake\Http\Client\Request $request The request to modify.
* @param array<string, mixed> $options Array of options containing the 'proxy' key.
* @return \Cake\Http\Client\Request The updated request object.
*/
protected function _addProxy(Request $request, array $options): Request
{
$auth = $options['proxy'];
/** @var \Cake\Http\Client\Auth\Basic $adapter */
$adapter = $this->_createAuth($auth, $options);
return $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<string, mixed> $options The overall request options to use.
* @return object Authentication strategy instance.
* @throws \Cake\Core\Exception\CakeException when an invalid strategy is chosen.
*/
protected function _createAuth(array $auth, array $options)
{
if (empty($auth['type'])) {
$auth['type'] = 'basic';
}
$name = ucfirst($auth['type']);
$class = App::className($name, 'Http/Client/Auth');
if (!$class) {
throw new CakeException(
sprintf('Invalid authentication type %s', $name)
);
}
return new $class($this, $options);
}
}