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:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'base_client.dart'; | 9 import 'base_client.dart'; |
10 import 'base_request.dart'; | 10 import 'base_request.dart'; |
(...skipping 11 matching lines...) Expand all Loading... |
22 /// Sends an HTTP request and asynchronously returns the response. | 22 /// Sends an HTTP request and asynchronously returns the response. |
23 Future<StreamedResponse> send(BaseRequest request) { | 23 Future<StreamedResponse> send(BaseRequest request) { |
24 var stream = request.finalize(); | 24 var stream = request.finalize(); |
25 | 25 |
26 var completer = new Completer<StreamedResponse>(); | 26 var completer = new Completer<StreamedResponse>(); |
27 var connection = _inner.openUrl(request.method, request.url); | 27 var connection = _inner.openUrl(request.method, request.url); |
28 connection.followRedirects = request.followRedirects; | 28 connection.followRedirects = request.followRedirects; |
29 connection.maxRedirects = request.maxRedirects; | 29 connection.maxRedirects = request.maxRedirects; |
30 connection.onError = (e) { | 30 connection.onError = (e) { |
31 async.then((_) { | 31 async.then((_) { |
32 // TODO(nweiz): remove this when issue 4974 is fixed | 32 if (completer.future.isComplete) { |
33 if (completer.future.isComplete) throw e; | 33 // TODO(nweiz): issue 7014 means that connection errors may be routed |
| 34 // here even after onResponse has been called. Since these errors are |
| 35 // also routed to the response input stream, we want to silently |
| 36 // ignore them. |
| 37 // |
| 38 // We test if they're HTTP exceptions to distinguish them from errors |
| 39 // caused by issue 4974 (see below). |
| 40 if (e is HttpException) return; |
| 41 |
| 42 // TODO(nweiz): issue 4974 means that any errors that appear in the |
| 43 // onRequest or onResponse callbacks get passed to onError. If the |
| 44 // completer has already fired, we want to re-throw those exceptions |
| 45 // to the top level so that they aren't silently ignored. |
| 46 throw e; |
| 47 } |
34 | 48 |
35 completer.completeException(e); | 49 completer.completeException(e); |
36 }); | 50 }); |
37 }; | 51 }; |
38 | 52 |
39 connection.onRequest = (underlyingRequest) { | 53 connection.onRequest = (underlyingRequest) { |
40 underlyingRequest.contentLength = request.contentLength; | 54 underlyingRequest.contentLength = request.contentLength; |
41 underlyingRequest.persistentConnection = request.persistentConnection; | 55 underlyingRequest.persistentConnection = request.persistentConnection; |
42 request.headers.forEach((name, value) { | 56 request.headers.forEach((name, value) { |
43 underlyingRequest.headers.set(name, value); | 57 underlyingRequest.headers.set(name, value); |
44 }); | 58 }); |
45 | 59 |
46 if (stream.closed) { | 60 if (stream.closed) { |
47 underlyingRequest.outputStream.close(); | 61 underlyingRequest.outputStream.close(); |
48 } else { | 62 } else { |
49 stream.pipe(underlyingRequest.outputStream); | 63 stream.pipe(underlyingRequest.outputStream); |
50 } | 64 } |
51 }; | 65 }; |
52 | 66 |
53 connection.onResponse = (response) { | 67 connection.onResponse = (response) { |
54 var headers = <String>{}; | 68 var headers = <String>{}; |
55 response.headers.forEach((key, value) => headers[key] = value); | 69 response.headers.forEach((key, value) => headers[key] = value); |
56 | 70 |
57 completer.complete(new StreamedResponse( | 71 completer.complete(new StreamedResponse( |
58 response.inputStream, | 72 wrapInputStream(response.inputStream), |
59 response.statusCode, | 73 response.statusCode, |
60 response.contentLength, | 74 response.contentLength, |
61 headers: headers, | 75 headers: headers, |
62 isRedirect: response.isRedirect, | 76 isRedirect: response.isRedirect, |
63 persistentConnection: response.persistentConnection, | 77 persistentConnection: response.persistentConnection, |
64 reasonPhrase: response.reasonPhrase)); | 78 reasonPhrase: response.reasonPhrase)); |
65 }; | 79 }; |
66 | 80 |
67 return completer.future; | 81 return completer.future; |
68 } | 82 } |
69 | 83 |
70 /// Closes the client. This terminates all active connections. If a client | 84 /// Closes the client. This terminates all active connections. If a client |
71 /// remains unclosed, the Dart process may not terminate. | 85 /// remains unclosed, the Dart process may not terminate. |
72 void close() { | 86 void close() { |
73 if (_inner != null) _inner.shutdown(); | 87 if (_inner != null) _inner.shutdown(force: true); |
74 _inner = null; | 88 _inner = null; |
75 } | 89 } |
76 } | 90 } |
OLD | NEW |