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