Skip to content

Commit 3fd368e

Browse files
committed
Fix unknown compress type
1 parent 04b3f82 commit 3fd368e

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

ext-src/swoole_http_request.cc

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -819,39 +819,45 @@ void HttpContext::set_compression_method(const char *accept_encoding, size_t len
819819
compression_method = HTTP_COMPRESS_BR;
820820
} else
821821
#endif
822+
#ifdef SW_HAVE_ZLIB
822823
if (swoole_strnpos(accept_encoding, length, ZEND_STRL("gzip")) >= 0) {
823824
accept_compression = 1;
824825
compression_method = HTTP_COMPRESS_GZIP;
825826
} else if (swoole_strnpos(accept_encoding, length, ZEND_STRL("deflate")) >= 0) {
826827
accept_compression = 1;
827828
compression_method = HTTP_COMPRESS_DEFLATE;
829+
} else
830+
#endif
828831
#ifdef SW_HAVE_ZSTD
829-
} else if (swoole_strnpos(accept_encoding, length, ZEND_STRL("zstd")) >= 0) {
832+
if (swoole_strnpos(accept_encoding, length, ZEND_STRL("zstd")) >= 0) {
830833
accept_compression = 1;
831834
compression_method = HTTP_COMPRESS_ZSTD;
835+
} else
832836
#endif
833-
} else {
837+
{
834838
accept_compression = 0;
835839
}
836840
}
837841

838842
const char *HttpContext::get_content_encoding() {
839-
if (compression_method == HTTP_COMPRESS_GZIP) {
843+
#ifdef SW_HAVE_BROTLI
844+
if (compression_method == HTTP_COMPRESS_BR) {
845+
return "br";
846+
} else
847+
#endif
848+
#ifdef SW_HAVE_ZLIB
849+
if (compression_method == HTTP_COMPRESS_GZIP) {
840850
return "gzip";
841851
} else if (compression_method == HTTP_COMPRESS_DEFLATE) {
842852
return "deflate";
843-
}
844-
#ifdef SW_HAVE_BROTLI
845-
else if (compression_method == HTTP_COMPRESS_BR) {
846-
return "br";
847-
}
853+
} else
848854
#endif
849855
#ifdef SW_HAVE_ZSTD
850-
else if (compression_method == HTTP_COMPRESS_ZSTD) {
856+
if (compression_method == HTTP_COMPRESS_ZSTD) {
851857
return "zstd";
852-
}
858+
} else
853859
#endif
854-
else {
860+
{
855861
return nullptr;
856862
}
857863
}

scripts/library.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ else
66
fi
77

88
apt update
9-
apt install -y cmake make gcc libssl-dev libmariadb-dev unixodbc-dev libaio-dev libaio1 sqlite3 libsqlite3-dev libzstd-dev
9+
apt install -y cmake make gcc libssl-dev libmariadb-dev unixodbc-dev libaio-dev libaio1 sqlite3 libsqlite3-dev libzstd-dev zlib1g-dev
1010
wget https://github.com/mariadb-corporation/mariadb-connector-odbc/archive/refs/tags/3.1.21.tar.gz
1111
tar zxf 3.1.21.tar.gz
1212
mkdir build
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
swoole_http_server: accept encoding type
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
use Swoole\Http\Server;
10+
use Swoole\Http\Request;
11+
use Swoole\Http\Response;
12+
use Swoole\Runtime;
13+
use function Swoole\Coroutine\run;
14+
15+
function curl_request(string $type, string $url) {
16+
$ch = curl_init();
17+
curl_setopt($ch, CURLOPT_URL, $url);
18+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
19+
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Encoding: {$type}"]);
20+
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $headerLine) use ($type) {
21+
if (stripos($headerLine, 'Content-Encoding:') !== false) {
22+
Assert::true(stripos($headerLine, $type) !== false);
23+
}
24+
return strlen($headerLine);
25+
});
26+
curl_exec($ch);
27+
curl_close($ch);
28+
}
29+
30+
$pm = new ProcessManager;
31+
$pm->parentFunc = function () use ($pm)
32+
{
33+
Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
34+
run(function () use ($pm) {
35+
$url = "http://127.0.0.1:".$pm->getFreePort();
36+
curl_request('br', $url);
37+
curl_request('gzip', $url);
38+
curl_request('deflate', $url);
39+
curl_request('zstd', $url);
40+
});
41+
$pm->kill();
42+
};
43+
44+
$pm->childFunc = function () use ($pm)
45+
{
46+
$http = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE, SWOOLE_SOCK_TCP);
47+
$http->set([
48+
'http_compression' => true,
49+
]);
50+
$http->on("WorkerStart", function ($serv, $wid) {
51+
global $pm;
52+
$pm->wakeup();
53+
});
54+
$http->on("request", function (Request $request, Response $response) {
55+
$response->end(co::readFile(__DIR__ . '/../../README.md'));
56+
});
57+
$http->start();
58+
};
59+
60+
$pm->childFirst();
61+
$pm->run();
62+
?>
63+
--EXPECT--

0 commit comments

Comments
 (0)