Switch to cupertino_http as http backend on ios

This commit is contained in:
Ming Ming 2024-07-31 22:32:29 +08:00
parent 4d5c811c9e
commit eb752e57a2
4 changed files with 23 additions and 2 deletions

View file

@ -308,6 +308,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.17.3" 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: dart_style:
dependency: transitive dependency: transitive
description: description:
@ -418,10 +426,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: ffi name: ffi
sha256: ed5337a5660c506388a9f012be0288fb38b49020ce2b45fe1f8b8323fe429f99 sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.2" version: "2.1.0"
file: file:
dependency: transitive dependency: transitive
description: description:

View file

@ -1,4 +1,5 @@
import 'package:cronet_http/cronet_http.dart'; import 'package:cronet_http/cronet_http.dart';
import 'package:cupertino_http/cupertino_http.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:np_platform_util/np_platform_util.dart'; import 'package:np_platform_util/np_platform_util.dart';
@ -18,12 +19,21 @@ Future<void> initHttp(String appVersion) async {
} catch (e, stackTrace) { } catch (e, stackTrace) {
_log.severe("Failed creating CronetEngine", 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() { Client makeHttpClient() {
if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) { if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) {
return CronetClient.fromCronetEngine(_cronetEngine!); return CronetClient.fromCronetEngine(_cronetEngine!);
} else if (getRawPlatform().isApple && _urlConfig != null) {
return CupertinoClient.fromSessionConfiguration(_urlConfig!);
} else { } else {
return makeHttpClientImpl(userAgent: _userAgent); return makeHttpClientImpl(userAgent: _userAgent);
} }
@ -31,5 +41,6 @@ Client makeHttpClient() {
late final String _userAgent; late final String _userAgent;
CronetEngine? _cronetEngine; CronetEngine? _cronetEngine;
URLSessionConfiguration? _urlConfig;
final _log = Logger("np_http"); final _log = Logger("np_http");

View file

@ -9,6 +9,7 @@ environment:
dependencies: dependencies:
cronet_http: ^1.2.0 cronet_http: ^1.2.0
cupertino_http: ^1.3.0
http: ^1.1.2 http: ^1.1.2
logging: ^1.2.0 logging: ^1.2.0
np_platform_util: np_platform_util:

View file

@ -16,6 +16,7 @@ enum NpPlatform {
bool get isMobile => this == android || this == iOs; bool get isMobile => this == android || this == iOs;
bool get isDesktop => bool get isDesktop =>
this == fuchsia || this == linux || this == macOs || this == windows; this == fuchsia || this == linux || this == macOs || this == windows;
bool get isApple => this == iOs || this == macOs;
} }
/// Get the current running platform /// Get the current running platform