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 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
| 10 import 'package:stack_trace/stack_trace.dart'; |
| 11 |
10 import 'base_client.dart'; | 12 import 'base_client.dart'; |
11 import 'base_request.dart'; | 13 import 'base_request.dart'; |
12 import 'streamed_response.dart'; | 14 import 'streamed_response.dart'; |
13 | 15 |
14 /// A `dart:io`-based HTTP client. This is the default client. | 16 /// A `dart:io`-based HTTP client. This is the default client. |
15 class IOClient extends BaseClient { | 17 class IOClient extends BaseClient { |
16 /// The underlying `dart:io` HTTP client. | 18 /// The underlying `dart:io` HTTP client. |
17 HttpClient _inner; | 19 HttpClient _inner; |
18 | 20 |
19 /// Creates a new HTTP client. | 21 /// Creates a new HTTP client. |
20 IOClient() : _inner = new HttpClient(); | 22 IOClient() : _inner = new HttpClient(); |
21 | 23 |
22 /// Sends an HTTP request and asynchronously returns the response. | 24 /// Sends an HTTP request and asynchronously returns the response. |
23 Future<StreamedResponse> send(BaseRequest request) { | 25 Future<StreamedResponse> send(BaseRequest request) { |
24 var stream = request.finalize(); | 26 var stream = request.finalize(); |
25 | 27 |
26 return _inner.openUrl(request.method, request.url).then((ioRequest) { | 28 return Chain.track(_inner.openUrl(request.method, request.url)) |
| 29 .then((ioRequest) { |
27 ioRequest.followRedirects = request.followRedirects; | 30 ioRequest.followRedirects = request.followRedirects; |
28 ioRequest.maxRedirects = request.maxRedirects; | 31 ioRequest.maxRedirects = request.maxRedirects; |
29 ioRequest.contentLength = request.contentLength; | 32 ioRequest.contentLength = request.contentLength; |
30 ioRequest.persistentConnection = request.persistentConnection; | 33 ioRequest.persistentConnection = request.persistentConnection; |
31 request.headers.forEach((name, value) { | 34 request.headers.forEach((name, value) { |
32 ioRequest.headers.set(name, value); | 35 ioRequest.headers.set(name, value); |
33 }); | 36 }); |
34 return stream.pipe(ioRequest); | 37 return Chain.track(stream.pipe(ioRequest)); |
35 }).then((response) { | 38 }).then((response) { |
36 var headers = {}; | 39 var headers = {}; |
37 response.headers.forEach((key, values) { | 40 response.headers.forEach((key, values) { |
38 headers[key] = values.join(','); | 41 headers[key] = values.join(','); |
39 }); | 42 }); |
40 | 43 |
41 return new StreamedResponse( | 44 return new StreamedResponse( |
42 response, | 45 response, |
43 response.statusCode, | 46 response.statusCode, |
44 response.contentLength, | 47 response.contentLength, |
45 request: request, | 48 request: request, |
46 headers: headers, | 49 headers: headers, |
47 isRedirect: response.isRedirect, | 50 isRedirect: response.isRedirect, |
48 persistentConnection: response.persistentConnection, | 51 persistentConnection: response.persistentConnection, |
49 reasonPhrase: response.reasonPhrase); | 52 reasonPhrase: response.reasonPhrase); |
50 }); | 53 }); |
51 } | 54 } |
52 | 55 |
53 /// Closes the client. This terminates all active connections. If a client | 56 /// Closes the client. This terminates all active connections. If a client |
54 /// remains unclosed, the Dart process may not terminate. | 57 /// remains unclosed, the Dart process may not terminate. |
55 void close() { | 58 void close() { |
56 if (_inner != null) _inner.close(force: true); | 59 if (_inner != null) _inner.close(force: true); |
57 _inner = null; | 60 _inner = null; |
58 } | 61 } |
59 } | 62 } |
OLD | NEW |