Skip to content

Commit 5bba671

Browse files
authored
Merge pull request #185 from utopia-php/fix-header-override
Fix: header override
2 parents 70ecf88 + f055a71 commit 5bba671

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/Response.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,15 @@ public function enablePayload(): static
540540
*
541541
* @param string $key
542542
* @param string $value
543+
* @param bool $override
543544
*/
544-
public function addHeader(string $key, string $value): static
545+
public function addHeader(string $key, string $value, bool $override = false): static
545546
{
547+
if ($override) {
548+
$this->headers[$key] = $value;
549+
return $this;
550+
}
551+
546552
if (\array_key_exists($key, $this->headers)) {
547553
if (\is_array($this->headers[$key])) {
548554
$this->headers[$key][] = $value;
@@ -659,13 +665,21 @@ public function send(string $body = ''): void
659665
}
660666

661667
$serverHeader = $this->headers['Server'] ?? 'Utopia/Http';
662-
$this->addHeader('Server', $serverHeader);
668+
$this->addHeader('Server', $serverHeader, override: true);
663669

664670
$this->appendCookies();
665671

672+
$hasContentEncoding = false;
673+
foreach ($this->headers as $name => $values) {
674+
if (\strtolower($name) === 'content-encoding') {
675+
$hasContentEncoding = true;
676+
break;
677+
}
678+
}
679+
666680
// Compress body only if all conditions are met:
667681
if (
668-
empty($this->headers['content-encoding']) &&
682+
!$hasContentEncoding &&
669683
!empty($this->acceptEncoding) &&
670684
$this->isCompressible($this->contentType) &&
671685
strlen($body) > $this->compressionMinSize
@@ -674,14 +688,14 @@ public function send(string $body = ''): void
674688

675689
if ($algorithm) {
676690
$body = $algorithm->compress($body);
677-
$this->addHeader('Content-Length', (string) \strlen($body));
678-
$this->addHeader('Content-Encoding', $algorithm->getContentEncoding());
679-
$this->addHeader('X-Utopia-Compression', 'true');
680-
$this->addHeader('Vary', 'Accept-Encoding');
691+
$this->addHeader('Content-Length', (string) \strlen($body), override: true);
692+
$this->addHeader('Content-Encoding', $algorithm->getContentEncoding(), override: true);
693+
$this->addHeader('X-Utopia-Compression', 'true', override: true);
694+
$this->addHeader('Vary', 'Accept-Encoding', override: true);
681695
}
682696
}
683697

684-
$this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime));
698+
$this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime), override: true);
685699
$this->appendHeaders();
686700

687701
// Send response
@@ -769,7 +783,7 @@ public function chunk(string $body = '', bool $end = false): void
769783
$this->sent = true;
770784
}
771785

772-
$this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime));
786+
$this->addHeader('X-Debug-Speed', (string) (microtime(true) - $this->startTime), override: true);
773787

774788
$this
775789
->appendCookies()
@@ -799,7 +813,7 @@ protected function appendHeaders(): static
799813

800814
// Send content type header
801815
if (!empty($this->contentType)) {
802-
$this->addHeader('Content-Type', $this->contentType);
816+
$this->addHeader('Content-Type', $this->contentType, override: true);
803817
}
804818

805819
// Set application headers
@@ -908,7 +922,7 @@ public function redirect(string $url, int $statusCode = 301): void
908922
}
909923

910924
$this
911-
->addHeader('Location', $url)
925+
->addHeader('Location', $url, override: true)
912926
->setStatusCode($statusCode)
913927
->send('');
914928
}

tests/e2e/ResponseTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,10 @@ public function testSetCookie()
162162
$this->assertEquals(200, $response['headers']['status-code']);
163163
$this->assertEquals('value1', $response['cookies']['key1']);
164164
$this->assertEquals('value2', $response['cookies']['key2']);
165+
166+
$response = $this->client->call(Client::METHOD_GET, '/set-cookie-override');
167+
$this->assertEquals(200, $response['headers']['status-code']);
168+
$this->assertArrayNotHasKey('key1', $response['cookies']);
169+
$this->assertEquals('value2', $response['cookies']['key2']);
165170
}
166171
}

tests/e2e/server.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
$response->send('OK');
4343
});
4444

45+
App::get('/set-cookie-override')
46+
->inject('request')
47+
->inject('response')
48+
->action(function (Request $request, Response $response) {
49+
$response->addHeader('Set-Cookie', 'key1=value1', override: true);
50+
$response->addHeader('Set-Cookie', 'key2=value2', override: true);
51+
$response->send('OK');
52+
});
53+
4554
App::get('/chunked')
4655
->inject('response')
4756
->action(function (Response $response) {

0 commit comments

Comments
 (0)