From eb752e57a297c8cb9c9f3243f4dc3969ec7f19bb Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Wed, 31 Jul 2024 22:32:29 +0800 Subject: [PATCH] Switch to cupertino_http as http backend on ios --- app/pubspec.lock | 12 ++++++++++-- np_http/lib/src/http.dart | 11 +++++++++++ np_http/pubspec.yaml | 1 + np_platform_util/lib/src/env.dart | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/pubspec.lock b/app/pubspec.lock index a92b38a3..b7f78b09 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -308,6 +308,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.17.3" + cupertino_http: + dependency: transitive + description: + name: cupertino_http + sha256: "0e3ed481280ad41d42d4881d4b6f89c083c17aa010205c9381ecb4c7ea7bdf1f" + url: "https://pub.dev" + source: hosted + version: "1.3.0" dart_style: dependency: transitive description: @@ -418,10 +426,10 @@ packages: dependency: transitive description: name: ffi - sha256: ed5337a5660c506388a9f012be0288fb38b49020ce2b45fe1f8b8323fe429f99 + sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878" url: "https://pub.dev" source: hosted - version: "2.0.2" + version: "2.1.0" file: dependency: transitive description: diff --git a/np_http/lib/src/http.dart b/np_http/lib/src/http.dart index aaebed36..530137a3 100644 --- a/np_http/lib/src/http.dart +++ b/np_http/lib/src/http.dart @@ -1,4 +1,5 @@ import 'package:cronet_http/cronet_http.dart'; +import 'package:cupertino_http/cupertino_http.dart'; import 'package:http/http.dart'; import 'package:logging/logging.dart'; import 'package:np_platform_util/np_platform_util.dart'; @@ -18,12 +19,21 @@ Future initHttp(String appVersion) async { } catch (e, stackTrace) { _log.severe("Failed creating CronetEngine", e, stackTrace); } + } else if (getRawPlatform().isApple) { + try { + _urlConfig = URLSessionConfiguration.ephemeralSessionConfiguration() + ..httpAdditionalHeaders = {"User-Agent": _userAgent}; + } catch (e, stackTrace) { + _log.severe("Failed creating URLSessionConfiguration", e, stackTrace); + } } } Client makeHttpClient() { if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) { return CronetClient.fromCronetEngine(_cronetEngine!); + } else if (getRawPlatform().isApple && _urlConfig != null) { + return CupertinoClient.fromSessionConfiguration(_urlConfig!); } else { return makeHttpClientImpl(userAgent: _userAgent); } @@ -31,5 +41,6 @@ Client makeHttpClient() { late final String _userAgent; CronetEngine? _cronetEngine; +URLSessionConfiguration? _urlConfig; final _log = Logger("np_http"); diff --git a/np_http/pubspec.yaml b/np_http/pubspec.yaml index a20c5ee1..d5128a4f 100644 --- a/np_http/pubspec.yaml +++ b/np_http/pubspec.yaml @@ -9,6 +9,7 @@ environment: dependencies: cronet_http: ^1.2.0 + cupertino_http: ^1.3.0 http: ^1.1.2 logging: ^1.2.0 np_platform_util: diff --git a/np_platform_util/lib/src/env.dart b/np_platform_util/lib/src/env.dart index 71ffab97..3e5fb9d9 100644 --- a/np_platform_util/lib/src/env.dart +++ b/np_platform_util/lib/src/env.dart @@ -16,6 +16,7 @@ enum NpPlatform { bool get isMobile => this == android || this == iOs; bool get isDesktop => this == fuchsia || this == linux || this == macOs || this == windows; + bool get isApple => this == iOs || this == macOs; } /// Get the current running platform