OLD | NEW |
(Empty) | |
| 1 library http_base.http_base_io; |
| 2 |
| 3 import 'dart:io' as io; |
| 4 import 'dart:async'; |
| 5 |
| 6 import 'http_base.dart'; |
| 7 export 'http_base.dart'; |
| 8 |
| 9 /// An implementation for [RequestHandler]. It uses dart:io to make http |
| 10 /// requests. |
| 11 class Client { |
| 12 // TODO: Should we provide a mechanism to close (forcefully or not) [_client]? |
| 13 final io.HttpClient _client = new io.HttpClient(); |
| 14 |
| 15 Future<Response> call(Request request) { |
| 16 return _client.openUrl(request.method, request.url).then((ioRequest) { |
| 17 // TODO: Special case Cookie/Set-Cookie here! |
| 18 |
| 19 for (var name in request.headers.names) { |
| 20 ioRequest.headers.set(name, request.headers[name]); |
| 21 } |
| 22 |
| 23 var stream = request.read(); |
| 24 return ioRequest.addStream(stream).then((_) { |
| 25 return ioRequest.close(); |
| 26 }); |
| 27 }).then((io.HttpClientResponse ioResponse) { |
| 28 var headerMap = {}; |
| 29 ioResponse.headers.forEach((name, values) { |
| 30 headerMap[name] = values; |
| 31 }); |
| 32 var headers = new HeadersImpl(headerMap); |
| 33 |
| 34 return new ResponseImpl( |
| 35 ioResponse.statusCode, headers: headers, body: ioResponse); |
| 36 }); |
| 37 } |
| 38 } |
OLD | NEW |