OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library io_client; | 5 library io_client; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
11 import 'base_client.dart'; | 11 import 'base_client.dart'; |
12 import 'base_request.dart'; | 12 import 'base_request.dart'; |
13 import 'exception.dart'; | 13 import 'exception.dart'; |
14 import 'io.dart' as io; | 14 import 'io.dart' as io; |
15 import 'streamed_response.dart'; | 15 import 'streamed_response.dart'; |
16 | 16 |
17 /// A `dart:io`-based HTTP client. This is the default client. | 17 /// A `dart:io`-based HTTP client. This is the default client. |
18 class IOClient extends BaseClient { | 18 class IOClient extends BaseClient { |
19 /// The underlying `dart:io` HTTP client. | 19 /// The underlying `dart:io` HTTP client. |
20 var _inner; | 20 var _inner; |
21 | 21 |
22 /// Creates a new HTTP client. | 22 /// Creates a new HTTP client. |
23 IOClient() { | 23 /// |
| 24 /// [innerClient] must be a `dart:io` HTTP client. If it's not passed, a |
| 25 /// default one will be instantiated. |
| 26 IOClient([innerClient]) { |
24 io.assertSupported("IOClient"); | 27 io.assertSupported("IOClient"); |
25 _inner = io.newHttpClient(); | 28 if (innerClient != null) { |
| 29 // TODO(nweiz): remove this assert when we can type [innerClient] |
| 30 // properly. |
| 31 assert(io.isHttpClient(innerClient)); |
| 32 _inner = innerClient; |
| 33 } else { |
| 34 _inner = io.newHttpClient(); |
| 35 } |
26 } | 36 } |
27 | 37 |
28 /// Sends an HTTP request and asynchronously returns the response. | 38 /// Sends an HTTP request and asynchronously returns the response. |
29 Future<StreamedResponse> send(BaseRequest request) { | 39 Future<StreamedResponse> send(BaseRequest request) { |
30 var stream = request.finalize(); | 40 var stream = request.finalize(); |
31 | 41 |
32 return Chain.track(_inner.openUrl(request.method, request.url)) | 42 return Chain.track(_inner.openUrl(request.method, request.url)) |
33 .then((ioRequest) { | 43 .then((ioRequest) { |
34 var contentLength = request.contentLength == null ? | 44 var contentLength = request.contentLength == null ? |
35 -1 : request.contentLength; | 45 -1 : request.contentLength; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 }); | 77 }); |
68 } | 78 } |
69 | 79 |
70 /// Closes the client. This terminates all active connections. If a client | 80 /// Closes the client. This terminates all active connections. If a client |
71 /// remains unclosed, the Dart process may not terminate. | 81 /// remains unclosed, the Dart process may not terminate. |
72 void close() { | 82 void close() { |
73 if (_inner != null) _inner.close(force: true); | 83 if (_inner != null) _inner.close(force: true); |
74 _inner = null; | 84 _inner = null; |
75 } | 85 } |
76 } | 86 } |
OLD | NEW |