Switch to cronet as http backend

This commit is contained in:
Ming Ming 2024-07-31 01:34:06 +08:00
parent fffe8f7d2b
commit 4d5c811c9e
15 changed files with 128 additions and 3 deletions

View file

@ -46,6 +46,7 @@ import 'package:nc_photos/session_storage.dart';
import 'package:nc_photos/touch_manager.dart'; import 'package:nc_photos/touch_manager.dart';
import 'package:np_db/np_db.dart'; import 'package:np_db/np_db.dart';
import 'package:np_gps_map/np_gps_map.dart'; import 'package:np_gps_map/np_gps_map.dart';
import 'package:np_http/np_http.dart';
import 'package:np_log/np_log.dart' as np_log; import 'package:np_log/np_log.dart' as np_log;
import 'package:np_platform_util/np_platform_util.dart'; import 'package:np_platform_util/np_platform_util.dart';
import 'package:visibility_detector/visibility_detector.dart'; import 'package:visibility_detector/visibility_detector.dart';
@ -70,6 +71,7 @@ Future<void> init(InitIsolateType isolateType) async {
await _initPref(); await _initPref();
await _initAccountPrefs(); await _initAccountPrefs();
_initEquatable(); _initEquatable();
await initHttp(k.versionStr);
if (features.isSupportSelfSignedCert) { if (features.isSupportSelfSignedCert) {
await _initSelfSignedCertManager(); await _initSelfSignedCertManager();
} }

View file

@ -1,6 +1,7 @@
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
// ignore: implementation_imports // ignore: implementation_imports
import 'package:flutter_cache_manager/src/cache_store.dart'; import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:np_http/np_http.dart';
class CancelableGetFile { class CancelableGetFile {
CancelableGetFile(this.store); CancelableGetFile(this.store);
@ -41,6 +42,7 @@ class ThumbnailCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 50000, maxNrOfCacheObjects: 50000,
fileService: HttpFileService(httpClient: makeHttpClient()),
), ),
); );
} }
@ -58,6 +60,7 @@ class LargeImageCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 1000, maxNrOfCacheObjects: 1000,
fileService: HttpFileService(httpClient: makeHttpClient()),
), ),
); );
} }
@ -73,6 +76,7 @@ class CoverCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 300, maxNrOfCacheObjects: 300,
fileService: HttpFileService(httpClient: makeHttpClient()),
), ),
); );
} }

View file

@ -7,6 +7,7 @@ import 'package:nc_photos/exception.dart';
import 'package:nc_photos/platform/download.dart' as itf; import 'package:nc_photos/platform/download.dart' as itf;
import 'package:nc_photos_plugin/nc_photos_plugin.dart'; import 'package:nc_photos_plugin/nc_photos_plugin.dart';
import 'package:np_codegen/np_codegen.dart'; import 'package:np_codegen/np_codegen.dart';
import 'package:np_http/np_http.dart';
import 'package:np_platform_util/np_platform_util.dart'; import 'package:np_platform_util/np_platform_util.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@ -65,7 +66,7 @@ class _AndroidDownload extends itf.Download {
try { try {
final uri = Uri.parse(url); final uri = Uri.parse(url);
final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); final req = http.Request("GET", uri)..headers.addAll(headers ?? {});
final response = await http.Client().send(req); final response = await makeHttpClient().send(req);
bool isEnd = false; bool isEnd = false;
Object? error; Object? error;
final size = response.contentLength; final size = response.contentLength;

View file

@ -2,6 +2,7 @@ import 'package:http/http.dart' as http;
import 'package:nc_photos/exception.dart'; import 'package:nc_photos/exception.dart';
import 'package:nc_photos/platform/download.dart' as itf; import 'package:nc_photos/platform/download.dart' as itf;
import 'package:nc_photos/web/file_saver.dart'; import 'package:nc_photos/web/file_saver.dart';
import 'package:np_http/np_http.dart';
class DownloadBuilder extends itf.DownloadBuilder { class DownloadBuilder extends itf.DownloadBuilder {
@override @override
@ -33,7 +34,7 @@ class _WebDownload extends itf.Download {
final uri = Uri.parse(url); final uri = Uri.parse(url);
final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); final req = http.Request("GET", uri)..headers.addAll(headers ?? {});
final response = final response =
await http.Response.fromStream(await http.Client().send(req)); await http.Response.fromStream(await makeHttpClient().send(req));
if (response.statusCode ~/ 100 != 2) { if (response.statusCode ~/ 100 != 2) {
throw DownloadException( throw DownloadException(
"Failed downloading $filename (HTTP ${response.statusCode})"); "Failed downloading $filename (HTTP ${response.statusCode})");

View file

@ -284,6 +284,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.6.3" version: "1.6.3"
cronet_http:
dependency: transitive
description:
name: cronet_http
sha256: "9b9f00ae48971bc8a8cbdd4528bd35511adce00fb79d1ebf9f9907667056640f"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
crypto: crypto:
dependency: "direct main" dependency: "direct main"
description: description:
@ -805,6 +813,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
jni:
dependency: transitive
description:
name: jni
sha256: "499558e919997adfc45809a66caf0b95b91393e23289dd2826b152f8f04e6611"
url: "https://pub.dev"
source: hosted
version: "0.7.3"
js: js:
dependency: transitive dependency: transitive
description: description:
@ -1114,6 +1130,13 @@ packages:
relative: true relative: true
source: path source: path
version: "0.0.1" version: "0.0.1"
np_http:
dependency: "direct main"
description:
path: "../np_http"
relative: true
source: path
version: "1.0.0"
np_lints: np_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:

View file

@ -110,6 +110,8 @@ dependencies:
path: ../np_geocoder path: ../np_geocoder
np_gps_map: np_gps_map:
path: ../np_gps_map path: ../np_gps_map
np_http:
path: ../np_http
np_log: np_log:
path: ../np_log path: ../np_log
np_math: np_math:

View file

@ -4,6 +4,7 @@ import 'package:logging/logging.dart';
import 'package:np_api/src/type.dart'; import 'package:np_api/src/type.dart';
import 'package:np_api/src/util.dart'; import 'package:np_api/src/util.dart';
import 'package:np_codegen/np_codegen.dart'; import 'package:np_codegen/np_codegen.dart';
import 'package:np_http/np_http.dart';
import 'package:xml/xml.dart'; import 'package:xml/xml.dart';
part 'api.g.dart'; part 'api.g.dart';
@ -65,7 +66,7 @@ class Api {
} }
_log.finer(req.url); _log.finer(req.url);
final response = final response =
await http.Response.fromStream(await http.Client().send(req)); await http.Response.fromStream(await makeHttpClient().send(req));
if (!isHttpStatusGood(response.statusCode)) { if (!isHttpStatusGood(response.statusCode)) {
if (response.statusCode == 404) { if (response.statusCode == 404) {
_log.severe( _log.severe(

View file

@ -18,6 +18,8 @@ dependencies:
path: ../codegen path: ../codegen
np_common: np_common:
path: ../np_common path: ../np_common
np_http:
path: ../np_http
np_log: np_log:
path: ../np_log path: ../np_log
np_string: np_string:

7
np_http/.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

3
np_http/lib/np_http.dart Normal file
View file

@ -0,0 +1,3 @@
library np_http;
export 'src/http.dart';

35
np_http/lib/src/http.dart Normal file
View file

@ -0,0 +1,35 @@
import 'package:cronet_http/cronet_http.dart';
import 'package:http/http.dart';
import 'package:logging/logging.dart';
import 'package:np_platform_util/np_platform_util.dart';
import 'http_stub.dart'
if (dart.library.js_interop) 'http_browser.dart'
if (dart.library.io) 'http_io.dart';
Future<void> initHttp(String appVersion) async {
_userAgent = "nc-photos $appVersion";
if (getRawPlatform() == NpPlatform.android) {
try {
_cronetEngine = CronetEngine.build(
enableHttp2: true,
userAgent: _userAgent,
);
} catch (e, stackTrace) {
_log.severe("Failed creating CronetEngine", e, stackTrace);
}
}
}
Client makeHttpClient() {
if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) {
return CronetClient.fromCronetEngine(_cronetEngine!);
} else {
return makeHttpClientImpl(userAgent: _userAgent);
}
}
late final String _userAgent;
CronetEngine? _cronetEngine;
final _log = Logger("np_http");

View file

@ -0,0 +1,8 @@
import 'package:http/browser_client.dart';
import 'package:http/http.dart';
Client makeHttpClientImpl({
required String userAgent,
}) {
return BrowserClient();
}

View file

@ -0,0 +1,10 @@
import 'dart:io';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
Client makeHttpClientImpl({
required String userAgent,
}) {
return IOClient(HttpClient()..userAgent = userAgent);
}

View file

@ -0,0 +1,7 @@
import 'package:http/http.dart';
Client makeHttpClientImpl({
required String userAgent,
}) {
throw UnsupportedError("Unsupported");
}

19
np_http/pubspec.yaml Normal file
View file

@ -0,0 +1,19 @@
name: np_http
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo
publish_to: none
environment:
sdk: ">=3.2.0 <4.0.0"
dependencies:
cronet_http: ^1.2.0
http: ^1.1.2
logging: ^1.2.0
np_platform_util:
path: ../np_platform_util
dev_dependencies:
np_lints:
path: ../np_lints