Skip to content

Commit

Permalink
fix make getting MIME type information is not case-sensitive #5114 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hongweipeng authored Aug 4, 2023
1 parent 08ea5db commit 92dcffc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/http/UPPER.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HELLO WORLD!
6 changes: 5 additions & 1 deletion src/protocol/mime_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "swoole_mime_type.h"

#include <algorithm>

namespace swoole {
namespace mime_type {

Expand Down Expand Up @@ -389,7 +391,9 @@ static std::unordered_map<std::string, std::string> 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<std::string, std::string> &list() {
Expand Down
51 changes: 51 additions & 0 deletions tests/swoole_http_server/bug_5114.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
swoole_http_server: bug #5114
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$pm = new SwooleTest\ProcessManager;
$pm->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

0 comments on commit 92dcffc

Please sign in to comment.