@@ -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 }
0 commit comments