Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Drivers/LaravelHttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Amp\ByteStream\ReadableResourceStream;
use Amp\Http\Cookie\RequestCookie;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Driver\DefaultHttpDriverFactory;
use Amp\Http\Server\HttpServer as AmpHttpServer;
use Amp\Http\Server\HttpServerStatus;
use Amp\Http\Server\Request as AmpRequest;
Expand Down Expand Up @@ -99,7 +100,16 @@ public function start(): void
return;
}

$this->socket = $server = SocketHttpServer::createForDirectAccess(new NullLogger());
// Increase the body size limit beyond Amp's 128-KiB
// default to prevent deadlocks when reading large
// JSON payloads in POST, PUT, and PATCH requests.
$this->socket = $server = SocketHttpServer::createForDirectAccess(
logger: $logger = new NullLogger(),
httpDriverFactory: new DefaultHttpDriverFactory(
logger: $logger,
bodySizeLimit: 64 * 1024 * 1024,
),
);

$server->expose("{$this->host}:{$this->port}");
$server->start(
Expand Down
47 changes: 47 additions & 0 deletions tests/Browser/Visit/LargeBodyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

it('accepts JSON request bodies larger than the default 128 KiB limit', function (): void {
Route::post('/large-body/echo', fn (Request $request): array => [
'received_bytes' => mb_strlen($request->getContent()),
'received_count' => count($request->json('items', [])),
]);

// Build a payload that comfortably exceeds Amp's 128 KiB default.
$items = [];
for ($i = 0; $i < 5000; $i++) {
$items[] = [
'id' => $i,
'name' => str_repeat('x', 32),
'description' => str_repeat('y', 64),
];
}
$payload = json_encode(['items' => $items]);

expect(mb_strlen($payload))->toBeGreaterThan(128 * 1024);

// Render a tiny page that POSTs the payload via fetch and writes the JSON
// response into the DOM so we can assert against it.
Route::get('/large-body/post-from-page', fn (): string => '
<html><body>
<pre id="result"></pre>
<script>
fetch("/large-body/echo", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: '.json_encode($payload).',
})
.then(r => r.json())
.then(data => { document.getElementById("result").textContent = JSON.stringify(data); });
</script>
</body></html>
');

visit('/large-body/post-from-page')
->assertSee('"received_bytes":'.mb_strlen($payload))
->assertSee('"received_count":5000');
});
Loading