| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import "dart:async" show Future, Stream; | 5 import "dart:async" show Future, Stream; |
| 6 import "dart:convert" show Encoding, LATIN1, UTF8; | 6 import "dart:convert" show Encoding, LATIN1, UTF8; |
| 7 import "dart:io" show File, | 7 import "dart:io" show File, |
| 8 HttpStatus, | 8 HttpStatus, |
| 9 HttpClient, | 9 HttpClient, |
| 10 HttpClientResponse, | 10 HttpClientResponse, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name); | 69 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name); |
| 70 } | 70 } |
| 71 HttpClientResponse response = await request.close(); | 71 HttpClientResponse response = await request.close(); |
| 72 _throwIfFailed(response, uri); | 72 _throwIfFailed(response, uri); |
| 73 encoding ??= Encoding.getByName(response.headers.contentType?.charset); | 73 encoding ??= Encoding.getByName(response.headers.contentType?.charset); |
| 74 if (encoding == null || encoding == LATIN1) { | 74 if (encoding == null || encoding == LATIN1) { |
| 75 // Default to LATIN-1 if no encoding found. | 75 // Default to LATIN-1 if no encoding found. |
| 76 // Special case LATIN-1 since it is common and doesn't need decoding. | 76 // Special case LATIN-1 since it is common and doesn't need decoding. |
| 77 int length = response.contentLength; | 77 int length = response.contentLength; |
| 78 if (length < 0) length = 0; | 78 if (length < 0) length = 0; |
| 79 var buffer = new Uint8Buffer(length); | 79 // Create empty buffer with capacity matching contentLength. |
| 80 var buffer = new Uint8Buffer(length)..length = 0; |
| 80 await for (var bytes in response) { | 81 await for (var bytes in response) { |
| 81 buffer.addAll(bytes); | 82 buffer.addAll(bytes); |
| 82 } | 83 } |
| 83 var byteList = buffer.buffer.asUint8List(0, buffer.length); | 84 var byteList = buffer.buffer.asUint8List(0, buffer.length); |
| 84 return new String.fromCharCodes(byteList); | 85 return new String.fromCharCodes(byteList); |
| 85 } | 86 } |
| 86 return response.transform(encoding.decoder).join(); | 87 return response.transform(encoding.decoder).join(); |
| 87 } | 88 } |
| 88 if (uri.scheme == "data") { | 89 if (uri.scheme == "data") { |
| 89 return uri.data.contentAsString(encoding: encoding); | 90 return uri.data.contentAsString(encoding: encoding); |
| 90 } | 91 } |
| 91 throw new UnsupportedError("Unsupported scheme: $uri"); | 92 throw new UnsupportedError("Unsupported scheme: $uri"); |
| 92 } | 93 } |
| 93 | 94 |
| 94 HttpClient _sharedHttpClient = new HttpClient()..maxConnectionsPerHost = 6; | 95 HttpClient _sharedHttpClient = new HttpClient()..maxConnectionsPerHost = 6; |
| 95 | 96 |
| 96 Future<HttpClientResponse> _httpGetBytes(Uri uri) async { | 97 Future<HttpClientResponse> _httpGetBytes(Uri uri) async { |
| 97 HttpClientRequest request = await _sharedHttpClient.getUrl(uri); | 98 HttpClientRequest request = await _sharedHttpClient.getUrl(uri); |
| 98 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); | 99 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); |
| 99 return request.close(); | 100 return request.close(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void _throwIfFailed(HttpClientResponse response, Uri uri) { | 103 void _throwIfFailed(HttpClientResponse response, Uri uri) { |
| 103 var statusCode = response.statusCode; | 104 var statusCode = response.statusCode; |
| 104 if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) { | 105 if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) { |
| 105 throw new HttpException(response.reasonPhrase, uri: uri); | 106 throw new HttpException(response.reasonPhrase, uri: uri); |
| 106 } | 107 } |
| 107 } | 108 } |
| OLD | NEW |