blob: ed14d1f7037fc2c940d46d7da8cc64b0c5e9d1fc (
plain)
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
|
<?php
namespace GuzzleHttp\Post;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Stream\Exception\CannotAttachException;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Query;
/**
* Holds POST fields and files and creates a streaming body when read methods
* are called on the object.
*/
class PostBody implements PostBodyInterface
{
/** @var StreamInterface */
private $body;
/** @var callable */
private $aggregator;
private $fields = [];
/** @var PostFileInterface[] */
private $files = [];
private $forceMultipart = false;
private $detached = false;
/**
* Applies request headers to a request based on the POST state
*
* @param RequestInterface $request Request to update
*/
public function applyRequestHeaders(RequestInterface $request)
{
if ($this->files || $this->forceMultipart) {
$request->setHeader(
'Content-Type',
'multipart/form-data; boundary=' . $this->getBody()->getBoundary()
);
} elseif ($this->fields && !$request->hasHeader('Content-Type')) {
$request->setHeader(
'Content-Type',
'application/x-www-form-urlencoded'
);
}
if ($size = $this->getSize()) {
$request->setHeader('Content-Length', $size);
}
}
public function forceMultipartUpload($force)
{
$this->forceMultipart = $force;
}
public function setAggregator(callable $aggregator)
{
$this->aggregator = $aggregator;
}
public function setField($name, $value)
{
$this->fields[$name] = $value;
$this->mutate();
}
public function replaceFields(array $fields)
{
$this->fields = $fields;
$this->mutate();
}
public function getField($name)
{
return isset($this->fields[$name]) ? $this->fields[$name] : null;
}
public function removeField($name)
{
unset($this->fields[$name]);
$this->mutate();
}
public function getFields($asString = false)
{
if (!$asString) {
return $this->fields;
}
$query = new Query($this->fields);
$query->setEncodingType(Query::RFC1738);
$query->setAggregator($this->getAggregator());
return (string) $query;
}
public function hasField($name)
{
return isset($this->fields[$name]);
}
public function getFile($name)
{
foreach ($this->files as $file) {
if ($file->getName() == $name) {
return $file;
}
}
return null;
}
public function getFiles()
{
return $this->files;
}
public function addFile(PostFileInterface $file)
{
$this->files[] = $file;
$this->mutate();
}
public function clearFiles()
{
$this->files = [];
$this->mutate();
}
/**
* Returns the numbers of fields + files
*
* @return int
*/
public function count()
{
return count($this->files) + count($this->fields);
}
public function __toString()
{
return (string) $this->getBody();
}
public function getContents($maxLength = -1)
{
return $this->getBody()->getContents();
}
public function close()
{
$this->detach();
}
public function detach()
{
$this->detached = true;
$this->fields = $this->files = [];
if ($this->body) {
$this->body->close();
$this->body = null;
}
}
public function attach($stream)
{
throw new CannotAttachException();
}
public function eof()
{
return $this->getBody()->eof();
}
public function tell()
{
return $this->body ? $this->body->tell() : 0;
}
public function isSeekable()
{
return true;
}
public function isReadable()
{
return true;
}
public function isWritable()
{
return false;
}
public function getSize()
{
return $this->getBody()->getSize();
}
public function seek($offset, $whence = SEEK_SET)
{
return $this->getBody()->seek($offset, $whence);
}
public function read($length)
{
return $this->getBody()->read($length);
}
public function write($string)
{
return false;
}
public function getMetadata($key = null)
{
return $key ? null : [];
}
/**
* Return a stream object that is built from the POST fields and files.
*
* If one has already been created, the previously created stream will be
* returned.
*/
private function getBody()
{
if ($this->body) {
return $this->body;
} elseif ($this->files || $this->forceMultipart) {
return $this->body = $this->createMultipart();
} elseif ($this->fields) {
return $this->body = $this->createUrlEncoded();
} else {
return $this->body = Stream::factory();
}
}
/**
* Get the aggregator used to join multi-valued field parameters
*
* @return callable
*/
final protected function getAggregator()
{
if (!$this->aggregator) {
$this->aggregator = Query::phpAggregator();
}
return $this->aggregator;
}
/**
* Creates a multipart/form-data body stream
*
* @return MultipartBody
*/
private function createMultipart()
{
// Flatten the nested query string values using the correct aggregator
return new MultipartBody(
call_user_func($this->getAggregator(), $this->fields),
$this->files
);
}
/**
* Creates an application/x-www-form-urlencoded stream body
*
* @return StreamInterface
*/
private function createUrlEncoded()
{
return Stream::factory($this->getFields(true));
}
/**
* Get rid of any cached data
*/
private function mutate()
{
$this->body = null;
}
}
|