forked from yiisoft/yii2-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
339 lines (309 loc) · 10.6 KB
/
Request.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\httpclient;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
/**
* Request represents HTTP request.
*
* @property string $url target URL.
* @property string $method request method.
* @property array $options request options. See [[setOptions()]] for details.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class Request extends Message
{
/**
* @var string target URL.
*/
private $_url;
/**
* @var string request method.
*/
private $_method = 'get';
/**
* @var array request options.
*/
private $_options = [];
/**
* @param string $url target URL
* @return $this self reference.
*/
public function setUrl($url)
{
$this->_url = $url;
return $this;
}
/**
* @return string target URL
*/
public function getUrl()
{
return $this->_url;
}
/**
* @param string $method request method
* @return $this self reference.
*/
public function setMethod($method)
{
$this->_method = $method;
return $this;
}
/**
* @return string request method
*/
public function getMethod()
{
return $this->_method;
}
/**
* Following options are supported:
* - timeout: integer, the maximum number of seconds to allow request to be executed.
* - proxy: string, URI specifying address of proxy server. (e.g. tcp://proxy.example.com:5100).
* - userAgent: string, the contents of the "User-Agent: " header to be used in a HTTP request.
* - followLocation: boolean, whether to follow any "Location: " header that the server sends as part of the HTTP header.
* - maxRedirects: integer, the max number of redirects to follow.
* - sslVerifyPeer: boolean, whether verification of the peer's certificate should be performed.
* - sslCafile: string, location of Certificate Authority file on local filesystem which should be used with
* the 'sslVerifyPeer' option to authenticate the identity of the remote peer.
* - sslCapath: string, a directory that holds multiple CA certificates.
*
* You may set options using keys, which are specific to particular transport, like `[CURLOPT_VERBOSE => true]` in case
* there is a necessity for it.
*
* @param array $options request options.
* @return $this self reference.
*/
public function setOptions(array $options)
{
$this->_options = $options;
return $this;
}
/**
* @return array request options.
*/
public function getOptions()
{
return $this->_options;
}
/**
* Adds more options to already defined ones.
* Please refer to [[setOptions()]] on how to specify options.
* @param array $options additional options
* @return $this self reference.
*/
public function addOptions(array $options)
{
$this->options = ArrayHelper::merge($this->options, $options); // `array_merge()` will produce invalid result for cURL options
return $this;
}
/**
* Adds a content part for multi-part content request.
* @param string $name part (form input) name.
* @param string $content content.
* @param array $options content part options, valid options are:
* - contentType - string, part content type
* - fileName - string, name of the uploading file
* - mimeType - string, part content type in case of file uploading
* @return $this self reference.
*/
public function addContent($name, $content, $options = [])
{
$multiPartContent = $this->getContent();
if (!is_array($multiPartContent)) {
$multiPartContent = [];
}
$options['content'] = $content;
$multiPartContent[$name] = $options;
$this->setContent($multiPartContent);
return $this;
}
/**
* Adds a file for upload as multi-part content.
* @see addContent()
* @param string $name part (form input) name
* @param string $fileName full name of the source file.
* @param array $options content part options, valid options are:
* - fileName - string, base name of the uploading file, if not set it base name of the source file will be used.
* - mimeType - string, file mime type, if not set it will be determine automatically from source file.
* @return $this
*/
public function addFile($name, $fileName, $options = [])
{
$content = file_get_contents($fileName);
if (!isset($options['mimeType'])) {
$options['mimeType'] = FileHelper::getMimeType($fileName);
}
if (!isset($options['fileName'])) {
$options['fileName'] = basename($fileName);
}
return $this->addContent($name, $content, $options);
}
/**
* Adds a string as a file upload.
* @see addContent()
* @param string $name part (form input) name
* @param string $content file content.
* @param array $options content part options, valid options are:
* - fileName - string, base name of the uploading file.
* - mimeType - string, file mime type, if not set it 'application/octet-stream' will be used.
* @return $this
*/
public function addFileContent($name, $content, $options = [])
{
if (!isset($options['mimeType'])) {
$options['mimeType'] = 'application/octet-stream';
}
if (!isset($options['fileName'])) {
$options['fileName'] = $name . '.dat';
}
return $this->addContent($name, $content, $options);
}
/**
* Prepares this request instance for sending.
* This method should be invoked by transport before sending a request.
* Do not call this method unless you know what you are doing.
* @return $this self reference.
*/
public function prepare()
{
if (!empty($this->client->baseUrl)) {
$url = $this->getUrl();
if (!preg_match('/^https?:\\/\\//i', $url)) {
$this->setUrl($this->client->baseUrl . '/' . $url);
}
}
$content = $this->getContent();
if ($content === null) {
$this->getFormatter()->format($this);
} elseif (is_array($content)) {
$this->prepareMultiPartContent($content);
}
return $this;
}
/**
* Prepares multi-part content.
* @param array $content multi part content.
*/
private function prepareMultiPartContent(array $content)
{
static $disallowedChars = ["\0", '"', "\r", "\n"];
$contentParts = [];
$data = $this->getData();
if (!empty($data)) {
foreach ($this->composeFormInputs($data) as $name => $value) {
$name = str_replace($disallowedChars, '_', $name);
$contentDisposition = 'Content-Disposition: form-data; name="' . $name . '";';
$contentParts[] = implode("\r\n", [$contentDisposition, '', $value]);
}
}
// process content parts :
foreach ($content as $name => $contentParams) {
$headers = [];
$name = str_replace($disallowedChars, '_', $name);
$contentDisposition = 'Content-Disposition: form-data; name="' . $name . '";';
if (isset($contentParams['fileName'])) {
$fileName = str_replace($disallowedChars, '_', $contentParams['fileName']);
$contentDisposition .= ' filename="' . $fileName . '"';
}
$headers[] = $contentDisposition;
if (isset($contentParams['contentType'])) {
$headers[] = 'Content-Type: ' . $contentParams['contentType'];
} elseif (isset($contentParams['mimeType'])) {
$headers[] = 'Content-Type: ' . $contentParams['mimeType'];
}
$contentParts[] = implode("\r\n", [implode("\r\n", $headers), '', $contentParams['content']]);
}
// generate safe boundary :
do {
$boundary = '---------------------' . md5(mt_rand() . microtime());
} while (preg_grep("/{$boundary}/", $contentParts));
// add boundary for each part :
array_walk($contentParts, function (&$part) use ($boundary) {
$part = "--{$boundary}\r\n{$part}";
});
// add final boundary :
$contentParts[] = "--{$boundary}--";
$contentParts[] = '';
$this->getHeaders()->set('content-type', "multipart/form-data; boundary={$boundary}");
$this->setContent(implode("\r\n", $contentParts));
}
/**
* Composes given data as form inputs submitted values, taking in account nested arrays.
* Converts `['form' => ['name' => 'value']]` to `['form[name]' => 'value']`.
* @param array $data
* @param string $baseKey
* @return array
*/
private function composeFormInputs(array $data, $baseKey = '')
{
$result = [];
foreach ($data as $key => $value) {
if (!empty($baseKey)) {
$key = $baseKey . '[' . $key . ']';
}
if (is_array($value)) {
$result = array_merge($result, $this->composeFormInputs($value, $key));
} else {
$result[$key] = $value;
}
}
return $result;
}
/**
* @inheritdoc
*/
public function composeHeaderLines()
{
$headers = parent::composeHeaderLines();
if ($this->hasCookies()) {
$headers[] = $this->composeCookieHeader();
}
return $headers;
}
/**
* Sends this request.
* @return Response response instance.
*/
public function send()
{
return $this->client->send($this);
}
/**
* @inheritdoc
*/
public function toString()
{
$result = strtoupper($this->getMethod()) . ' ' . $this->getUrl();
$parentResult = parent::toString();
if ($parentResult !== '') {
$result .= "\n" . $parentResult;
}
return $result;
}
/**
* @return string cookie header value.
*/
private function composeCookieHeader()
{
$parts = [];
foreach ($this->getCookies() as $cookie) {
$parts[] = $cookie->name . '=' . urlencode($cookie->value);
}
return 'Cookie: ' . implode(';', $parts);
}
/**
* @return FormatterInterface message formatter instance.
*/
private function getFormatter()
{
return $this->client->getFormatter($this->getFormat());
}
}