| 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 var contentLength = request.contentLength == null ? |
| 31 -1 : request.contentLength; |
| 30 ioRequest | 32 ioRequest |
| 31 ..followRedirects = request.followRedirects | 33 ..followRedirects = request.followRedirects |
| 32 ..maxRedirects = request.maxRedirects | 34 ..maxRedirects = request.maxRedirects |
| 33 ..contentLength = request.contentLength | 35 ..contentLength = contentLength |
| 34 ..persistentConnection = request.persistentConnection; | 36 ..persistentConnection = request.persistentConnection; |
| 35 request.headers.forEach((name, value) { | 37 request.headers.forEach((name, value) { |
| 36 ioRequest.headers.set(name, value); | 38 ioRequest.headers.set(name, value); |
| 37 }); | 39 }); |
| 38 return Chain.track(stream.pipe(ioRequest)); | 40 return Chain.track(stream.pipe(ioRequest)); |
| 39 }).then((response) { | 41 }).then((response) { |
| 40 var headers = {}; | 42 var headers = {}; |
| 41 response.headers.forEach((key, values) { | 43 response.headers.forEach((key, values) { |
| 42 headers[key] = values.join(','); | 44 headers[key] = values.join(','); |
| 43 }); | 45 }); |
| 44 | 46 |
| 47 var contentLength = response.contentLength == -1 ? |
| 48 null : response.contentLength; |
| 45 return new StreamedResponse( | 49 return new StreamedResponse( |
| 46 response, | 50 response, |
| 47 response.statusCode, | 51 response.statusCode, |
| 48 response.contentLength, | 52 contentLength: contentLength, |
| 49 request: request, | 53 request: request, |
| 50 headers: headers, | 54 headers: headers, |
| 51 isRedirect: response.isRedirect, | 55 isRedirect: response.isRedirect, |
| 52 persistentConnection: response.persistentConnection, | 56 persistentConnection: response.persistentConnection, |
| 53 reasonPhrase: response.reasonPhrase); | 57 reasonPhrase: response.reasonPhrase); |
| 54 }); | 58 }); |
| 55 } | 59 } |
| 56 | 60 |
| 57 /// Closes the client. This terminates all active connections. If a client | 61 /// Closes the client. This terminates all active connections. If a client |
| 58 /// remains unclosed, the Dart process may not terminate. | 62 /// remains unclosed, the Dart process may not terminate. |
| 59 void close() { | 63 void close() { |
| 60 if (_inner != null) _inner.close(force: true); | 64 if (_inner != null) _inner.close(force: true); |
| 61 _inner = null; | 65 _inner = null; |
| 62 } | 66 } |
| 63 } | 67 } |
| OLD | NEW |