OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 part of vmservice_io; |
| 6 |
| 7 var _httpClient; |
| 8 |
| 9 void _loadHttp(sendPort, uri) { |
| 10 if (_httpClient == null) { |
| 11 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; |
| 12 } |
| 13 _httpClient.getUrl(uri) |
| 14 .then((HttpClientRequest request) => request.close()) |
| 15 .then((HttpClientResponse response) { |
| 16 var builder = new BytesBuilder(copy: false); |
| 17 response.listen( |
| 18 builder.add, |
| 19 onDone: () { |
| 20 if (response.statusCode != 200) { |
| 21 var msg = 'Failure getting $uri: ' |
| 22 '${response.statusCode} ${response.reasonPhrase}'; |
| 23 sendPort.send(msg); |
| 24 } else { |
| 25 sendPort.send(builder.takeBytes()); |
| 26 } |
| 27 }, |
| 28 onError: (e) { |
| 29 sendPort.send(e.toString()); |
| 30 }); |
| 31 }) |
| 32 .catchError((e) { |
| 33 sendPort.send(e.toString()); |
| 34 }); |
| 35 // It's just here to push an event on the event loop so that we invoke the |
| 36 // scheduled microtasks. |
| 37 Timer.run(() {}); |
| 38 } |
| 39 |
| 40 void _loadFile(sendPort, path) { |
| 41 var sourceFile = new File(path); |
| 42 sourceFile.readAsBytes().then((data) { |
| 43 sendPort.send(data); |
| 44 }, |
| 45 onError: (e) { |
| 46 sendPort.send(e.toString()); |
| 47 }); |
| 48 } |
| 49 |
| 50 _processLoadRequest(request) { |
| 51 var sp = request[0]; |
| 52 var uri = Uri.parse(request[1]); |
| 53 if (uri.scheme == 'file') { |
| 54 _loadFile(sp, uri.toFilePath()); |
| 55 } else if ((uri.scheme == 'http') || (uri.scheme == 'https')) { |
| 56 _loadHttp(sp, uri); |
| 57 } else { |
| 58 sp.send('Unknown scheme for $uri'); |
| 59 } |
| 60 } |
OLD | NEW |