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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- `RequestConverter::toSymfonyRequest()` now returns empty content for multipart/form-data requests
- Matches PHP-FPM behavior where `php://input` is not available for multipart
- Previously `getContent()` returned full raw body including file contents
- Files remain accessible via `$request->files` as before
- **Migration**: If your code relies on reading raw multipart body via `getContent()`, you'll need to adapt it

## [0.13.0] - 2026-04-05

### Added
Expand Down
12 changes: 9 additions & 3 deletions src/DTO/RequestConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public static function toSymfonyRequest(\Workerman\Protocols\Http\Request $rawRe

// Only populate POST bag for form-encoded content types
// JSON and other content types should leave POST bag empty (like PHP-FPM)
$contentType = $rawRequest->header('content-type', '');
$isFormData = preg_match('/^(application\/x-www-form-urlencoded|multipart\/form-data)\b/i', (string) $contentType);
$contentType = strtolower((string) $rawRequest->header('content-type', ''));
$isFormUrlEncoded = str_starts_with($contentType, 'application/x-www-form-urlencoded');
$isMultipart = str_starts_with($contentType, 'multipart/form-data');
$isFormData = $isFormUrlEncoded || $isMultipart;

// Build server bag with HTTP_* headers (CGI convention)
$headers = $rawRequest->header() ?? [];
Expand Down Expand Up @@ -69,14 +71,18 @@ public static function toSymfonyRequest(\Workerman\Protocols\Http\Request $rawRe
}
}

// For multipart requests, pass empty content to match PHP-FPM behavior
// (php://input is not available for multipart - body is consumed during $_POST/$_FILES parsing)
$content = $isMultipart ? '' : $rawRequest->rawBody();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Before this change, getContent() on a multipart request returned the full raw body. Now it returns ''. Any application code, middleware, or custom parser reading raw multipart data will silently break.

The behavior is correct for PHP-FPM compatibility, but this should be documented as a breaking change in CHANGELOG.md with a migration note so existing users are aware.

— coder-model via Qwen Code /review


return new Request(
is_array($query) ? $query : [],
$isFormData && is_array($post) ? $post : [],
[],
is_array($cookies) ? $cookies : [],
$files,
$server,
$rawRequest->rawBody(),
$content,
);
}
}
50 changes: 50 additions & 0 deletions tests/RequestConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,56 @@ public function testRequestTimeAndRequestTimeFloatAreSet(): void
$this->assertSame($requestTime, (int) $requestTimeFloat);
}

public function testMultipartRequestReturnsEmptyContent(): void
{
$buffer = $this->createMultipartRequest(
boundary: 'TestBoundary',
fields: [
[
'name' => 'test_file',
'filename' => 'test.txt',
'content' => 'test content',
],
],
);

$rawRequest = new Request($buffer);
$symfonyRequest = RequestConverter::toSymfonyRequest($rawRequest);

$this->assertSame('', $symfonyRequest->getContent());
$this->assertTrue($symfonyRequest->files->has('test_file'));
}

public function testJsonRequestPreservesContent(): void
{
$buffer = "POST /test HTTP/1.1\r\n";
$buffer .= "Host: localhost\r\n";
$buffer .= "Content-Type: application/json\r\n";
$buffer .= "Content-Length: 15\r\n";
$buffer .= "\r\n";
$buffer .= '{"key":"value"}';

$rawRequest = new Request($buffer);
$symfonyRequest = RequestConverter::toSymfonyRequest($rawRequest);

$this->assertSame('{"key":"value"}', $symfonyRequest->getContent());
}

public function testFormUrlEncodedPreservesContent(): void
{
$buffer = "POST /test HTTP/1.1\r\n";
$buffer .= "Host: localhost\r\n";
$buffer .= "Content-Type: application/x-www-form-urlencoded\r\n";
$buffer .= "Content-Length: 9\r\n";
$buffer .= "\r\n";
$buffer .= 'key=value';

$rawRequest = new Request($buffer);
$symfonyRequest = RequestConverter::toSymfonyRequest($rawRequest);

$this->assertSame('key=value', $symfonyRequest->getContent());
}

private function createMockConnection(int $localPort): \Workerman\Connection\TcpConnection
{
return new class ($localPort) extends \Workerman\Connection\TcpConnection {
Expand Down
Loading