Skip to content

Commit 75926c2

Browse files
committed
Update Request::getMultipartBody method
1 parent 1ef3dc5 commit 75926c2

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/Request.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public function __toString() : string
119119
}
120120

121121
/**
122+
* NOTE: Created by comparing a request made with Firefox.
123+
*
122124
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#multipartform-data
123125
*
124126
* @return string
@@ -144,22 +146,30 @@ protected function getMultipartBody() : string
144146
$files = ArraySimple::convert($this->getFiles());
145147
foreach ($files as $field => $file) {
146148
$field = \htmlspecialchars($field, \ENT_QUOTES | \ENT_HTML5);
147-
$filename = \htmlspecialchars($file->getName(), \ENT_QUOTES | \ENT_HTML5);
149+
$field = \preg_replace('/\[\d+]/', '[]', $field);
150+
$filename = \htmlspecialchars($file->getFullPath(), \ENT_QUOTES | \ENT_HTML5);
151+
$contentType = $file->getClientType() ?: 'application/octet-stream';
148152
$getContentsOf = $file->isMoved() ? $file->getDestination() : $file->getTmpName();
149153
$data = '';
150154
if ($getContentsOf !== '') {
151155
$data = \file_get_contents($getContentsOf);
152156
}
153157
$bodyParts[] = \implode("\r\n", [
154158
"Content-Disposition: form-data; name=\"{$field}\"; filename=\"{$filename}\"",
155-
'Content-Type: ' . $file->getClientType(),
159+
'Content-Type: ' . $contentType,
156160
'',
157161
$data,
158162
]);
159163
}
160-
$boundary = \explode(';', $this->getContentType(), 2);
161-
$boundary = \trim($boundary[1]);
162-
$boundary = \substr($boundary, \strlen('boundary='));
164+
$boundary = '';
165+
$boundaryParts = \explode(';', $this->getContentType());
166+
foreach ($boundaryParts as $boundaryPart) {
167+
$boundaryPart = \trim($boundaryPart);
168+
if (\str_starts_with(\strtolower($boundaryPart), 'boundary=')) {
169+
$boundary = \substr($boundaryPart, \strlen('boundary='));
170+
break;
171+
}
172+
}
163173
foreach ($bodyParts as &$part) {
164174
$part = "--{$boundary}\r\n{$part}";
165175
}
@@ -175,6 +185,7 @@ protected function getMultipartBody() : string
175185
/*
176186
$serverLength = (string) $_SERVER['CONTENT_LENGTH'];
177187
$algoLength = (string) \strlen($bodyParts);
188+
\var_dump($serverLength, $algoLength);
178189
if ($serverLength !== $algoLength) {
179190
throw new \Exception(
180191
'$_SERVER CONTENT_LENGTH is ' . $serverLength

tests/RequestTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ public function testToStringMultipart() : void
352352
'full_path' => [
353353
1 => [
354354
'aa' => [
355-
0 => 'Test.php',
355+
0 => 'dir/Test.php',
356356
1 => '',
357357
],
358358
],
359-
2 => 'Other File.php',
359+
2 => 'dir/Other File.php',
360360
],
361361
'type' => [
362362
1 => [
@@ -422,26 +422,26 @@ public function testToStringMultipart() : void
422422
'Porto Alegre',
423423
];
424424
$files[0] = [
425-
'Content-Disposition: form-data; name="files[1][aa][0]"; filename="Test.php"',
425+
'Content-Disposition: form-data; name="files[][aa][]"; filename="dir/Test.php"',
426426
'Content-Type: application/x-httpd-php',
427427
'',
428428
\file_get_contents($filepath),
429429
];
430430
$files[1] = [
431-
'Content-Disposition: form-data; name="files[1][aa][1]"; filename=""',
432-
'Content-Type: ',
431+
'Content-Disposition: form-data; name="files[][aa][]"; filename=""',
432+
'Content-Type: application/octet-stream',
433433
'',
434434
'',
435435
];
436436
$files[2] = [
437-
'Content-Disposition: form-data; name="files[2]"; filename="Other File.php"',
437+
'Content-Disposition: form-data; name="files[]"; filename="dir/Other File.php"',
438438
'Content-Type: text/x-php',
439439
'',
440440
\file_get_contents($filepath),
441441
];
442442
$files[3] = [
443443
'Content-Disposition: form-data; name="foo"; filename=""',
444-
'Content-Type: ',
444+
'Content-Type: application/octet-stream',
445445
'',
446446
'',
447447
];
@@ -460,7 +460,7 @@ public function testToStringMultipart() : void
460460
$body .= \implode("\r\n", $files[3]) . "\r\n";
461461
$body .= $boundary . "--\r\n";
462462
$contentLength = \strlen($body);
463-
self::assertSame(953, $contentLength);
463+
self::assertSame(1004, $contentLength);
464464
$message = $startLine . "\r\n"
465465
. \implode("\r\n", $headerLines) . "\r\n"
466466
. "\r\n"

0 commit comments

Comments
 (0)