Skip to content

[5.1.x]Fix unknown compress type #5781

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

Merged
merged 1 commit into from
May 27, 2025
Merged
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
15 changes: 10 additions & 5 deletions ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -817,29 +817,34 @@ 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 {
} else
#endif
{
accept_compression = 0;
}
}

const char *HttpContext::get_content_encoding() {
#ifdef SW_HAVE_ZLIB
if (compression_method == HTTP_COMPRESS_GZIP) {
return "gzip";
} else if (compression_method == HTTP_COMPRESS_DEFLATE) {
return "deflate";
}
} else
#endif
#ifdef SW_HAVE_BROTLI
else if (compression_method == HTTP_COMPRESS_BR) {
if (compression_method == HTTP_COMPRESS_BR) {
return "br";
}
} 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
@@ -1,6 +1,6 @@
#!/bin/sh -e
apt update
apt install -y libaio-dev libaio1 sqlite3 libsqlite3-dev unixodbc unixodbc-dev odbc-mariadb
apt install -y libaio-dev libaio1 sqlite3 libsqlite3-dev unixodbc unixodbc-dev odbc-mariadb zlib1g-dev
wget -nv https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip
unzip instantclient-basiclite-linuxx64.zip && rm instantclient-basiclite-linuxx64.zip
wget -nv https://download.oracle.com/otn_software/linux/instantclient/instantclient-sdk-linuxx64.zip
Expand Down
62 changes: 62 additions & 0 deletions tests/swoole_http_server/accept_encoding.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--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);
});
$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