Skip to content

Commit 812f12e

Browse files
committed
perf(client): enable HTTP/2 and brotli support in internal HTTP client
- Prefer HTTP/2 by setting RequestOptions::VERSION => "2.0" so clients that respect PSR-7 request version will prefer HTTP/2. - Add a curl hint (CURLOPT_HTTP_VERSION) to prefer HTTP/2 via ALPN (CURL_HTTP_VERSION_2TLS or CURL_HTTP_VERSION_2_0 fallback) while allowing automatic fallback to HTTP/1.1. - Advertise Brotli ("br") in Accept-Encoding when the php-brotli extension is available (detected via function_exists('brotli_uncompress')), otherwise fall back to gzip. Notes: - The PSR-7 request version is used as a hint for HTTP client libraries; setting the version to "2.0" signals a preference for HTTP/2 at the request abstraction level. - The curl option is defensive: it prefers HTTP/2 where libcurl supports it (via ALPN), but will not break on older libcurl/builds (uses defined()). Compatibility: - Fully backwards compatible: if the php-brotli extension is not present, no Brotli usage will occur and behaviour remains equivalent to previous (gzip). Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
1 parent 3a687ea commit 812f12e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/private/Http/Client/Client.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ private function buildRequestOptions(array $options): array {
5454
$defaults = [
5555
RequestOptions::VERIFY => $this->getCertBundle(),
5656
RequestOptions::TIMEOUT => IClient::DEFAULT_REQUEST_TIMEOUT,
57+
// Prefer HTTP/2 globally (PSR-7 request version)
58+
RequestOptions::VERSION => '2.0',
5759
];
60+
// cURL hint: Prefer HTTP/2 (with ALPN); automatically falls back to 1.1.
61+
$defaults['curl'][\CURLOPT_HTTP_VERSION]
62+
= \defined('CURL_HTTP_VERSION_2TLS') ? \CURL_HTTP_VERSION_2TLS
63+
: (\defined('CURL_HTTP_VERSION_2_0') ? \CURL_HTTP_VERSION_2_0
64+
: \CURL_HTTP_VERSION_NONE);
5865

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

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

91105
// Fallback for save_to

0 commit comments

Comments
 (0)