-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
MessageFactory.php
364 lines (303 loc) · 11.3 KB
/
MessageFactory.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
<?php
namespace GuzzleHttp\Message;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\CookieJarInterface;
use GuzzleHttp\Event\ListenerAttacherTrait;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Post\PostFile;
use GuzzleHttp\Post\PostFileInterface;
use GuzzleHttp\Query;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\Cookie;
use GuzzleHttp\Subscriber\HttpError;
use GuzzleHttp\Subscriber\Redirect;
use GuzzleHttp\Url;
use \InvalidArgumentException as Iae;
/**
* Default HTTP request factory used to create Request and Response objects.
*/
class MessageFactory implements MessageFactoryInterface
{
use ListenerAttacherTrait;
/** @var HttpError */
private $errorPlugin;
/** @var Redirect */
private $redirectPlugin;
/** @var array */
private $customOptions;
/** @var array Request options passed through to request Config object */
private static $configMap = [
'connect_timeout' => 1, 'timeout' => 1, 'verify' => 1, 'ssl_key' => 1,
'cert' => 1, 'proxy' => 1, 'debug' => 1, 'save_to' => 1, 'stream' => 1,
'expect' => 1, 'future' => 1
];
/** @var array Default allow_redirects request option settings */
private static $defaultRedirect = [
'max' => 5,
'strict' => false,
'referer' => false,
'protocols' => ['http', 'https']
];
/**
* @param array $customOptions Associative array of custom request option
* names mapping to functions used to apply
* the option. The function accepts the request
* and the option value to apply.
*/
public function __construct(array $customOptions = [])
{
$this->errorPlugin = new HttpError();
$this->redirectPlugin = new Redirect();
$this->customOptions = $customOptions;
}
public function createResponse(
$statusCode,
array $headers = [],
$body = null,
array $options = []
) {
if (null !== $body) {
$body = Stream::factory($body);
}
return new Response($statusCode, $headers, $body, $options);
}
public function createRequest($method, $url, array $options = [])
{
// Handle the request protocol version option that needs to be
// specified in the request constructor.
if (isset($options['version'])) {
$options['config']['protocol_version'] = $options['version'];
unset($options['version']);
}
$request = new Request($method, $url, [], null,
isset($options['config']) ? $options['config'] : []);
unset($options['config']);
// Use a POST body by default
if (strtoupper($method) == 'POST'
&& !isset($options['body'])
&& !isset($options['json'])
) {
$options['body'] = [];
}
if ($options) {
$this->applyOptions($request, $options);
}
return $request;
}
/**
* Create a request or response object from an HTTP message string
*
* @param string $message Message to parse
*
* @return RequestInterface|ResponseInterface
* @throws \InvalidArgumentException if unable to parse a message
*/
public function fromMessage($message)
{
static $parser;
if (!$parser) {
$parser = new MessageParser();
}
// Parse a response
if (strtoupper(substr($message, 0, 4)) == 'HTTP') {
$data = $parser->parseResponse($message);
return $this->createResponse(
$data['code'],
$data['headers'],
$data['body'] === '' ? null : $data['body'],
$data
);
}
// Parse a request
if (!($data = ($parser->parseRequest($message)))) {
throw new \InvalidArgumentException('Unable to parse request');
}
return $this->createRequest(
$data['method'],
Url::buildUrl($data['request_url']),
[
'headers' => $data['headers'],
'body' => $data['body'] === '' ? null : $data['body'],
'config' => [
'protocol_version' => $data['protocol_version']
]
]
);
}
/**
* Apply POST fields and files to a request to attempt to give an accurate
* representation.
*
* @param RequestInterface $request Request to update
* @param array $body Body to apply
*/
protected function addPostData(RequestInterface $request, array $body)
{
static $fields = ['string' => true, 'array' => true, 'NULL' => true,
'boolean' => true, 'double' => true, 'integer' => true];
$post = new PostBody();
foreach ($body as $key => $value) {
if (isset($fields[gettype($value)])) {
$post->setField($key, $value);
} elseif ($value instanceof PostFileInterface) {
$post->addFile($value);
} else {
$post->addFile(new PostFile($key, $value));
}
}
if ($request->getHeader('Content-Type') == 'multipart/form-data') {
$post->forceMultipartUpload(true);
}
$request->setBody($post);
}
protected function applyOptions(
RequestInterface $request,
array $options = []
) {
$config = $request->getConfig();
$emitter = $request->getEmitter();
foreach ($options as $key => $value) {
if (isset(self::$configMap[$key])) {
$config[$key] = $value;
continue;
}
switch ($key) {
case 'allow_redirects':
if ($value === false) {
continue 2;
}
if ($value === true) {
$value = self::$defaultRedirect;
} elseif (!is_array($value)) {
throw new Iae('allow_redirects must be true, false, or array');
} else {
// Merge the default settings with the provided settings
$value += self::$defaultRedirect;
}
$config['redirect'] = $value;
$emitter->attach($this->redirectPlugin);
break;
case 'decode_content':
if ($value === false) {
continue 2;
}
$config['decode_content'] = true;
if ($value !== true) {
$request->setHeader('Accept-Encoding', $value);
}
break;
case 'headers':
if (!is_array($value)) {
throw new Iae('header value must be an array');
}
foreach ($value as $k => $v) {
$request->setHeader($k, $v);
}
break;
case 'exceptions':
if ($value === true) {
$emitter->attach($this->errorPlugin);
}
break;
case 'body':
if (is_array($value)) {
$this->addPostData($request, $value);
} elseif ($value !== null) {
$request->setBody(Stream::factory($value));
}
break;
case 'auth':
if (!$value) {
continue 2;
}
if (is_array($value)) {
$type = isset($value[2]) ? strtolower($value[2]) : 'basic';
} else {
$type = strtolower($value);
}
$config['auth'] = $value;
if ($type == 'basic') {
$request->setHeader(
'Authorization',
'Basic ' . base64_encode("$value[0]:$value[1]")
);
} elseif ($type == 'digest') {
// @todo: Do not rely on curl
$config->setPath('curl/' . CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$config->setPath('curl/' . CURLOPT_USERPWD, "$value[0]:$value[1]");
}
break;
case 'query':
if ($value instanceof Query) {
$original = $request->getQuery();
// Do not overwrite existing query string variables by
// overwriting the object with the query string data passed
// in the URL
$value->overwriteWith($original->toArray());
$request->setQuery($value);
} elseif (is_array($value)) {
// Do not overwrite existing query string variables
$query = $request->getQuery();
foreach ($value as $k => $v) {
if (!isset($query[$k])) {
$query[$k] = $v;
}
}
} else {
throw new Iae('query must be an array or Query object');
}
break;
case 'cookies':
if ($value === true) {
static $cookie = null;
if (!$cookie) {
$cookie = new Cookie();
}
$emitter->attach($cookie);
} elseif (is_array($value)) {
$emitter->attach(
new Cookie(CookieJar::fromArray($value, $request->getHost()))
);
} elseif ($value instanceof CookieJarInterface) {
$emitter->attach(new Cookie($value));
} elseif ($value !== false) {
throw new Iae('cookies must be an array, true, or CookieJarInterface');
}
break;
case 'events':
if (!is_array($value)) {
throw new Iae('events must be an array');
}
$this->attachListeners($request,
$this->prepareListeners(
$value,
['before', 'complete', 'error', 'progress', 'end']
)
);
break;
case 'subscribers':
if (!is_array($value)) {
throw new Iae('subscribers must be an array');
}
foreach ($value as $subscribers) {
$emitter->attach($subscribers);
}
break;
case 'json':
$request->setBody(Stream::factory(json_encode($value)));
if (!$request->hasHeader('Content-Type')) {
$request->setHeader('Content-Type', 'application/json');
}
break;
default:
// Check for custom handler functions.
if (isset($this->customOptions[$key])) {
$fn = $this->customOptions[$key];
$fn($request, $value);
continue 2;
}
throw new Iae("No method can handle the {$key} config key");
}
}
}
}