| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 // dart:io based strategy for loading resources. | 5 // dart:io based strategy for loading resources. |
| 6 | 6 |
| 7 import "dart:io" show File, HttpClient, HttpClientRequest, HttpClientResponse; | |
| 8 import "dart:async" show Future, Stream; | 7 import "dart:async" show Future, Stream; |
| 9 import "dart:convert" show Encoding, UTF8; | 8 import "dart:convert" show Encoding, LATIN1, UTF8; |
| 9 import "dart:io" show |
| 10 File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders; |
| 11 import "dart:typed_data" show Uint8List; |
| 10 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; | 12 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; |
| 11 | 13 |
| 14 /// Read the bytes of a URI as a stream of bytes. |
| 12 Stream<List<int>> readAsStream(Uri uri) async* { | 15 Stream<List<int>> readAsStream(Uri uri) async* { |
| 13 if (uri.scheme == "file") { | 16 if (uri.scheme == "file") { |
| 14 yield* new File.fromUri(uri).openRead(); | 17 yield* new File.fromUri(uri).openRead(); |
| 15 return; | 18 return; |
| 16 } | 19 } |
| 17 if (uri.scheme == "http" || uri.scheme == "https") { | 20 if (uri.scheme == "http" || uri.scheme == "https") { |
| 18 HttpClientResponse response = await _httpGet(uri); | 21 HttpClientResponse response = await _httpGet(uri); |
| 19 yield* response; | 22 yield* response; |
| 20 return; | 23 return; |
| 21 } | 24 } |
| 25 if (uri.scheme == "data") { |
| 26 yield uri.data.contentAsBytes(); |
| 27 return; |
| 28 } |
| 22 throw new UnsupportedError("Unsupported scheme: $uri"); | 29 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 23 } | 30 } |
| 24 | 31 |
| 32 /// Read the bytes of a URI as a list of bytes. |
| 25 Future<List<int>> readAsBytes(Uri uri) async { | 33 Future<List<int>> readAsBytes(Uri uri) async { |
| 26 if (uri.scheme == "file") { | 34 if (uri.scheme == "file") { |
| 27 return new File.fromUri(uri).readAsBytes(); | 35 return new File.fromUri(uri).readAsBytes(); |
| 28 } | 36 } |
| 29 if (uri.scheme == "http" || uri.scheme == "https") { | 37 if (uri.scheme == "http" || uri.scheme == "https") { |
| 30 HttpClientResponse response = await _httpGet(uri); | 38 HttpClientResponse response = await _httpGet(uri); |
| 31 Uint8Buffer buffer = new Uint8Buffer(); | 39 Uint8Buffer buffer = new Uint8Buffer(); |
| 32 await for (var bytes in response) { | 40 await for (var bytes in response) { |
| 33 buffer.addAll(bytes); | 41 buffer.addAll(bytes); |
| 34 } | 42 } |
| 35 return buffer.toList(); | 43 return buffer.toList(); |
| 36 } | 44 } |
| 45 if (uri.scheme == "data") { |
| 46 return uri.data.contentAsBytes(); |
| 47 } |
| 37 throw new UnsupportedError("Unsupported scheme: $uri"); | 48 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 38 } | 49 } |
| 39 | 50 |
| 51 /// Read the bytes of a URI as a string. |
| 40 Future<String> readAsString(Uri uri, Encoding encoding) async { | 52 Future<String> readAsString(Uri uri, Encoding encoding) async { |
| 41 if (encoding == null) encoding = UTF8; | |
| 42 if (uri.scheme == "file") { | 53 if (uri.scheme == "file") { |
| 54 if (encoding == null) encoding = UTF8; |
| 43 return new File.fromUri(uri).readAsString(encoding: encoding); | 55 return new File.fromUri(uri).readAsString(encoding: encoding); |
| 44 } | 56 } |
| 45 if (uri.scheme == "http" || uri.scheme == "https") { | 57 if (uri.scheme == "http" || uri.scheme == "https") { |
| 46 HttpClientResponse response = await _httpGet(uri); | 58 HttpClientRequest request = await new HttpClient().getUrl(uri); |
| 47 Uint8Buffer buffer = new Uint8Buffer(); | 59 // Prefer text/plain, text/* if possible, otherwise take whatever is there. |
| 48 await for (var bytes in response) { | 60 request.headers.set(HttpHeaders.ACCEPT, "text/plain, text/*, */*"); |
| 49 buffer.addAll(bytes); | 61 if (encoding != null) { |
| 62 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name); |
| 50 } | 63 } |
| 51 new String.fromCharCodes(buffer.toList()); | 64 HttpClientResponse response = await request.close(); |
| 65 encoding ??= Encoding.getByName(response.headers.contentType?.charset); |
| 66 if (encoding == null || encoding == LATIN1) { |
| 67 // Default to LATIN-1 if no encoding found. |
| 68 // Special case LATIN-1 since it is common and doesn't need decoding. |
| 69 int length = response.contentLength; |
| 70 if (length < 0) length = 0; |
| 71 Uint8Buffer buffer = new Uint8Buffer(length); |
| 72 await for (var bytes in response) { |
| 73 buffer.addAll(bytes); |
| 74 } |
| 75 var byteList = new Uint8List.view(buffer.buffer, 0, buffer.length); |
| 76 return new String.fromCharCodes(byteList); |
| 77 } |
| 78 return response.transform(encoding.decoder).join(); |
| 79 } |
| 80 if (uri.scheme == "data") { |
| 81 return uri.data.contentAsString(encoding: encoding); |
| 52 } | 82 } |
| 53 throw new UnsupportedError("Unsupported scheme: $uri"); | 83 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 54 } | 84 } |
| 55 | 85 |
| 56 Future<HttpClientResponse> _httpGet(Uri uri) async { | 86 Future<HttpClientResponse> _httpGet(Uri uri) async { |
| 57 HttpClientRequest request = await new HttpClient().getUrl(uri); | 87 HttpClientRequest request = await new HttpClient().getUrl(uri); |
| 58 return await request.close(); | 88 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); |
| 89 return request.close(); |
| 59 } | 90 } |
| OLD | NEW |