From ab42702ba7f155166cac41cecc5e6d99bdaa2abc Mon Sep 17 00:00:00 2001 From: Joan Miquel Date: Tue, 21 Apr 2026 00:06:53 +0200 Subject: [PATCH 1/5] Fix typo in Mime Type comments and improve method For easier use the function. Especially in middlewares. --- src/Protocols/Http/Response.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Protocols/Http/Response.php b/src/Protocols/Http/Response.php index 8ee51b7e0..1fad92676 100644 --- a/src/Protocols/Http/Response.php +++ b/src/Protocols/Http/Response.php @@ -56,7 +56,7 @@ class Response implements Stringable public ?array $file = null; /** - * Mine type map. + * Mime Type map. * @var array */ protected static array $mimeTypeMap = [ @@ -340,6 +340,17 @@ public function getHeaders(): array return $this->headers; } + /** + * Get Mime Type from extension. + * + * @param string $ext + * @return string + */ + public function getMimeType(string $ext): string + { + return self::$mimeTypeMap[$ext] ?? 'application/octet-stream'; + } + /** * Set status. * @@ -496,15 +507,12 @@ protected function createHeadForFile(array $fileInfo): string if ($baseName === '') { $baseName = 'unknown'; } + $mime = $this->getMimeType($extension); if (!isset($headers['Content-Type'])) { - if (isset(self::$mimeTypeMap[$extension])) { - $head .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n"; - } else { - $head .= "Content-Type: application/octet-stream\r\n"; - } + $head .= "Content-Type: " . $mime . "\r\n"; } - if (!isset($headers['Content-Disposition']) && !isset(self::$mimeTypeMap[$extension])) { + if (!isset($headers['Content-Disposition']) && $mime === 'application/octet-stream') { $head .= "Content-Disposition: attachment; filename=\"$baseName\"\r\n"; } From 8f6ed442a5484298933ae29604f356bfbb824063 Mon Sep 17 00:00:00 2001 From: Joan Miquel Date: Tue, 21 Apr 2026 00:40:37 +0200 Subject: [PATCH 2/5] Fix Content-Type header assignment in Response.php --- src/Protocols/Http/Response.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Protocols/Http/Response.php b/src/Protocols/Http/Response.php index 1fad92676..76c901bca 100644 --- a/src/Protocols/Http/Response.php +++ b/src/Protocols/Http/Response.php @@ -507,9 +507,10 @@ protected function createHeadForFile(array $fileInfo): string if ($baseName === '') { $baseName = 'unknown'; } - $mime = $this->getMimeType($extension); + $mime = ''; if (!isset($headers['Content-Type'])) { - $head .= "Content-Type: " . $mime . "\r\n"; + $mime = $this->getMimeType($extension); + $head .= "Content-Type: " . $mime . "\r\n"; } if (!isset($headers['Content-Disposition']) && $mime === 'application/octet-stream') { From 7fd2a91502ff8ccb8fa7f2ab98dd2d2596d85e6d Mon Sep 17 00:00:00 2001 From: Joan Miquel Date: Tue, 21 Apr 2026 00:58:28 +0200 Subject: [PATCH 3/5] Refactor ResponseTest for clarity and updates --- tests/Unit/Protocols/Http/ResponseTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Protocols/Http/ResponseTest.php b/tests/Unit/Protocols/Http/ResponseTest.php index 1bd61fd6a..ecbe70870 100644 --- a/tests/Unit/Protocols/Http/ResponseTest.php +++ b/tests/Unit/Protocols/Http/ResponseTest.php @@ -46,12 +46,10 @@ // as the implementation of headers is a different function for files. // or actually maybe the Response is the one should be rewritten to reuse? $response = new Response(); - $tmpFile = tempnam(sys_get_temp_dir(), 'test'); - rename($tmpFile, $tmpFile .'.jpg'); - $tmpFile .= '.jpg'; + $tmpFile = tempnam(sys_get_temp_dir(), 'test.jpg'); file_put_contents($tmpFile, 'hello, xiami'); $response->withFile($tmpFile, 0, 12); expect((string)$response) ->toContain('Content-Type: image/jpeg') ->toContain('Last-Modified: '); -}); \ No newline at end of file +}); From eded073d707f870d9ce4ef80a6ce89be3beae434 Mon Sep 17 00:00:00 2001 From: Joan Miquel Date: Tue, 21 Apr 2026 01:09:13 +0200 Subject: [PATCH 4/5] Refactor ResponseTest for improved clarity and structure --- tests/Unit/Protocols/Http/ResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Protocols/Http/ResponseTest.php b/tests/Unit/Protocols/Http/ResponseTest.php index ecbe70870..01c61d795 100644 --- a/tests/Unit/Protocols/Http/ResponseTest.php +++ b/tests/Unit/Protocols/Http/ResponseTest.php @@ -46,7 +46,7 @@ // as the implementation of headers is a different function for files. // or actually maybe the Response is the one should be rewritten to reuse? $response = new Response(); - $tmpFile = tempnam(sys_get_temp_dir(), 'test.jpg'); + $tmpFile = sys_get_temp_dir().'test.jpg'; file_put_contents($tmpFile, 'hello, xiami'); $response->withFile($tmpFile, 0, 12); expect((string)$response) From ef2fd8cb695ada04963e47a975c95537fb7c8dff Mon Sep 17 00:00:00 2001 From: Joan Miquel Date: Tue, 21 Apr 2026 01:12:39 +0200 Subject: [PATCH 5/5] Revert ResponseTest changes --- tests/Unit/Protocols/Http/ResponseTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Protocols/Http/ResponseTest.php b/tests/Unit/Protocols/Http/ResponseTest.php index 01c61d795..ec2a12654 100644 --- a/tests/Unit/Protocols/Http/ResponseTest.php +++ b/tests/Unit/Protocols/Http/ResponseTest.php @@ -46,7 +46,9 @@ // as the implementation of headers is a different function for files. // or actually maybe the Response is the one should be rewritten to reuse? $response = new Response(); - $tmpFile = sys_get_temp_dir().'test.jpg'; + $tmpFile = tempnam(sys_get_temp_dir(), 'test'); + rename($tmpFile, $tmpFile .'.jpg'); + $tmpFile .= '.jpg'; file_put_contents($tmpFile, 'hello, xiami'); $response->withFile($tmpFile, 0, 12); expect((string)$response)