Skip to content

Commit

Permalink
修复网络监控会影响非文本请求内容响应的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
[linjizong] committed Feb 20, 2021
1 parent 84b08f1 commit bf65325
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 130 deletions.
1 change: 1 addition & 0 deletions Flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## [0.2.13] - 修复网络监控会影响非文本请求内容响应的问题
## [0.2.12] - 修改页面层级,现在DoKitApp将以Stack存放用户传入的Widget,防止各种InheritedWidget影响到用户Widget
## [0.2.11] - 修复一些UI异常问题;日志模块增加错误信息过滤,防止被错误日志冲刷掉;
## [0.2.10] - 修复DoKitApp在某些case下获取高度为0的异常.
Expand Down
2 changes: 1 addition & 1 deletion Flutter/example/ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
310a81ea87d5214a76b45c32814582e1
d4d880dcbf7794a0d22fc1a36580d2b3
6 changes: 6 additions & 0 deletions Flutter/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PODS:
- Flutter (1.0.0)
- package_info (0.0.1):
- Flutter
- path_provider (0.0.1):
- Flutter
- path_provider_linux (0.0.1):
- Flutter
- path_provider_windows (0.0.1):
Expand All @@ -20,6 +22,7 @@ PODS:
DEPENDENCIES:
- Flutter (from `Flutter`)
- package_info (from `.symlinks/plugins/package_info/ios`)
- path_provider (from `.symlinks/plugins/path_provider/ios`)
- path_provider_linux (from `.symlinks/plugins/path_provider_linux/ios`)
- path_provider_windows (from `.symlinks/plugins/path_provider_windows/ios`)
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
Expand All @@ -33,6 +36,8 @@ EXTERNAL SOURCES:
:path: Flutter
package_info:
:path: ".symlinks/plugins/package_info/ios"
path_provider:
:path: ".symlinks/plugins/path_provider/ios"
path_provider_linux:
:path: ".symlinks/plugins/path_provider_linux/ios"
path_provider_windows:
Expand All @@ -51,6 +56,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
path_provider: fb74bd0465e96b594bb3b5088ee4a4e7bb1f2a9d
path_provider_linux: 4d630dc393e1f20364f3e3b4a2ff41d9674a84e4
path_provider_windows: a2b81600c677ac1959367280991971cb9a1edb3b
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
Expand Down
79 changes: 45 additions & 34 deletions Flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:dokit/dokit.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

void main() {
DoKit.runApp(
Expand Down Expand Up @@ -117,6 +119,20 @@ class _DoKitTestPageState extends State<DoKitTestPage> {
onPressed: mockHttpGet,
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
color: Color(0xffcccccc)),
margin: EdgeInsets.only(bottom: 30),
child: FlatButton(
child: Text('Test Download',
style: TextStyle(
color: Color(0xff000000),
fontSize: 18,
)),
onPressed: testDownload,
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
Expand Down Expand Up @@ -178,40 +194,35 @@ class _DoKitTestPageState extends State<DoKitTestPage> {

Timer timer;

// downloadFile(String url, {String filename}) async {
// var httpClient = http.Client();
// var request = new http.Request('GET', Uri.parse(url));
// var response = httpClient.send(request);
// String dir = '/test/';
//
// List<List<int>> chunks = new List();
// int downloaded = 0;
//
// response.asStream().listen((http.StreamedResponse r) {
//
// r.stream.listen((List<int> chunk) {
// // Display percentage of completion
// debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');
//
// chunks.add(chunk);
// downloaded += chunk.length;
// }, onDone: () async {
// // Display percentage of completion
// debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');
//
// // Save the file
// // File file = new File('$dir/$filename');
// // final Uint8List bytes = Uint8List(r.contentLength);
// // int offset = 0;
// // for (List<int> chunk in chunks) {
// // bytes.setRange(offset, offset + chunk.length, chunk);
// // offset += chunk.length;
// // }
// // await file.writeAsBytes(bytes);
// return;
// });
// });
// }
testDownload() async {
String url =
'https://pt-starfile.didistatic.com/static/starfile/node20210220/895f1e95e30aba5dd56d6f2ccf768b57/GjzGU0Pvv11613804530384.zip';
String savePath = await getPhoneLocalPath();
String zipName = 'test.zip';
Dio dio = new Dio();
print("$savePath/$zipName");
Response response = await dio.download(url, "$savePath/$zipName",
onReceiveProgress: (received, total) {
if (total != -1) {
// 当前下载的百分比
// print((received / total * 100).toStringAsFixed(0) + "%");
// print("received=$received total=$total");
if (received == total) {
print("下载完成 ✅ ");
}
} else {}
});
}

///获取手机的存储目录路径
///getExternalStorageDirectory() 获取的是 android 的外部存储 (External Storage)
/// getApplicationDocumentsDirectory 获取的是 ios 的Documents` or `Downloads` 目录
Future<String> getPhoneLocalPath() async {
final directory = Theme.of(context).platform == TargetPlatform.android
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
return directory.path;
}

void testMethodChannel() {
timer?.cancel();
Expand Down
4 changes: 4 additions & 0 deletions Flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ dependencies:
sdk: flutter
dokit:
path: ..
dio: 3.0.10
path_provider: 1.6.0


dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
51 changes: 26 additions & 25 deletions Flutter/lib/engine/dokit_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,21 @@ class DoKitHttpClientResponse implements HttpClientResponse {
@override
StreamSubscription<List<int>> listen(void Function(List<int> event) onData,
{Function onError, void Function() onDone, bool cancelOnError}) {
if (!isTextResponse()) {
recordResponse(
statusCode, "返回结果不支持解析", headers?.toString(), contentLength);
return origin.listen(onData,
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
}
void onDataWrapper(List<int> result) {
onData(result);
try {
if (isTextResponse()) {
if (getEncoding() != null) {
recordResponse(statusCode, getEncoding().decode(result),
headers?.toString(), contentLength);
} else {
recordResponse(
statusCode, "返回结果解析失败", headers?.toString(), contentLength);
}
if (getEncoding() != null) {
recordResponse(statusCode, getEncoding().decode(result),
headers?.toString(), contentLength);
} else {
recordResponse(
statusCode, "返回结果不支持解析", headers?.toString(), contentLength);
statusCode, "返回结果解析失败", headers?.toString(), contentLength);
}
} catch (e) {
recordResponse(
Expand Down Expand Up @@ -604,27 +605,27 @@ class DoKitHttpClientResponse implements HttpClientResponse {
@override
Stream<S> transform<S>(StreamTransformer<List<int>, S> streamTransformer) {
Stream s = origin.transform(streamTransformer);
if (!isTextResponse()) {
recordResponse(
statusCode, "返回结果不支持解析", headers?.toString(), contentLength);
return s;
}
s = s.asBroadcastStream();
s.listen((event) {
if (isTextResponse()) {
if (event is Uint8List) {
Uint8List result = event;
if (getEncoding() != null) {
recordResponse(statusCode, getEncoding().decode(result.toList()),
headers?.toString(), event.length);
} else {
recordResponse(
statusCode, "返回结果解析失败", headers?.toString(), contentLength);
}
} else if (event is String) {
recordResponse(statusCode, event, headers?.toString(), contentLength);
if (event is Uint8List) {
Uint8List result = event;
if (getEncoding() != null) {
recordResponse(statusCode, getEncoding().decode(result.toList()),
headers?.toString(), event.length);
} else {
recordResponse(statusCode, 'unknown type:${event.runtimeType}',
headers?.toString(), contentLength);
recordResponse(
statusCode, "返回结果解析失败", headers?.toString(), contentLength);
}
} else if (event is String) {
recordResponse(statusCode, event, headers?.toString(), contentLength);
} else {
recordResponse(
statusCode, "返回结果不支持解析", headers?.toString(), contentLength);
recordResponse(statusCode, 'unknown type:${event.runtimeType}',
headers?.toString(), contentLength);
}
});
return s;
Expand Down
Loading

0 comments on commit bf65325

Please sign in to comment.