Skip to content

Commit 86bca71

Browse files
[HttpClient] Support file uploads by nesting resource streams in option "body"
1 parent 33da4c8 commit 86bca71

File tree

5 files changed

+213
-18
lines changed

5 files changed

+213
-18
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `UriTemplateHttpClient` to use URI templates as specified in the RFC 6570
88
* Add `ServerSentEvent::getArrayData()` to get the Server-Sent Event's data decoded as an array when it's a JSON payload
99
* Allow array of urls as `base_uri` option value in `RetryableHttpClient` to retry on a new url each time
10+
* Support file uploads by nesting resource streams in option "body"
1011

1112
6.2
1213
---

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 115 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
1515
use Symfony\Component\HttpClient\Exception\TransportException;
16+
use Symfony\Component\HttpClient\Response\StreamableInterface;
17+
use Symfony\Component\HttpClient\Response\StreamWrapper;
18+
use Symfony\Component\Mime\MimeTypes;
1619

1720
/**
1821
* Provides the common logic from writing HttpClientInterface implementations.
@@ -94,11 +97,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
9497
}
9598

9699
if (isset($options['body'])) {
97-
if (\is_array($options['body']) && (!isset($options['normalized_headers']['content-type'][0]) || !str_contains($options['normalized_headers']['content-type'][0], 'application/x-www-form-urlencoded'))) {
98-
$options['normalized_headers']['content-type'] = ['Content-Type: application/x-www-form-urlencoded'];
99-
}
100-
101-
$options['body'] = self::normalizeBody($options['body']);
100+
$options['body'] = self::normalizeBody($options['body'], $options['normalized_headers']);
102101

103102
if (\is_string($options['body'])
104103
&& (string) \strlen($options['body']) !== substr($h = $options['normalized_headers']['content-length'][0] ?? '', 16)
@@ -313,21 +312,127 @@ private static function normalizeHeaders(array $headers): array
313312
*
314313
* @throws InvalidArgumentException When an invalid body is passed
315314
*/
316-
private static function normalizeBody($body)
315+
private static function normalizeBody($body, array &$normalizedHeaders = [])
317316
{
318317
if (\is_array($body)) {
319-
array_walk_recursive($body, $caster = static function (&$v) use (&$caster) {
320-
if (\is_object($v)) {
318+
static $cookie;
319+
320+
$streams = [];
321+
array_walk_recursive($body, $caster = static function (&$v) use (&$caster, &$streams, &$cookie) {
322+
if (\is_resource($v) || $v instanceof StreamableInterface) {
323+
$cookie = hash('xxh128', $cookie ??= random_bytes(8), true);
324+
$k = substr(strtr(base64_encode($cookie), '+/', '-_'), 0, -2);
325+
$streams[$k] = $v instanceof StreamableInterface ? $v->toStream(false) : $v;
326+
$v = $k;
327+
} elseif (\is_object($v)) {
321328
if ($vars = get_object_vars($v)) {
322329
array_walk_recursive($vars, $caster);
323330
$v = $vars;
324-
} elseif (method_exists($v, '__toString')) {
331+
} elseif ($v instanceof \Stringable) {
325332
$v = (string) $v;
326333
}
327334
}
328335
});
329336

330-
return http_build_query($body, '', '&');
337+
$body = http_build_query($body, '', '&');
338+
339+
if ('' === $body || !$streams && !str_contains($normalizedHeaders['content-type'][0] ?? '', 'multipart/form-data')) {
340+
if (!str_contains($normalizedHeaders['content-type'][0] ?? '', 'application/x-www-form-urlencoded')) {
341+
$normalizedHeaders['content-type'] = ['Content-Type: application/x-www-form-urlencoded'];
342+
}
343+
344+
return $body;
345+
}
346+
347+
if (preg_match('{multipart/form-data; boundary=(?|"([^"\r\n]++)"|([-!#$%&\'*+.^_`|~_A-Za-z0-9]++))}', $normalizedHeaders['content-type'][0] ?? '', $boundary)) {
348+
$boundary = $boundary[1];
349+
} else {
350+
$boundary = substr(strtr(base64_encode($cookie ??= random_bytes(8)), '+/', '-_'), 0, -2);
351+
$normalizedHeaders['content-type'] = ['Content-Type: multipart/form-data; boundary='.$boundary];
352+
}
353+
354+
$body = explode('&', $body);
355+
$contentLength = 0;
356+
357+
foreach ($body as $i => $part) {
358+
[$k, $v] = explode('=', $part, 2);
359+
$part = ($i ? "\r\n" : '')."--{$boundary}\r\n";
360+
$k = str_replace(['"', "\r", "\n"], ['%22', '%0D', '%0A'], urldecode($k)); // see WHATWG HTML living standard
361+
362+
if (!isset($streams[$v])) {
363+
$part .= "Content-Disposition: form-data; name=\"{$k}\"\r\n\r\n".urldecode($v);
364+
$contentLength += 0 <= $contentLength ? \strlen($part) : 0;
365+
$body[$i] = [$k, $part, null];
366+
continue;
367+
}
368+
$v = $streams[$v];
369+
370+
if (!\is_array($m = @stream_get_meta_data($v))) {
371+
throw new TransportException(sprintf('Invalid "%s" resource found in body part "%s".', get_resource_type($v), $k));
372+
}
373+
if (feof($v)) {
374+
throw new TransportException(sprintf('Uploaded stream ended for body part "%s".', $k));
375+
}
376+
377+
$m += stream_context_get_options($v)['http'] ?? [];
378+
$filename = basename($m['filename'] ?? $m['uri'] ?? 'unknown');
379+
$filename = str_replace(['"', "\r", "\n"], ['%22', '%0D', '%0A'], $filename);
380+
$contentType = $m['content_type'] ?? null;
381+
382+
if (($headers = $m['wrapper_data'] ?? []) instanceof StreamWrapper) {
383+
$hasContentLength = false;
384+
$headers = $headers->getResponse()->getInfo('response_headers');
385+
} elseif ($hasContentLength = 0 < $h = fstat($v)['size'] ?? 0) {
386+
$contentLength += 0 <= $contentLength ? $h : 0;
387+
}
388+
389+
foreach (\is_array($headers) ? $headers : [] as $h) {
390+
if (\is_string($h) && 0 === stripos($h, 'Content-Type: ')) {
391+
$contentType ??= substr($h, 14);
392+
} elseif (!$hasContentLength && \is_string($h) && 0 === stripos($h, 'Content-Length: ')) {
393+
$hasContentLength = true;
394+
$contentLength += 0 <= $contentLength ? substr($h, 16) : 0;
395+
} elseif (\is_string($h) && 0 === stripos($h, 'Content-Encoding: ')) {
396+
$contentLength = -1;
397+
}
398+
}
399+
400+
if (!$hasContentLength) {
401+
$contentLength = -1;
402+
}
403+
if (null === $contentType && 'plainfile' === ($m['wrapper_type'] ?? null) && isset($m['uri'])) {
404+
static $mimeTypes;
405+
$mimeTypes ??= class_exists(MimeTypes::class) ? new MimeTypes() : false;
406+
$contentType = $mimeTypes ? $mimeTypes->guessMimeType($m['uri']) : null;
407+
}
408+
$contentType ??= 'application/octet-stream';
409+
410+
$part .= "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$filename}\"\r\n";
411+
$part .= "Content-Type: {$contentType}\r\n\r\n";
412+
413+
$contentLength += 0 <= $contentLength ? \strlen($part) : 0;
414+
$body[$i] = [$k, $part, $v];
415+
}
416+
417+
$body[++$i] = ['', "\r\n--{$boundary}--\r\n", null];
418+
419+
if (0 < $contentLength) {
420+
$normalizedHeaders['content-length'] = ['Content-Length: '.($contentLength += \strlen($body[$i][1]))];
421+
}
422+
423+
$body = static function ($size) use ($body) {
424+
foreach ($body as [$k, $part, $h]) {
425+
yield $part;
426+
427+
while (null !== $h && !feof($h)) {
428+
if (false === $part = fread($h, $size)) {
429+
throw new TransportException(sprintf('Error while reading uploaded stream for body part "%s".', $k));
430+
}
431+
432+
yield $part;
433+
}
434+
}
435+
};
331436
}
332437

333438
if (\is_string($body)) {

src/Symfony/Component/HttpClient/Internal/CurlClientState.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class CurlClientState extends ClientState
3333
public array $pauseExpiries = [];
3434
public int $execCounter = \PHP_INT_MIN;
3535
public ?LoggerInterface $logger = null;
36+
public bool $performing = false;
3637

3738
public static array $curlVersion;
3839

src/Symfony/Component/HttpClient/Response/CurlResponse.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ final class CurlResponse implements ResponseInterface, StreamableInterface
3232
}
3333
use TransportResponseTrait;
3434

35-
private static bool $performing = false;
3635
private CurlClientState $multi;
3736

3837
/**
@@ -182,7 +181,7 @@ public function __construct(CurlClientState $multi, \CurlHandle|string $ch, arra
182181
unset($multi->pauseExpiries[$id], $multi->openHandles[$id], $multi->handlesActivity[$id]);
183182
curl_setopt($ch, \CURLOPT_PRIVATE, '_0');
184183

185-
if (self::$performing) {
184+
if ($multi->performing) {
186185
return;
187186
}
188187

@@ -234,13 +233,13 @@ public function getInfo(string $type = null): mixed
234233

235234
public function getContent(bool $throw = true): string
236235
{
237-
$performing = self::$performing;
238-
self::$performing = $performing || '_0' === curl_getinfo($this->handle, \CURLINFO_PRIVATE);
236+
$performing = $this->multi->performing;
237+
$this->multi->performing = $performing || '_0' === curl_getinfo($this->handle, \CURLINFO_PRIVATE);
239238

240239
try {
241240
return $this->doGetContent($throw);
242241
} finally {
243-
self::$performing = $performing;
242+
$this->multi->performing = $performing;
244243
}
245244
}
246245

@@ -279,7 +278,7 @@ private static function schedule(self $response, array &$runningResponses): void
279278
*/
280279
private static function perform(ClientState $multi, array &$responses = null): void
281280
{
282-
if (self::$performing) {
281+
if ($multi->performing) {
283282
if ($responses) {
284283
$response = current($responses);
285284
$multi->handlesActivity[(int) $response->handle][] = null;
@@ -290,7 +289,7 @@ private static function perform(ClientState $multi, array &$responses = null): v
290289
}
291290

292291
try {
293-
self::$performing = true;
292+
$multi->performing = true;
294293
++$multi->execCounter;
295294
$active = 0;
296295
while (\CURLM_CALL_MULTI_PERFORM === ($err = curl_multi_exec($multi->handle, $active))) {
@@ -327,7 +326,7 @@ private static function perform(ClientState $multi, array &$responses = null): v
327326
$multi->handlesActivity[$id][] = \in_array($result, [\CURLE_OK, \CURLE_TOO_MANY_REDIRECTS], true) || '_0' === $waitFor || curl_getinfo($ch, \CURLINFO_SIZE_DOWNLOAD) === curl_getinfo($ch, \CURLINFO_CONTENT_LENGTH_DOWNLOAD) ? null : new TransportException(ucfirst(curl_error($ch) ?: curl_strerror($result)).sprintf(' for "%s".', curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL)));
328327
}
329328
} finally {
330-
self::$performing = false;
329+
$multi->performing = false;
331330
}
332331
}
333332

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
16+
use Symfony\Component\HttpClient\HttpClient;
1617
use Symfony\Component\HttpClient\HttpClientTrait;
1718
use Symfony\Contracts\HttpClient\HttpClientInterface;
1819

@@ -68,6 +69,94 @@ public function testPrepareRequestWithBodyIsArray()
6869
$this->assertContains('Content-Type: application/x-www-form-urlencoded; charset=utf-8', $options['headers']);
6970
}
7071

72+
public function testNormalizeBodyMultipart()
73+
{
74+
$file = fopen('php://memory', 'r+');
75+
stream_context_set_option($file, ['http' => [
76+
'filename' => 'test.txt',
77+
'content_type' => 'text/plain',
78+
]]);
79+
fwrite($file, 'foobarbaz');
80+
rewind($file);
81+
82+
$headers = [
83+
'content-type' => ['Content-Type: multipart/form-data; boundary=ABCDEF'],
84+
];
85+
$body = [
86+
'foo[]' => 'bar',
87+
'bar' => [
88+
$file,
89+
],
90+
];
91+
92+
$body = self::normalizeBody($body, $headers);
93+
94+
$result = '';
95+
while ('' !== $data = $body(self::$CHUNK_SIZE)) {
96+
$result .= $data;
97+
}
98+
99+
$expected = <<<'EOF'
100+
--ABCDEF
101+
Content-Disposition: form-data; name="foo[]"
102+
103+
bar
104+
--ABCDEF
105+
Content-Disposition: form-data; name="bar[0]"; filename="test.txt"
106+
Content-Type: text/plain
107+
108+
foobarbaz
109+
--ABCDEF--
110+
111+
EOF;
112+
$expected = str_replace("\n", "\r\n", $expected);
113+
114+
$this->assertSame($expected, $result);
115+
}
116+
117+
/**
118+
* @group network
119+
*
120+
* @dataProvider provideNormalizeBodyMultipartForwardStream
121+
*/
122+
public function testNormalizeBodyMultipartForwardStream($stream)
123+
{
124+
$body = [
125+
'logo' => $stream,
126+
];
127+
128+
$headers = [];
129+
$body = self::normalizeBody($body, $headers);
130+
131+
$result = '';
132+
while ('' !== $data = $body(self::$CHUNK_SIZE)) {
133+
$result .= $data;
134+
}
135+
136+
$this->assertSame(1, preg_match('/^Content-Type: multipart\/form-data; boundary=(?<boundary>.+)$/', $headers['content-type'][0], $matches));
137+
$this->assertSame('Content-Length: 3086', $headers['content-length'][0]);
138+
$this->assertSame(3086, \strlen($result));
139+
140+
$expected = <<<EOF
141+
--{$matches['boundary']}
142+
Content-Disposition: form-data; name="logo"; filename="1f44d.png"
143+
Content-Type: image/png
144+
145+
%A
146+
--{$matches['boundary']}--
147+
148+
EOF;
149+
$expected = str_replace("\n", "\r\n", $expected);
150+
151+
$this->assertStringMatchesFormat($expected, $result);
152+
}
153+
154+
public static function provideNormalizeBodyMultipartForwardStream()
155+
{
156+
yield 'native' => [fopen('https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png', 'r')];
157+
yield 'symfony' => [HttpClient::create()->request('GET', 'https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png')->toStream()];
158+
}
159+
71160
/**
72161
* @dataProvider provideResolveUrl
73162
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy