Skip to content

Commit

Permalink
Some minor readability fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Sep 7, 2023
1 parent 07b82c6 commit d64190d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pkgs/cronet_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.0

* Switch to using `package:jnigen` for bindings to Cronet
* Support for running in background isolates.

## 0.2.2

* Require Dart 3.0
Expand Down
31 changes: 16 additions & 15 deletions pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ class CronetEngine {
JObject.fromRef(Jni.getCachedApplicationContext()));

try {
if (storagePath != null) {
builder.setStoragePath(storagePath.toJString());
}

if (cacheMode == CacheMode.disabled) {
builder.enableHttpCache(0, 0); // HTTP_CACHE_DISABLED, 0 bytes
} else if (cacheMode != null && cacheMaxSize != null) {
Expand All @@ -108,6 +104,10 @@ class CronetEngine {
builder.enableQuic(enableQuic);
}

if (storagePath != null) {
builder.setStoragePath(storagePath.toJString());
}

if (userAgent != null) {
builder.setUserAgent(userAgent.toJString());
}
Expand Down Expand Up @@ -228,9 +228,10 @@ class CronetClient extends BaseClient {
final cronetCallbacks =
jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface.implement(
jb.$UrlRequestCallbackProxy_UrlRequestCallbackInterfaceImpl(
onResponseStarted: (urlRequest, info) {
onResponseStarted: (urlRequest, responseInfo) {
responseStream = StreamController();
final responseHeaders = _cronetToClientHeaders(info.getAllHeaders());
final responseHeaders =
_cronetToClientHeaders(responseInfo.getAllHeaders());
int? contentLength;

switch (responseHeaders['content-length']) {
Expand All @@ -247,27 +248,27 @@ class CronetClient extends BaseClient {
}
responseCompleter.complete(StreamedResponse(
responseStream!.stream,
info.getHttpStatusCode(),
responseInfo.getHttpStatusCode(),
contentLength: contentLength,
reasonPhrase: info.getHttpStatusText().toString(),
reasonPhrase: responseInfo.getHttpStatusText().toString(),
request: request,
isRedirect: false,
headers: responseHeaders,
));

urlRequest.read(jb.ByteBuffer.allocateDirect(1024 * 1024));
},
onRedirectReceived: (urlRequest, info, newLocationUrl) {
onRedirectReceived: (urlRequest, responseInfo, newLocationUrl) {
if (!request.followRedirects) {
cronetRequest.cancel();
responseCompleter.complete(StreamedResponse(
const Stream.empty(), // Cronet provides no body for redirects.
info.getHttpStatusCode(),
responseInfo.getHttpStatusCode(),
contentLength: 0,
reasonPhrase: info.getHttpStatusText().toString(),
reasonPhrase: responseInfo.getHttpStatusText().toString(),
request: request,
isRedirect: true,
headers: _cronetToClientHeaders(info.getAllHeaders())));
headers: _cronetToClientHeaders(responseInfo.getAllHeaders())));
}
++numRedirects;
if (numRedirects <= request.maxRedirects) {
Expand All @@ -278,7 +279,7 @@ class CronetClient extends BaseClient {
ClientException('Redirect limit exceeded', request.url));
}
},
onReadCompleted: (urlRequest, urlResponseInfo, byteBuffer) {
onReadCompleted: (urlRequest, responseInfo, byteBuffer) {
byteBuffer.flip();
final data = Uint8List(byteBuffer.remaining());
for (var i = 0; i < byteBuffer.remaining(); ++i) {
Expand All @@ -288,10 +289,10 @@ class CronetClient extends BaseClient {
byteBuffer.clear();
cronetRequest.read(byteBuffer);
},
onSucceeded: (urlRequest, urlResponseInfo) {
onSucceeded: (urlRequest, responseInfo) {
responseStream!.sink.close();
},
onFailed: (urlRequest, urlResponseInfo, cronetException) {
onFailed: (urlRequest, responseInfo, cronetException) {
final error = ClientException(
'Cronet exception: ${cronetException.toString()}', request.url);
if (responseStream == null) {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cronet_http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: cronet_http
description: >
An Android Flutter plugin that provides access to the Cronet HTTP client.
version: 0.2.2
version: 0.5.0
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http

environment:
Expand Down

0 comments on commit d64190d

Please sign in to comment.