/
Client.php
281 lines (263 loc) · 7.31 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
<?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\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Utility\Hash;
/**
* The end user interface for doing HTTP requests.
*
* ### Scoped clients
*
* ### Doing requests
*
* ### Using authentication
*
*/
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
*
*
* @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 = []) {
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 content.
* @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)
->content($data);
if (isset($options['headers'])) {
$request->header($options['headers']);
}
if (isset($options['cookies'])) {
$request->cookie($options['cookies']);
}
// TODO auth + proxy config.
return $request;
}
}