@@ -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,7 +665,7 @@ 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
@@ -674,14 +680,14 @@ public function send(string $body = ''): void
674680
675681 if ($ algorithm ) {
676682 $ 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 ' );
683+ $ this ->addHeader ('Content-Length ' , (string ) \strlen ($ body ), override: true );
684+ $ this ->addHeader ('Content-Encoding ' , $ algorithm ->getContentEncoding (), override: true );
685+ $ this ->addHeader ('X-Utopia-Compression ' , 'true ' , override: true );
686+ $ this ->addHeader ('Vary ' , 'Accept-Encoding ' , override: true );
681687 }
682688 }
683689
684- $ this ->addHeader ('X-Debug-Speed ' , (string ) (microtime (true ) - $ this ->startTime ));
690+ $ this ->addHeader ('X-Debug-Speed ' , (string ) (microtime (true ) - $ this ->startTime ), override: true );
685691 $ this ->appendHeaders ();
686692
687693 // Send response
@@ -769,7 +775,7 @@ public function chunk(string $body = '', bool $end = false): void
769775 $ this ->sent = true ;
770776 }
771777
772- $ this ->addHeader ('X-Debug-Speed ' , (string ) (microtime (true ) - $ this ->startTime ));
778+ $ this ->addHeader ('X-Debug-Speed ' , (string ) (microtime (true ) - $ this ->startTime ), override: true );
773779
774780 $ this
775781 ->appendCookies ()
@@ -799,7 +805,7 @@ protected function appendHeaders(): static
799805
800806 // Send content type header
801807 if (!empty ($ this ->contentType )) {
802- $ this ->addHeader ('Content-Type ' , $ this ->contentType );
808+ $ this ->addHeader ('Content-Type ' , $ this ->contentType , override: true );
803809 }
804810
805811 // Set application headers
@@ -908,7 +914,7 @@ public function redirect(string $url, int $statusCode = 301): void
908914 }
909915
910916 $ this
911- ->addHeader ('Location ' , $ url )
917+ ->addHeader ('Location ' , $ url, override: true )
912918 ->setStatusCode ($ statusCode )
913919 ->send ('' );
914920 }
0 commit comments