Skip to content

[6.0]Fix unknown compress type #5780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -819,39 +819,45 @@ void HttpContext::set_compression_method(const char *accept_encoding, size_t len
compression_method = HTTP_COMPRESS_BR;
} else
#endif
#ifdef SW_HAVE_ZLIB
if (swoole_strnpos(accept_encoding, length, ZEND_STRL("gzip")) >= 0) {
accept_compression = 1;
compression_method = HTTP_COMPRESS_GZIP;
} else if (swoole_strnpos(accept_encoding, length, ZEND_STRL("deflate")) >= 0) {
accept_compression = 1;
compression_method = HTTP_COMPRESS_DEFLATE;
} else
#endif
#ifdef SW_HAVE_ZSTD
} else if (swoole_strnpos(accept_encoding, length, ZEND_STRL("zstd")) >= 0) {
if (swoole_strnpos(accept_encoding, length, ZEND_STRL("zstd")) >= 0) {
accept_compression = 1;
compression_method = HTTP_COMPRESS_ZSTD;
} else
#endif
} else {
{
accept_compression = 0;
}
}

const char *HttpContext::get_content_encoding() {
if (compression_method == HTTP_COMPRESS_GZIP) {
#ifdef SW_HAVE_BROTLI
if (compression_method == HTTP_COMPRESS_BR) {
return "br";
} else
#endif
#ifdef SW_HAVE_ZLIB
if (compression_method == HTTP_COMPRESS_GZIP) {
return "gzip";
} else if (compression_method == HTTP_COMPRESS_DEFLATE) {
return "deflate";
}
#ifdef SW_HAVE_BROTLI
else if (compression_method == HTTP_COMPRESS_BR) {
return "br";
}
} else
#endif
#ifdef SW_HAVE_ZSTD
else if (compression_method == HTTP_COMPRESS_ZSTD) {
if (compression_method == HTTP_COMPRESS_ZSTD) {
return "zstd";
}
} else
#endif
else {
{
return nullptr;
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ else
fi

apt update
apt install -y cmake make gcc libssl-dev libmariadb-dev unixodbc-dev libaio-dev libaio1 sqlite3 libsqlite3-dev libzstd-dev
apt install -y cmake make gcc libssl-dev libmariadb-dev unixodbc-dev libaio-dev libaio1 sqlite3 libsqlite3-dev libzstd-dev zlib1g-dev
wget https://github.com/mariadb-corporation/mariadb-connector-odbc/archive/refs/tags/3.1.21.tar.gz
tar zxf 3.1.21.tar.gz
mkdir build
Expand Down
63 changes: 63 additions & 0 deletions tests/swoole_http_server/accept_encoding.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--TEST--
swoole_http_server: accept encoding type
--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;
use Swoole\Runtime;
use function Swoole\Coroutine\run;

function curl_request(string $type, string $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Encoding: {$type}"]);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $headerLine) use ($type) {
if (stripos($headerLine, 'Content-Encoding:') !== false) {
Assert::true(stripos($headerLine, $type) !== false);
}
return strlen($headerLine);
});
curl_exec($ch);
curl_close($ch);
}

$pm = new ProcessManager;
$pm->parentFunc = function () use ($pm)
{
Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
run(function () use ($pm) {
$url = "http://127.0.0.1:".$pm->getFreePort();
curl_request('br', $url);
curl_request('gzip', $url);
curl_request('deflate', $url);
curl_request('zstd', $url);
});
$pm->kill();
};

$pm->childFunc = function () use ($pm)
{
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE, SWOOLE_SOCK_TCP);
$http->set([
'http_compression' => true,
]);
$http->on("WorkerStart", function ($serv, $wid) {
global $pm;
$pm->wakeup();
});
$http->on("request", function (Request $request, Response $response) {
$response->end(co::readFile(__DIR__ . '/../../README.md'));
});
$http->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
Loading