| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library client; |
| 6 |
| 7 import 'dart:io'; |
| 8 |
| 9 import 'base_client.dart'; |
| 10 import 'base_request.dart'; |
| 11 import 'streamed_response.dart'; |
| 12 import 'utils.dart'; |
| 13 |
| 14 /// An HTTP client which takes care of maintaining persistent connections across |
| 15 /// multiple requests to the same server. If you only need to send a single |
| 16 /// request, it's usually easier to use [head], [get], [post], |
| 17 /// [put], or [delete] instead. |
| 18 /// |
| 19 /// When creating an HTTP client class with additional functionality, it's |
| 20 /// recommended that you subclass [BaseClient] and wrap another instance of |
| 21 /// [BaseClient] rather than subclassing [Client] directly. This allows all |
| 22 /// subclasses of [BaseClient] to be mutually composable. |
| 23 class Client extends BaseClient { |
| 24 /// The underlying `dart:io` HTTP client. |
| 25 HttpClient _inner; |
| 26 |
| 27 /// Creates a new HTTP client. |
| 28 Client() : _inner = new HttpClient(); |
| 29 |
| 30 /// Sends an HTTP request and asynchronously returns the response. |
| 31 Future<StreamedResponse> send(BaseRequest request) { |
| 32 var stream = request.finalize(); |
| 33 |
| 34 var completer = new Completer<StreamedResponse>(); |
| 35 var connection = _inner.openUrl(request.method, request.url); |
| 36 connection.onError = (e) { |
| 37 async.then((_) { |
| 38 // TODO(nweiz): remove this when issue 4974 is fixed |
| 39 if (completer.future.isComplete) throw e; |
| 40 |
| 41 completer.completeException(e); |
| 42 }); |
| 43 }; |
| 44 |
| 45 connection.onRequest = (underlyingRequest) { |
| 46 underlyingRequest.contentLength = request.contentLength; |
| 47 underlyingRequest.persistentConnection = request.persistentConnection; |
| 48 request.headers.forEach((name, value) { |
| 49 underlyingRequest.headers.set(name, value); |
| 50 }); |
| 51 |
| 52 if (stream.closed) { |
| 53 underlyingRequest.outputStream.close(); |
| 54 } else { |
| 55 stream.pipe(underlyingRequest.outputStream); |
| 56 } |
| 57 }; |
| 58 |
| 59 connection.onResponse = (response) { |
| 60 var headers = <String>{}; |
| 61 response.headers.forEach((key, value) => headers[key] = value); |
| 62 |
| 63 completer.complete(new StreamedResponse( |
| 64 response.inputStream, |
| 65 response.statusCode, |
| 66 response.contentLength, |
| 67 headers: headers, |
| 68 isRedirect: response.isRedirect, |
| 69 persistentConnection: response.persistentConnection, |
| 70 reasonPhrase: response.reasonPhrase)); |
| 71 }; |
| 72 |
| 73 return completer.future; |
| 74 } |
| 75 |
| 76 /// Closes the client. This terminates all active connections. If a client |
| 77 /// remains unclosed, the Dart process may not terminate. |
| 78 void close() { |
| 79 if (_inner != null) _inner.shutdown(); |
| 80 _inner = null; |
| 81 } |
| 82 } |
| OLD | NEW |