Skip to content

Commit

Permalink
Merge pull request #38 from andrei1489/master
Browse files Browse the repository at this point in the history
* fixed issue with size being 0 and missing payload
  • Loading branch information
brianegan authored Mar 27, 2020
2 parents 72cef7b + 8aeea33 commit 1787dd1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public String method() {
@Override
public byte[] body() throws IOException {
if (map.get("body") != null) {
byte[] bytes = new byte[]{};
List<Integer> body = ((List<Integer>) map.get("body"));
byte[] bytes = new byte[body.size()];
for (int i = 0; i < body.size(); i++) {
bytes[i] = body.get(i).byteValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void onMethodCall(final MethodCall call, Result result) {
case "interpretResponseStream":
interpretResponseStream(((String) call.arguments));
break;
case "onData":
onData((Map<String, Object>) call.arguments);
case "onDataReceived":
onDataReceived((Map<String, Object>) call.arguments);
break;
case "onDone":
onDone((String) call.arguments);
Expand Down Expand Up @@ -97,7 +97,7 @@ private void onDone(String id) {
}
}

private void onData(Map<String, Object> arguments) {
private void onDataReceived(Map<String, Object> arguments) {
final String dataId = ((String) arguments.get("id"));
final byte[] data = ((byte[]) arguments.get("data"));
final LinkedBlockingQueue<QueueItem> queue = queues.get(dataId);
Expand All @@ -106,6 +106,7 @@ private void onData(Map<String, Object> arguments) {
} catch (InterruptedException e) {
e.printStackTrace();
}
mEventReporter.dataReceived(dataId,data.length,data.length);
}

private void responseHeadersReceived(Map<String, Object> arguments) {
Expand Down
43 changes: 31 additions & 12 deletions lib/src/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,43 @@ class StethoHttpClient implements HttpClient {
int port,
String path,
) async {
return _wrapResponse(await client.open(method, host, port, path));
Uri uri = Uri(host: host,port: port, path: path);
return await openUrl(method, uri);
}

@override
Future<HttpClientRequest> openUrl(String method, Uri url) async {
return client.openUrl(method, url).then((request) {
final wrapped = _wrapResponse(request);

scheduleMicrotask(() {
MethodChannelController.requestWillBeSent(
new FlutterStethoInspectorRequest(
url: request.uri.toString(),
headers: headersToMap(request.headers),
method: request.method,
id: wrapped.id,
),
);
});
List<int> body = [];
if (method.toLowerCase() != 'post' && method.toLowerCase() != 'put'){
scheduleMicrotask(() {
MethodChannelController.requestWillBeSent(
new FlutterStethoInspectorRequest(
url: request.uri.toString(),
headers: headersToMap(request.headers),
method: request.method,
id: wrapped.id,
body: body,
),
);
});
} else {
wrapped.stream.listen((onData) {
body.addAll(onData);
scheduleMicrotask(() {
MethodChannelController.requestWillBeSent(
new FlutterStethoInspectorRequest(
url: request.uri.toString(),
headers: headersToMap(request.headers),
method: request.method,
id: wrapped.id,
body: body,
),
);
});
});
}

return wrapped;
});
Expand Down
16 changes: 14 additions & 2 deletions lib/src/http_client_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:flutter_stetho/src/utils.dart';
class StethoHttpClientRequest implements HttpClientRequest {
final HttpClientRequest request;
final String id;
final StreamController<List<int>> _streamController = StreamController.broadcast();
Stream get stream => _streamController.stream.asBroadcastStream();

StethoHttpClientRequest(
this.request,
Expand All @@ -18,6 +20,7 @@ class StethoHttpClientRequest implements HttpClientRequest {

@override
void add(List<int> data) {
_streamController.add(data);
request.add(data);
}

Expand All @@ -28,13 +31,14 @@ class StethoHttpClientRequest implements HttpClientRequest {

@override
Future addStream(Stream<List<int>> stream) {
return request.addStream(stream);
var newStream = stream.asBroadcastStream();
newStream.listen((onData)=> _streamController.add(onData));
return request.addStream(newStream);
}

@override
Future<HttpClientResponse> close() async {
final response = await request.close();

MethodChannelController.responseHeadersReceived(
new FlutterStethoInspectorResponse(
url: request.uri.toString(),
Expand Down Expand Up @@ -122,15 +126,23 @@ class StethoHttpClientRequest implements HttpClientRequest {
@override
void writeAll(Iterable objects, [String separator = ""]) {
request.writeAll(objects, separator);
String data = objects.map((object) => object.toString()).join(separator);
_streamController.add(data.codeUnits);
}

@override
void writeCharCode(int charCode) {
request.writeCharCode(charCode);
_streamController.add([charCode]);
}

@override
void writeln([Object obj = ""]) {
request.writeln(obj);
if (obj is String){
_streamController.add(obj.codeUnits);
} else {
_streamController.add(obj.toString().codeUnits);
}
}
}
4 changes: 2 additions & 2 deletions lib/src/method_channel_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MethodChannelController {
static Future<dynamic> responseReadFailed(List<String> idError) =>
_channel.invokeMethod('responseReadFailed', idError);

static Future<dynamic> onData(Map<String, Object> map) =>
_channel.invokeMethod('onData', map);
static Future<dynamic> onDataReceived(Map<String, Object> map) =>
_channel.invokeMethod('onDataReceived', map);

static Future<dynamic> onDone(String id) =>
_channel.invokeMethod('onDone', id);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:flutter_stetho/src/method_channel_controller.dart';
StreamTransformer<List<int>, List<int>> createResponseTransformer(String id) {
return new StreamTransformer.fromHandlers(handleData: (data, sink) {
sink.add(data);
MethodChannelController.onData({"data": data, "id": id});
MethodChannelController.onDataReceived({"data": data, "id": id});
}, handleError: (error, stacktrace, sink) {
sink.addError(error, stacktrace);
MethodChannelController.responseReadFailed([id, error.toString()]);
Expand Down

0 comments on commit 1787dd1

Please sign in to comment.