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