Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions lib/private/Http/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ private function buildRequestOptions(array $options): array {
$defaults = [
RequestOptions::VERIFY => $this->getCertBundle(),
RequestOptions::TIMEOUT => IClient::DEFAULT_REQUEST_TIMEOUT,
// Prefer HTTP/2 globally (PSR-7 request version)
RequestOptions::VERSION => '2.0',
];
// cURL hint: Prefer HTTP/2 (with ALPN); automatically falls back to 1.1.
$defaults['curl'][\CURLOPT_HTTP_VERSION]
= \defined('CURL_HTTP_VERSION_2TLS') ? \CURL_HTTP_VERSION_2TLS
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0
: \CURL_HTTP_VERSION_NONE);

$options['nextcloud']['allow_local_address'] = $this->isLocalAddressAllowed($options);
if ($options['nextcloud']['allow_local_address'] === false) {
Expand Down Expand Up @@ -84,8 +91,15 @@ private function buildRequestOptions(array $options): array {
$options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
}

if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) {
$options[RequestOptions::HEADERS]['Accept-Encoding'] = 'gzip';
// Ensure headers array exists and set Accept-Encoding only if not present
$headers = $options[RequestOptions::HEADERS] ?? [];
if (!isset($headers['Accept-Encoding'])) {
$acceptEnc = 'gzip';
if (function_exists('brotli_uncompress')) {
$acceptEnc = 'br, ' . $acceptEnc;
}
$options[RequestOptions::HEADERS] = $headers; // ensure headers are present
$options[RequestOptions::HEADERS]['Accept-Encoding'] = $acceptEnc;
}

// Fallback for save_to
Expand Down
52 changes: 48 additions & 4 deletions tests/lib/Http/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ private function setUpDefaultRequestOptions(): void {
->with()
->willReturn('/my/path.crt');

$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';

// compute curl http version hint like in production code
$curlVersion = \defined('CURL_HTTP_VERSION_2TLS')
? \CURL_HTTP_VERSION_2TLS
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0 : \CURL_HTTP_VERSION_NONE);

$this->defaultRequestOptions = [
'verify' => '/my/path.crt',
'proxy' => [
Expand All @@ -284,12 +291,16 @@ private function setUpDefaultRequestOptions(): void {
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
'Accept-Encoding' => $acceptEnc,
],
'timeout' => 30,
'nextcloud' => [
'allow_local_address' => true,
],
'version' => '2.0',
'curl' => [
\CURLOPT_HTTP_VERSION => $curlVersion,
],
];
}

Expand Down Expand Up @@ -466,11 +477,18 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
->expects($this->never())
->method('listCertificates');

$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';

// compute curl http version hint like in production code
$curlVersion = \defined('CURL_HTTP_VERSION_2TLS')
? \CURL_HTTP_VERSION_2TLS
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0 : \CURL_HTTP_VERSION_NONE);

$this->assertEquals([
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
'Accept-Encoding' => $acceptEnc,
],
'timeout' => 30,
'nextcloud' => [
Expand All @@ -484,6 +502,10 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
): void {
},
],
'version' => '2.0',
'curl' => [
\CURLOPT_HTTP_VERSION => $curlVersion,
],
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
}

Expand Down Expand Up @@ -513,6 +535,13 @@ public function testSetDefaultOptionsWithProxy(): void {
->with()
->willReturn('/my/path.crt');

$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';

// compute curl http version hint like in production code
$curlVersion = \defined('CURL_HTTP_VERSION_2TLS')
? \CURL_HTTP_VERSION_2TLS
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0 : \CURL_HTTP_VERSION_NONE);

$this->assertEquals([
'verify' => '/my/path.crt',
'proxy' => [
Expand All @@ -521,7 +550,7 @@ public function testSetDefaultOptionsWithProxy(): void {
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
'Accept-Encoding' => $acceptEnc,
],
'timeout' => 30,
'nextcloud' => [
Expand All @@ -535,6 +564,10 @@ public function testSetDefaultOptionsWithProxy(): void {
): void {
},
],
'version' => '2.0',
'curl' => [
\CURLOPT_HTTP_VERSION => $curlVersion,
],
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
}

Expand Down Expand Up @@ -564,6 +597,13 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
->with()
->willReturn('/my/path.crt');

$acceptEnc = function_exists('brotli_uncompress') ? 'br, gzip' : 'gzip';

// compute curl http version hint like in production code
$curlVersion = \defined('CURL_HTTP_VERSION_2TLS')
? \CURL_HTTP_VERSION_2TLS
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0 : \CURL_HTTP_VERSION_NONE);

$this->assertEquals([
'verify' => '/my/path.crt',
'proxy' => [
Expand All @@ -573,7 +613,7 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'Accept-Encoding' => 'gzip',
'Accept-Encoding' => $acceptEnc,
],
'timeout' => 30,
'nextcloud' => [
Expand All @@ -587,6 +627,10 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
): void {
},
],
'version' => '2.0',
'curl' => [
\CURLOPT_HTTP_VERSION => $curlVersion,
],
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
}
}
Loading