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 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | |
10 | |
11 import 'base_client.dart'; | 9 import 'base_client.dart'; |
12 import 'base_request.dart'; | 10 import 'base_request.dart'; |
13 import 'exception.dart'; | 11 import 'exception.dart'; |
14 import 'io.dart' as io; | 12 import 'io.dart' as io; |
15 import 'streamed_response.dart'; | 13 import 'streamed_response.dart'; |
16 | 14 |
17 /// A `dart:io`-based HTTP client. | 15 /// A `dart:io`-based HTTP client. |
18 /// | 16 /// |
19 /// This is the default client when running on the command line. | 17 /// This is the default client when running on the command line. |
20 class IOClient extends BaseClient { | 18 class IOClient extends BaseClient { |
(...skipping 13 matching lines...) Expand all Loading... |
34 _inner = innerClient; | 32 _inner = innerClient; |
35 } else { | 33 } else { |
36 _inner = io.newHttpClient(); | 34 _inner = io.newHttpClient(); |
37 } | 35 } |
38 } | 36 } |
39 | 37 |
40 /// Sends an HTTP request and asynchronously returns the response. | 38 /// Sends an HTTP request and asynchronously returns the response. |
41 Future<StreamedResponse> send(BaseRequest request) { | 39 Future<StreamedResponse> send(BaseRequest request) { |
42 var stream = request.finalize(); | 40 var stream = request.finalize(); |
43 | 41 |
44 return Chain.track(_inner.openUrl(request.method, request.url)) | 42 return _inner.openUrl(request.method, request.url) |
45 .then((ioRequest) { | 43 .then((ioRequest) { |
46 var contentLength = request.contentLength == null ? | 44 var contentLength = request.contentLength == null ? |
47 -1 : request.contentLength; | 45 -1 : request.contentLength; |
48 ioRequest | 46 ioRequest |
49 ..followRedirects = request.followRedirects | 47 ..followRedirects = request.followRedirects |
50 ..maxRedirects = request.maxRedirects | 48 ..maxRedirects = request.maxRedirects |
51 ..contentLength = contentLength | 49 ..contentLength = contentLength |
52 ..persistentConnection = request.persistentConnection; | 50 ..persistentConnection = request.persistentConnection; |
53 request.headers.forEach((name, value) { | 51 request.headers.forEach((name, value) { |
54 ioRequest.headers.set(name, value); | 52 ioRequest.headers.set(name, value); |
55 }); | 53 }); |
56 return Chain.track(stream.pipe(ioRequest)); | 54 return stream.pipe(ioRequest); |
57 }).then((response) { | 55 }).then((response) { |
58 var headers = {}; | 56 var headers = {}; |
59 response.headers.forEach((key, values) { | 57 response.headers.forEach((key, values) { |
60 headers[key] = values.join(','); | 58 headers[key] = values.join(','); |
61 }); | 59 }); |
62 | 60 |
63 var contentLength = response.contentLength == -1 ? | 61 var contentLength = response.contentLength == -1 ? |
64 null : response.contentLength; | 62 null : response.contentLength; |
65 return new StreamedResponse( | 63 return new StreamedResponse( |
66 response.handleError((error) => | 64 response.handleError((error) => |
(...skipping 12 matching lines...) Expand all Loading... |
79 }); | 77 }); |
80 } | 78 } |
81 | 79 |
82 /// Closes the client. This terminates all active connections. If a client | 80 /// Closes the client. This terminates all active connections. If a client |
83 /// remains unclosed, the Dart process may not terminate. | 81 /// remains unclosed, the Dart process may not terminate. |
84 void close() { | 82 void close() { |
85 if (_inner != null) _inner.close(force: true); | 83 if (_inner != null) _inner.close(force: true); |
86 _inner = null; | 84 _inner = null; |
87 } | 85 } |
88 } | 86 } |
OLD | NEW |