π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add adaptive HTTP2 Dio adapter
Neeraj Gupta95c2b0df06c4
diff --git a/mobile/apps/photos/lib/core/network/network.dart b/mobile/apps/photos/lib/core/network/network.dartindex 8fb2ba01c8..0c7281c6c6 100644--- a/mobile/apps/photos/lib/core/network/network.dart+++ b/mobile/apps/photos/lib/core/network/network.dart@@ -1,7 +1,11 @@import "dart:async";import 'dart:io';+import 'dart:typed_data';import 'package:dio/dio.dart';+import 'package:dio/io.dart';+import 'package:dio_http2_adapter/dio_http2_adapter.dart';+import 'package:logging/logging.dart';import 'package:package_info_plus/package_info_plus.dart';import "package:photos/core/configuration.dart";import "package:photos/core/event_bus.dart";@@ -10,17 +14,19 @@ import "package:photos/events/endpoint_updated_event.dart";import "package:ua_client_hints/ua_client_hints.dart";class NetworkClient {+ final _logger = Logger("NetworkClient");late Dio _dio;late Dio _enteDio;StreamSubscription<EndpointUpdatedEvent>? _endpointUpdatedSubscription;static const kConnectTimeout = 15;+ static const _connectTimeout = Duration(seconds: kConnectTimeout);Future<void> init(PackageInfo packageInfo) async {final String ua = await userAgent();final endpoint = Configuration.instance.getHttpEndpoint();_dio = Dio(BaseOptions(- connectTimeout: const Duration(seconds: kConnectTimeout),+ connectTimeout: _connectTimeout,headers: {HttpHeaders.userAgentHeader: ua,'X-Client-Version': packageInfo.version,@@ -31,7 +37,7 @@ class NetworkClient {_enteDio = Dio(BaseOptions(baseUrl: endpoint,- connectTimeout: const Duration(seconds: kConnectTimeout),+ connectTimeout: _connectTimeout,headers: {HttpHeaders.userAgentHeader: ua,'X-Client-Version': packageInfo.version,@@ -40,6 +46,9 @@ class NetworkClient {),);+ _dio.httpClientAdapter = _newAdaptiveHttpClientAdapter(_connectTimeout);+ _enteDio.httpClientAdapter = _newAdaptiveHttpClientAdapter(_connectTimeout);+_setupInterceptors(endpoint);if (_endpointUpdatedSubscription != null) {@@ -58,6 +67,33 @@ class NetworkClient {_enteDio.interceptors.add(EnteRequestInterceptor(endpoint));}+ HttpClientAdapter _newAdaptiveHttpClientAdapter(Duration connectTimeout) {+ final fallbackAdapter = IOHttpClientAdapter();+ final http2Adapter = Http2Adapter(+ ConnectionManager(+ handshakeTimout: connectTimeout,+ ),+ fallbackAdapter: fallbackAdapter,+ onNotSupported: (options, requestStream, cancelFuture, exception) {+ _logger.info(+ "HTTP/2 not supported for ${options.method} "+ "${_uriOrigin(options.uri)}; falling back to IO adapter",+ );+ return fallbackAdapter.fetch(options, requestStream, cancelFuture);+ },+ );+ return _AdaptiveHttpClientAdapter(+ http2Adapter: http2Adapter,+ fallbackAdapter: fallbackAdapter,+ logger: _logger,+ );+ }++ String _uriOrigin(Uri uri) {+ final port = uri.hasPort ? ":${uri.port}" : "";+ return "${uri.scheme}://${uri.host}$port";+ }+NetworkClient._privateConstructor();static NetworkClient instance = NetworkClient._privateConstructor();@@ -66,3 +102,102 @@ class NetworkClient {Dio get enteDio => _enteDio;}++class _AdaptiveHttpClientAdapter implements HttpClientAdapter {+ _AdaptiveHttpClientAdapter({+ required HttpClientAdapter http2Adapter,+ required HttpClientAdapter fallbackAdapter,+ required Logger logger,+ }) : _http2Adapter = http2Adapter,+ _fallbackAdapter = fallbackAdapter,+ _logger = logger;++ final HttpClientAdapter _http2Adapter;+ final HttpClientAdapter _fallbackAdapter;+ final Logger _logger;++ @override+ Future<ResponseBody> fetch(+ RequestOptions options,+ Stream<Uint8List>? requestStream,+ Future<void>? cancelFuture,+ ) {+ if (options.uri.scheme != "https") {+ return _fallbackAdapter.fetch(options, requestStream, cancelFuture);+ }+ // Keep explicit body send-timeout behavior on Dio's IO adapter. The app+ // does not set this globally, but per-request timeouts should not inherit+ // dio_http2_adapter's stream-close behavior after a timed-out body stream.+ if (_hasRequestBodySendTimeout(options, requestStream)) {+ return _fallbackAdapter.fetch(options, requestStream, cancelFuture);+ }+ // Keep cancellable body streams on Dio's IO adapter. dio_http2_adapter closes+ // the HTTP/2 outgoing stream on cancellation, which can race with active file+ // streams and surface stream-close errors instead of a clean Dio cancellation.+ if (_hasCancellableRequestBody(requestStream, cancelFuture)) {+ return _fallbackAdapter.fetch(options, requestStream, cancelFuture);+ }++ return _http2Adapter.fetch(options, requestStream, cancelFuture).catchError(+ (Object e, StackTrace s) {+ if (e is TimeoutException) {+ throw DioException.connectionTimeout(+ requestOptions: options,+ timeout: options.connectTimeout ?? Duration.zero,+ error: e,+ );+ }+ if (_isFallbackHandshakeError(e)) {+ _logger.info(+ "HTTP/2 TLS negotiation failed for ${options.method} "+ "${_uriOrigin(options.uri)}; falling back to IO adapter",+ );+ return _fallbackAdapter.fetch(options, requestStream, cancelFuture);+ }+ Error.throwWithStackTrace(e, s);+ },+ );+ }++ @override+ void close({bool force = false}) {+ _http2Adapter.close(force: force);+ _fallbackAdapter.close(force: force);+ }++ bool _isFallbackHandshakeError(Object e) {+ if (e is HandshakeException) {+ return _isNoApplicationProtocolError(e);+ }+ if (e is DioException && e.error is HandshakeException) {+ return _isNoApplicationProtocolError(e.error! as HandshakeException);+ }+ return false;+ }++ bool _isNoApplicationProtocolError(HandshakeException e) {+ final message = "${e.message} $e".toLowerCase();+ return message.contains("no_application_protocol") ||+ message.contains("no application protocol");+ }++ bool _hasRequestBodySendTimeout(+ RequestOptions options,+ Stream<Uint8List>? requestStream,+ ) {+ final sendTimeout = options.sendTimeout ?? Duration.zero;+ return requestStream != null && sendTimeout > Duration.zero;+ }++ bool _hasCancellableRequestBody(+ Stream<Uint8List>? requestStream,+ Future<void>? cancelFuture,+ ) {+ return requestStream != null && cancelFuture != null;+ }++ String _uriOrigin(Uri uri) {+ final port = uri.hasPort ? ":${uri.port}" : "";+ return "${uri.scheme}://${uri.host}$port";+ }+}diff --git a/mobile/apps/photos/plugins/ente_cast/pubspec.lock b/mobile/apps/photos/plugins/ente_cast/pubspec.lockindex b4e04d42a6..4dbff14821 100644--- a/mobile/apps/photos/plugins/ente_cast/pubspec.lock+++ b/mobile/apps/photos/plugins/ente_cast/pubspec.lock@@ -38,10 +38,10 @@ packages:dependency: "direct main"description:name: dio- sha256: "253a18bbd4851fecba42f7343a1df3a9a4c1d31a2c1b37e221086b4fa8c8dbc9"+ sha256: aff32c08f92787a557dd5c0145ac91536481831a01b4648136373cddb0e64f8curl: "https://pub.dev"source: hosted- version: "5.8.0+1"+ version: "5.9.2"dio_web_adapter:dependency: transitivedescription:@@ -132,6 +132,14 @@ packages:url: "https://pub.dev"source: hostedversion: "1.16.0"+ mime:+ dependency: transitive+ description:+ name: mime+ sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6"+ url: "https://pub.dev"+ source: hosted+ version: "2.0.0"multicast_dns:dependency: transitivedescription:diff --git a/mobile/apps/photos/plugins/ente_cast/pubspec.yaml b/mobile/apps/photos/plugins/ente_cast/pubspec.yamlindex 853c1acc55..c3241ab1f2 100644--- a/mobile/apps/photos/plugins/ente_cast/pubspec.yaml+++ b/mobile/apps/photos/plugins/ente_cast/pubspec.yaml@@ -11,7 +11,7 @@ dependencies:url: https://github.com/guyluz11/flutter_cast.gitref: af6378574352884beab6cddec462c7fdfc9a8c35collection: 1.19.1- dio: 5.8.0+1+ dio: 5.9.2flutter:sdk: fluttershared_preferences: 2.5.3diff --git a/mobile/apps/photos/plugins/ente_feature_flag/pubspec.lock b/mobile/apps/photos/plugins/ente_feature_flag/pubspec.lockindex 49a6dd97c1..3506097c92 100644--- a/mobile/apps/photos/plugins/ente_feature_flag/pubspec.lock+++ b/mobile/apps/photos/plugins/ente_feature_flag/pubspec.lock@@ -29,10 +29,10 @@ packages:dependency: "direct main"description:name: dio- sha256: "253a18bbd4851fecba42f7343a1df3a9a4c1d31a2c1b37e221086b4fa8c8dbc9"+ sha256: aff32c08f92787a557dd5c0145ac91536481831a01b4648136373cddb0e64f8curl: "https://pub.dev"source: hosted- version: "5.8.0+1"+ version: "5.9.2"dio_web_adapter:dependency: transitivedescription:@@ -107,6 +107,14 @@ packages:url: "https://pub.dev"source: hostedversion: "1.16.0"+ mime:+ dependency: transitive+ description:+ name: mime+ sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6"+ url: "https://pub.dev"+ source: hosted+ version: "2.0.0"path:dependency: transitivedescription:diff --git a/mobile/apps/photos/plugins/ente_feature_flag/pubspec.yaml b/mobile/apps/photos/plugins/ente_feature_flag/pubspec.yamlindex 07d474dab0..d33372e177 100644--- a/mobile/apps/photos/plugins/ente_feature_flag/pubspec.yaml+++ b/mobile/apps/photos/plugins/ente_feature_flag/pubspec.yaml@@ -7,7 +7,7 @@ environment:dependencies:collection: 1.19.1- dio: 5.8.0+1+ dio: 5.9.2flutter:sdk: fluttershared_preferences: 2.5.3diff --git a/mobile/apps/photos/pubspec.lock b/mobile/apps/photos/pubspec.lockindex 61d2a9d171..7579643b09 100644--- a/mobile/apps/photos/pubspec.lock+++ b/mobile/apps/photos/pubspec.lock@@ -527,10 +527,18 @@ packages:dependency: "direct main"description:name: dio- sha256: "253a18bbd4851fecba42f7343a1df3a9a4c1d31a2c1b37e221086b4fa8c8dbc9"+ sha256: aff32c08f92787a557dd5c0145ac91536481831a01b4648136373cddb0e64f8curl: "https://pub.dev"source: hosted- version: "5.8.0+1"+ version: "5.9.2"+ dio_http2_adapter:+ dependency: "direct main"+ description:+ name: dio_http2_adapter+ sha256: "79f3d69b155b92a786c8734bd11860390b986210d4e07cbb6a5c8c806a7187b2"+ url: "https://pub.dev"+ source: hosted+ version: "2.7.0"dio_web_adapter:dependency: transitivedescription:@@ -1330,6 +1338,14 @@ packages:url: "https://pub.dev"source: hostedversion: "1.6.0"+ http2:+ dependency: transitive+ description:+ name: http2+ sha256: "382d3aefc5bd6dc68c6b892d7664f29b5beb3251611ae946a98d35158a82bbfa"+ url: "https://pub.dev"+ source: hosted+ version: "2.3.1"http_client_helper:dependency: transitivedescription:diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yamlindex a7ddc00486..2b3411ab46 100644--- a/mobile/apps/photos/pubspec.yaml+++ b/mobile/apps/photos/pubspec.yaml@@ -48,7 +48,8 @@ dependencies:dart_ui_isolate: 1.1.1defer_pointer: 0.0.2device_info_plus: 11.3.0- dio: 5.8.0+1+ dio: 5.9.2+ dio_http2_adapter: 2.7.0dots_indicator: 4.0.1dotted_border: 2.1.0dropdown_button2: 2.3.9diff --git a/mobile/apps/photos/scripts/internal_changes.txt b/mobile/apps/photos/scripts/internal_changes.txtindex 07d9964c80..8fb4675482 100644--- a/mobile/apps/photos/scripts/internal_changes.txt+++ b/mobile/apps/photos/scripts/internal_changes.txt@@ -1,4 +1,5 @@Latest changes:+- Neeraj: Add HTTP/2 support with HTTP/1.1 fallback for Photos API requests- Amrithesh: Improve zoomed OCR selection UX in the photo viewer- Neeraj: Fix dedupe cleanup progress percentage display- Neeraj: Fix Added by visibility in file details