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:io'; | 8 import 'dart:io'; |
8 | 9 |
9 import 'base_client.dart'; | 10 import 'base_client.dart'; |
10 import 'base_request.dart'; | 11 import 'base_request.dart'; |
11 import 'streamed_response.dart'; | 12 import 'streamed_response.dart'; |
12 import 'utils.dart'; | 13 import 'utils.dart'; |
13 | 14 |
14 /// A `dart:io`-based HTTP client. This is the default client. | 15 /// A `dart:io`-based HTTP client. This is the default client. |
15 class IOClient extends BaseClient { | 16 class IOClient extends BaseClient { |
16 /// The underlying `dart:io` HTTP client. | 17 /// The underlying `dart:io` HTTP client. |
17 HttpClient _inner; | 18 HttpClient _inner; |
18 | 19 |
19 /// Creates a new HTTP client. | 20 /// Creates a new HTTP client. |
20 IOClient() : _inner = new HttpClient(); | 21 IOClient() : _inner = new HttpClient(); |
21 | 22 |
22 /// Sends an HTTP request and asynchronously returns the response. | 23 /// Sends an HTTP request and asynchronously returns the response. |
23 Future<StreamedResponse> send(BaseRequest request) { | 24 Future<StreamedResponse> send(BaseRequest request) { |
24 var stream = request.finalize(); | 25 var stream = request.finalize(); |
25 | 26 |
26 var completer = new Completer<StreamedResponse>(); | 27 var completer = new Completer<StreamedResponse>(); |
27 var connection = _inner.openUrl(request.method, request.url); | 28 var connection = _inner.openUrl(request.method, request.url); |
| 29 bool completed = false; |
28 connection.followRedirects = request.followRedirects; | 30 connection.followRedirects = request.followRedirects; |
29 connection.maxRedirects = request.maxRedirects; | 31 connection.maxRedirects = request.maxRedirects; |
30 connection.onError = (e) { | 32 connection.onError = (e) { |
31 async.then((_) { | 33 async.then((_) { |
32 // TODO(nweiz): issue 4974 means that any errors that appear in the | 34 // TODO(nweiz): issue 4974 means that any errors that appear in the |
33 // onRequest or onResponse callbacks get passed to onError. If the | 35 // onRequest or onResponse callbacks get passed to onError. If the |
34 // completer has already fired, we want to re-throw those exceptions | 36 // completer has already fired, we want to re-throw those exceptions |
35 // to the top level so that they aren't silently ignored. | 37 // to the top level so that they aren't silently ignored. |
36 if (completer.future.isComplete) throw e; | 38 if (completed) throw e; |
37 | 39 |
38 completer.completeException(e); | 40 completed = true; |
| 41 completer.completeError(e); |
39 }); | 42 }); |
40 }; | 43 }; |
41 | 44 |
42 connection.onRequest = (underlyingRequest) { | 45 connection.onRequest = (underlyingRequest) { |
43 underlyingRequest.contentLength = request.contentLength; | 46 underlyingRequest.contentLength = request.contentLength; |
44 underlyingRequest.persistentConnection = request.persistentConnection; | 47 underlyingRequest.persistentConnection = request.persistentConnection; |
45 request.headers.forEach((name, value) { | 48 request.headers.forEach((name, value) { |
46 underlyingRequest.headers.set(name, value); | 49 underlyingRequest.headers.set(name, value); |
47 }); | 50 }); |
48 | 51 |
49 if (stream.closed) { | 52 if (stream.closed) { |
50 underlyingRequest.outputStream.close(); | 53 underlyingRequest.outputStream.close(); |
51 } else { | 54 } else { |
52 stream.pipe(underlyingRequest.outputStream); | 55 stream.pipe(underlyingRequest.outputStream); |
53 } | 56 } |
54 }; | 57 }; |
55 | 58 |
56 connection.onResponse = (response) { | 59 connection.onResponse = (response) { |
57 var headers = <String>{}; | 60 var headers = <String>{}; |
58 response.headers.forEach((key, value) => headers[key] = value); | 61 response.headers.forEach((key, value) => headers[key] = value); |
59 | 62 |
| 63 if (completed) return; |
| 64 |
| 65 completed = true; |
60 completer.complete(new StreamedResponse( | 66 completer.complete(new StreamedResponse( |
61 response.inputStream, | 67 response.inputStream, |
62 response.statusCode, | 68 response.statusCode, |
63 response.contentLength, | 69 response.contentLength, |
64 request: request, | 70 request: request, |
65 headers: headers, | 71 headers: headers, |
66 isRedirect: response.isRedirect, | 72 isRedirect: response.isRedirect, |
67 persistentConnection: response.persistentConnection, | 73 persistentConnection: response.persistentConnection, |
68 reasonPhrase: response.reasonPhrase)); | 74 reasonPhrase: response.reasonPhrase)); |
69 }; | 75 }; |
70 | 76 |
71 return completer.future; | 77 return completer.future; |
72 } | 78 } |
73 | 79 |
74 /// Closes the client. This terminates all active connections. If a client | 80 /// Closes the client. This terminates all active connections. If a client |
75 /// remains unclosed, the Dart process may not terminate. | 81 /// remains unclosed, the Dart process may not terminate. |
76 void close() { | 82 void close() { |
77 if (_inner != null) _inner.shutdown(force: true); | 83 if (_inner != null) _inner.shutdown(force: true); |
78 _inner = null; | 84 _inner = null; |
79 } | 85 } |
80 } | 86 } |
OLD | NEW |