Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Improvements after learning how fpm and symfony work with headers
  • Loading branch information
DmitriiKoziuk committed Aug 9, 2023
commit 659f26c0605098e0b4c117b00f0e90c0f119077f
19 changes: 6 additions & 13 deletions src/swoole/src/SymfonyHttpBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Swoole\Http\Request;
use Swoole\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand Down Expand Up @@ -66,19 +65,13 @@ public static function reflectSymfonyResponse(SymfonyResponse $sfResponse, Respo
private static function buildServer(Request $request): array
{
$serverHeaders = [];
if (true === is_array($request->header)) {
foreach ($request->header as $name => $value) {
$name = strtoupper($name);

if (
false === in_array($name, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'])
&& false === str_starts_with($name, 'HTTP_')
) {
$name = sprintf('HTTP_%s', $name);
}

$serverHeaders[$name] = $value;
/** Inspired by https://github.com/symfony/symfony/blob/85366b4767b1761f40701f4ea6692d5280e0d58d/src/Symfony/Component/HttpFoundation/Request.php#L546-L553 */
foreach ($request->header ?? [] as $name => $value) {
$name = strtoupper(str_replace('-', '_', $name));
if (false === in_array($name, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'])) {
$name = sprintf('HTTP_%s', $name);
}
$serverHeaders[$name] = $value;
}

return array_merge(
Expand Down
23 changes: 16 additions & 7 deletions src/swoole/tests/Unit/SymfonyHttpBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testThatSwooleRequestIsConverted(): void
{
$request = $this->createMock(Request::class);
$request->server = ['request_method' => 'post'];
$request->header = ['content-type' => 'application/json'];
$request->header = ['content-type' => 'application/json', 'x-upstream-use' => 'swoole', 'http-upstream' => 'swoole'];
$request->cookie = ['foo' => 'cookie'];
$request->get = ['foo' => 'get'];
$request->post = ['foo' => 'post'];
Expand All @@ -40,8 +40,17 @@ public function testThatSwooleRequestIsConverted(): void

$sfRequest = SymfonyHttpBridge::convertSwooleRequest($request);

$this->assertSame(['REQUEST_METHOD' => 'post', 'HTTP_CONTENT-TYPE' => 'application/json'], $sfRequest->server->all());
$this->assertSame(['content-type' => ['application/json']], $sfRequest->headers->all());
$this->assertSame([
'REQUEST_METHOD' => 'post',
'CONTENT_TYPE' => 'application/json',
'HTTP_X_UPSTREAM_USE' => 'swoole',
'HTTP_HTTP_UPSTREAM' => 'swoole',
], $sfRequest->server->all());
$this->assertSame([
'content-type' => ['application/json'],
'x-upstream-use' => ['swoole'],
'http-upstream' => ['swoole'],
], $sfRequest->headers->all());
$this->assertSame(['foo' => 'cookie'], $sfRequest->cookies->all());
$this->assertSame(['foo' => 'get'], $sfRequest->query->all());
$this->assertSame(['foo' => 'post'], $sfRequest->request->all());
Expand Down Expand Up @@ -153,7 +162,7 @@ public function testThatSubRequestGetsParentRequestHeaders(): void
{
$request = $this->createMock(Request::class);
$request->server = ['request_method' => 'post'];
$request->header = ['host' => 'example.com', 'content-type' => 'application/json'];
$request->header = ['host' => 'example.com', 'content-type' => 'application/json', 'x-upstream-use' => 'swoole', 'http-upstream' => 'swoole'];
$request->cookie = ['foo' => 'cookie'];
$request->get = ['foo' => 'get'];
$request->post = ['foo' => 'post'];
Expand All @@ -166,12 +175,10 @@ public function testThatSubRequestGetsParentRequestHeaders(): void
'size' => 0,
],
];
$request->expects(self::once())->method('rawContent')->willReturn('{"foo": "body"}');

$sfRequest = SymfonyHttpBridge::convertSwooleRequest($request);

$this->assertSame(['host' => ['example.com'], 'content-type' => ['application/json']], $sfRequest->headers->all());

// Simulating creating sub-request
$sfSubRequest = SymfonyRequest::create(
'/some-url',
'get',
Expand All @@ -183,5 +190,7 @@ public function testThatSubRequestGetsParentRequestHeaders(): void

$this->assertSame('example.com', $sfSubRequest->headers->get('host'));
$this->assertSame('application/json', $sfSubRequest->headers->get('content-type'));
$this->assertSame('swoole', $sfSubRequest->headers->get('x-upstream-use'));
$this->assertSame('swoole', $sfSubRequest->headers->get('http-upstream'));
}
}