Skip to content

Commit 7eea3b7

Browse files
authored
Merge pull request #55433 from nextcloud/ernolf/enh/http2-brotli-client
perf(client): enable HTTP/2 and brotli support in internal HTTP client
2 parents 635e26d + 7c526b4 commit 7eea3b7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

lib/private/Http/Client/Client.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ private function buildRequestOptions(array $options): array {
5656
$defaults = [
5757
RequestOptions::VERIFY => $this->getCertBundle(),
5858
RequestOptions::TIMEOUT => IClient::DEFAULT_REQUEST_TIMEOUT,
59+
// Prefer HTTP/2 globally (PSR-7 request version)
60+
RequestOptions::VERSION => '2.0',
5961
];
62+
$defaults['curl'][\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2TLS;
6063

6164
$options['nextcloud']['allow_local_address'] = $this->isLocalAddressAllowed($options);
6265
if ($options['nextcloud']['allow_local_address'] === false) {
@@ -87,8 +90,15 @@ private function buildRequestOptions(array $options): array {
8790
$options[RequestOptions::HEADERS]['User-Agent'] = $userAgent;
8891
}
8992

90-
if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) {
91-
$options[RequestOptions::HEADERS]['Accept-Encoding'] = 'gzip';
93+
// Ensure headers array exists and set Accept-Encoding only if not present
94+
$headers = $options[RequestOptions::HEADERS] ?? [];
95+
if (!isset($headers['Accept-Encoding'])) {
96+
$acceptEnc = 'gzip';
97+
if (function_exists('brotli_uncompress')) {
98+
$acceptEnc = 'br, ' . $acceptEnc;
99+
}
100+
$options[RequestOptions::HEADERS] = $headers; // ensure headers are present
101+
$options[RequestOptions::HEADERS]['Accept-Encoding'] = $acceptEnc;
92102
}
93103

94104
// Fallback for save_to

tests/lib/Http/Client/ClientTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,27 @@ private function setUpDefaultRequestOptions(): void {
284284
$this->serverVersion->method('getVersionString')
285285
->willReturn('123.45.6');
286286

287+
$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';
287288
$this->defaultRequestOptions = [
288289
'verify' => '/my/path.crt',
289290
'proxy' => [
290291
'http' => 'foo',
291292
'https' => 'foo'
292293
],
293294
'headers' => [
295+
294296
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
295-
'Accept-Encoding' => 'gzip',
297+
'Accept-Encoding' => $acceptEnc,
298+
296299
],
297300
'timeout' => 30,
298301
'nextcloud' => [
299302
'allow_local_address' => true,
300303
],
304+
'version' => '2.0',
305+
'curl' => [
306+
\CURLOPT_HTTP_VERSION => \CURL_HTTP_VERSION_2TLS,
307+
],
301308
];
302309
}
303310

@@ -481,11 +488,13 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
481488
$this->serverVersion->method('getVersionString')
482489
->willReturn('123.45.6');
483490

491+
$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';
492+
484493
$this->assertEquals([
485494
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
486495
'headers' => [
487496
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
488-
'Accept-Encoding' => 'gzip',
497+
'Accept-Encoding' => $acceptEnc,
489498
],
490499
'timeout' => 30,
491500
'nextcloud' => [
@@ -499,6 +508,10 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
499508
): void {
500509
},
501510
],
511+
'version' => '2.0',
512+
'curl' => [
513+
\CURLOPT_HTTP_VERSION => \CURL_HTTP_VERSION_2TLS,
514+
],
502515
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
503516
}
504517

@@ -531,6 +544,8 @@ public function testSetDefaultOptionsWithProxy(): void {
531544
$this->serverVersion->method('getVersionString')
532545
->willReturn('123.45.6');
533546

547+
$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';
548+
534549
$this->assertEquals([
535550
'verify' => '/my/path.crt',
536551
'proxy' => [
@@ -539,7 +554,7 @@ public function testSetDefaultOptionsWithProxy(): void {
539554
],
540555
'headers' => [
541556
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
542-
'Accept-Encoding' => 'gzip',
557+
'Accept-Encoding' => $acceptEnc,
543558
],
544559
'timeout' => 30,
545560
'nextcloud' => [
@@ -553,6 +568,10 @@ public function testSetDefaultOptionsWithProxy(): void {
553568
): void {
554569
},
555570
],
571+
'version' => '2.0',
572+
'curl' => [
573+
\CURLOPT_HTTP_VERSION => \CURL_HTTP_VERSION_2TLS,
574+
],
556575
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
557576
}
558577

@@ -585,6 +604,8 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
585604
$this->serverVersion->method('getVersionString')
586605
->willReturn('123.45.6');
587606

607+
$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';
608+
588609
$this->assertEquals([
589610
'verify' => '/my/path.crt',
590611
'proxy' => [
@@ -594,7 +615,7 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
594615
],
595616
'headers' => [
596617
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
597-
'Accept-Encoding' => 'gzip',
618+
'Accept-Encoding' => $acceptEnc,
598619
],
599620
'timeout' => 30,
600621
'nextcloud' => [
@@ -608,6 +629,10 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
608629
): void {
609630
},
610631
],
632+
'version' => '2.0',
633+
'curl' => [
634+
\CURLOPT_HTTP_VERSION => \CURL_HTTP_VERSION_2TLS,
635+
],
611636
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
612637
}
613638
}

0 commit comments

Comments
 (0)