| 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 if (completer.future.isComplete) { | 32 // TODO(nweiz): issue 4974 means that any errors that appear in the |
| 33 // TODO(nweiz): issue 7014 means that connection errors may be routed | 33 // onRequest or onResponse callbacks get passed to onError. If the |
| 34 // here even after onResponse has been called. Since these errors are | 34 // completer has already fired, we want to re-throw those exceptions |
| 35 // also routed to the response input stream, we want to silently | 35 // to the top level so that they aren't silently ignored. |
| 36 // ignore them. | 36 if (completer.future.isComplete) throw e; |
| 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 } | |
| 48 | 37 |
| 49 completer.completeException(e); | 38 completer.completeException(e); |
| 50 }); | 39 }); |
| 51 }; | 40 }; |
| 52 | 41 |
| 53 connection.onRequest = (underlyingRequest) { | 42 connection.onRequest = (underlyingRequest) { |
| 54 underlyingRequest.contentLength = request.contentLength; | 43 underlyingRequest.contentLength = request.contentLength; |
| 55 underlyingRequest.persistentConnection = request.persistentConnection; | 44 underlyingRequest.persistentConnection = request.persistentConnection; |
| 56 request.headers.forEach((name, value) { | 45 request.headers.forEach((name, value) { |
| 57 underlyingRequest.headers.set(name, value); | 46 underlyingRequest.headers.set(name, value); |
| 58 }); | 47 }); |
| 59 | 48 |
| 60 if (stream.closed) { | 49 if (stream.closed) { |
| 61 underlyingRequest.outputStream.close(); | 50 underlyingRequest.outputStream.close(); |
| 62 } else { | 51 } else { |
| 63 stream.pipe(underlyingRequest.outputStream); | 52 stream.pipe(underlyingRequest.outputStream); |
| 64 } | 53 } |
| 65 }; | 54 }; |
| 66 | 55 |
| 67 connection.onResponse = (response) { | 56 connection.onResponse = (response) { |
| 68 var headers = <String>{}; | 57 var headers = <String>{}; |
| 69 response.headers.forEach((key, value) => headers[key] = value); | 58 response.headers.forEach((key, value) => headers[key] = value); |
| 70 | 59 |
| 71 completer.complete(new StreamedResponse( | 60 completer.complete(new StreamedResponse( |
| 72 wrapInputStream(response.inputStream), | 61 response.inputStream, |
| 73 response.statusCode, | 62 response.statusCode, |
| 74 response.contentLength, | 63 response.contentLength, |
| 75 request: request, | 64 request: request, |
| 76 headers: headers, | 65 headers: headers, |
| 77 isRedirect: response.isRedirect, | 66 isRedirect: response.isRedirect, |
| 78 persistentConnection: response.persistentConnection, | 67 persistentConnection: response.persistentConnection, |
| 79 reasonPhrase: response.reasonPhrase)); | 68 reasonPhrase: response.reasonPhrase)); |
| 80 }; | 69 }; |
| 81 | 70 |
| 82 return completer.future; | 71 return completer.future; |
| 83 } | 72 } |
| 84 | 73 |
| 85 /// Closes the client. This terminates all active connections. If a client | 74 /// Closes the client. This terminates all active connections. If a client |
| 86 /// remains unclosed, the Dart process may not terminate. | 75 /// remains unclosed, the Dart process may not terminate. |
| 87 void close() { | 76 void close() { |
| 88 if (_inner != null) _inner.shutdown(force: true); | 77 if (_inner != null) _inner.shutdown(force: true); |
| 89 _inner = null; | 78 _inner = null; |
| 90 } | 79 } |
| 91 } | 80 } |
| OLD | NEW |