File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import 'package:async/async.dart';
20
20
import 'package:aws_common/aws_common.dart' ;
21
21
import 'package:http/http.dart' as http;
22
22
import 'package:meta/meta.dart' ;
23
+ import 'package:stream_transform/stream_transform.dart' ;
23
24
24
25
/// {@template aws_common.aws_http_request}
25
26
/// A parameterized HTTP request.
@@ -109,18 +110,30 @@ abstract class AWSBaseHttpRequest implements Closeable {
109
110
/// If [client] is not provided, a short-lived one is created for this request.
110
111
Future <AWSStreamedHttpResponse > send ([http.Client ? client]) async {
111
112
final useClient = client ?? http.Client ();
113
+
114
+ // Closes the HTTP client, but only if we created it.
115
+ void closeClient () {
116
+ if (client == null ) {
117
+ useClient.close ();
118
+ }
119
+ }
120
+
112
121
try {
113
122
final resp = await useClient.send (httpRequest);
114
123
return AWSStreamedHttpResponse (
115
124
headers: resp.headers,
116
125
statusCode: resp.statusCode,
117
- body: resp.stream,
126
+ body: resp.stream.tap (
127
+ null ,
128
+ // Wait until the body has been read before closing the client,
129
+ // since chunked requests require that the client not be closed
130
+ // until they're complete.
131
+ onDone: closeClient,
132
+ ),
118
133
);
119
- } finally {
120
- // Only close a client we created.
121
- if (client == null ) {
122
- useClient.close ();
123
- }
134
+ } on Object {
135
+ closeClient ();
136
+ rethrow ;
124
137
}
125
138
}
126
139
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ dependencies:
13
13
collection : ^1.15.0
14
14
http : ^0.13.4
15
15
meta : ^1.7.0
16
+ stream_transform : ^2.0.0
16
17
uuid : 3.0.6
17
18
18
19
dev_dependencies :
You can’t perform that action at this time.
0 commit comments