fix: Add multipart form-data support for Laravel HTTP server#200
fix: Add multipart form-data support for Laravel HTTP server#200homiedopie wants to merge 6 commits intopestphp:4.xfrom
Conversation
|
Good day, I detect an error when you don't select a file: The browser send a file with the header Additionally, I notice that other corner cases like when the file is greater that the max file size defined in the INI file (UPLOAD_ERR_INI_SIZE and UPLOAD_ERR_FORM_SIZE) |
|
@gtg-bantonio Thanks for the feedback. Ill check it out and will add some test as well! |
|
@gtg-bantonio I addressed it on the new fixes I have added. Can you please check on your end? Thank you! |
7527459 to
52ad5dd
Compare
52ad5dd to
6e72b2e
Compare
|
I found another bug, with nested indexed fields in a linear sequence: The decoded data should be (in PHP array notation) [
'extra' => 'Test',
'data' => [
0 => [
'field1' => '1',
'field2' => '1',
],
1 => [
'field1' => '2',
'field2' => '0',
],
],
]But instead the parser returns: [
'extra' => 'Test',
'data' => [
0 => [
'field1' => '1',
'field2' => ['0', '1'],
],
1 => [
'field1' => '2',
'field2' => '0',
],
],
] |
|
Can I ask, how is this PR different from the other PR #177 ? Confused on which one to follow. |
|
@akulmehta in this PR, the library that use is the |
|
@gtg-bantonio - thanks for explaining it! I addressed the issue already and separated the test (dedicated to multipart upload/parameters) |
|
I can't found another error with our full script, and the tests and code are better that mine, so I will close my PR in favor of yours |
b5a29af to
f0400c0
Compare
|
@gtg-bantonio - Thanks again for your help! wouldnt be possible without your guidance and inputs! |
| $cookies = array_map(fn (RequestCookie $cookie): string => urldecode($cookie->getValue()), $request->getCookies()); | ||
| $cookies = array_merge($cookies, test()->prepareCookiesForRequest()); // @phpstan-ignore-line | ||
| /** @var array<string, string> $serverVariables */ | ||
| $serverVariables = test()->serverVariables(); // @phpstan-ignore-line |
There was a problem hiding this comment.
$_SERVER['CONTENT_TYPE'] is still inconsistent with $request->header('content-type'), when checked in the Laravel end.
You can fix this by adding $serverVariables['CONTENT_TYPE'] = $contentType; right here, and verify with this test:
it('matches content-type in server variables when uploading files', function (): void {
Route::get('/form', fn (): string => '
<html>
<head></head>
<body>
<form method="post" action="/content-type" enctype="multipart/form-data">
<label for="file1">A file</label>
<input id="file1" type="file" name="file1">
<button type="submit">Send</button>
</form>
</body>
</html>
');
Route::post(
'/content-type',
fn (Request $request): string => $request->server('CONTENT_TYPE') === $request->header('content-type') ? 'true' : 'false',
);
visit('/form')
->attach('A file', fixture('lorem-ipsum.txt'))
->screenshot(true, 'OOF')
->click('Send')
->assertSee('true');
});There was a problem hiding this comment.
@bluesheep100 - I added one for content length as well. Tests are added. Can you please check on your end? Thank you for your feedback!
There was a problem hiding this comment.
Content type is still missing from $_SERVER; apparently I had it wrong how it works.
However looking into Symfony\Component\HttpFoundation\Request, I can see it has an overrideGlobals() method.
It seems most correct to call that after setting the request headers, and it will update $_SERVER appropriately, instead of handling it manually on a case-by-case basis.
Here's the relevant snippet:
foreach ($this->headers->all() as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
$_SERVER[$key] = implode(', ', $value);
} else {
$_SERVER['HTTP_'.$key] = implode(', ', $value);
}
}EDIT: Can confirm that $symfonyRequest->overrideGlobals() also makes my file uploading browser tests pass.
There was a problem hiding this comment.
@bluesheep100 - This has been addressed, I added a case for content_md5 as well to cover all bases. Please check thanks!
Problem
Requestobject.Fix
UploadedFileinstances.Test
Verification
Note
Related
Related #177 - Thanks to @gtg-bantonio
Fixes #1495