From 92dcffc34dc96075098f64b77f8d70e465fecf2e Mon Sep 17 00:00:00 2001 From: Weipeng Hong <961365124@qq.com> Date: Fri, 4 Aug 2023 10:48:32 +0800 Subject: [PATCH] fix make getting MIME type information is not case-sensitive #5114 (#5115) --- examples/http/UPPER.TXT | 1 + src/protocol/mime_type.cc | 6 ++- tests/swoole_http_server/bug_5114.phpt | 51 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 examples/http/UPPER.TXT create mode 100644 tests/swoole_http_server/bug_5114.phpt diff --git a/examples/http/UPPER.TXT b/examples/http/UPPER.TXT new file mode 100644 index 0000000000..428b97b82b --- /dev/null +++ b/examples/http/UPPER.TXT @@ -0,0 +1 @@ +HELLO WORLD! diff --git a/src/protocol/mime_type.cc b/src/protocol/mime_type.cc index 44d043c942..177606b285 100644 --- a/src/protocol/mime_type.cc +++ b/src/protocol/mime_type.cc @@ -18,6 +18,8 @@ #include "swoole_mime_type.h" +#include + namespace swoole { namespace mime_type { @@ -389,7 +391,9 @@ static std::unordered_map map_( static const std::string octet_stream("application/octet-stream"); static std::string get_suffix(const std::string &filename) { - return std::string(filename).substr(filename.find_last_of('.') + 1); + std::string suffix = std::string(filename).substr(filename.find_last_of('.') + 1); + std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower); + return suffix; } const std::unordered_map &list() { diff --git a/tests/swoole_http_server/bug_5114.phpt b/tests/swoole_http_server/bug_5114.phpt new file mode 100644 index 0000000000..43d1d1fbf9 --- /dev/null +++ b/tests/swoole_http_server/bug_5114.phpt @@ -0,0 +1,51 @@ +--TEST-- +swoole_http_server: bug #5114 +--SKIPIF-- + +--FILE-- +parentFunc = function () use ($pm) { + Swoole\Coroutine\run(function () use ($pm) { + $response = httpRequest("http://127.0.0.1:{$pm->getFreePort()}/http/UPPER.TXT"); + Assert::same($response['statusCode'], 200); + Assert::same($response['headers']['content-type'], 'text/plain'); + Assert::same($response['body'], "HELLO WORLD!\n"); + + $response = httpRequest("http://127.0.0.1:{$pm->getFreePort()}/http/test.txt"); + Assert::same($response['statusCode'], 200); + Assert::same($response['headers']['content-type'], 'text/plain'); + Assert::same($response['body'], "hello world!\n"); + }); + echo "DONE\n"; + $pm->kill(); +}; + +$pm->childFunc = function () use ($pm) { + $http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE); + $http->set([ + 'log_file' => '/dev/null', + 'open_http2_protocol' => true, + 'enable_static_handler' => true, + 'document_root' => dirname(dirname(__DIR__)) . '/examples/', + 'static_handler_locations' => ['/static', '/'] + ]); + $http->on('workerStart', function () use ($pm) { + $pm->wakeup(); + }); + $http->on('request', function (Request $request, Response $response) use ($http) { + $response->end('hello world'); + }); + $http->start(); +}; +$pm->childFirst(); +$pm->run(); +?> +--EXPECT-- +DONE