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, |
11 HttpClientRequest, | 11 HttpClientRequest, |
| 12 HttpException, |
12 HttpHeaders; | 13 HttpHeaders; |
13 | 14 |
14 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; | 15 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; |
15 | 16 |
16 /// Read the bytes of a URI as a stream of bytes. | 17 /// Read the bytes of a URI as a stream of bytes. |
17 Stream<List<int>> readAsStream(Uri uri) async* { | 18 Stream<List<int>> readAsStream(Uri uri) async* { |
18 if (uri.scheme == "file") { | 19 if (uri.scheme == "file") { |
19 yield* new File.fromUri(uri).openRead(); | 20 yield* new File.fromUri(uri).openRead(); |
20 return; | 21 return; |
21 } | 22 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 return new String.fromCharCodes(byteList); | 84 return new String.fromCharCodes(byteList); |
84 } | 85 } |
85 return response.transform(encoding.decoder).join(); | 86 return response.transform(encoding.decoder).join(); |
86 } | 87 } |
87 if (uri.scheme == "data") { | 88 if (uri.scheme == "data") { |
88 return uri.data.contentAsString(encoding: encoding); | 89 return uri.data.contentAsString(encoding: encoding); |
89 } | 90 } |
90 throw new UnsupportedError("Unsupported scheme: $uri"); | 91 throw new UnsupportedError("Unsupported scheme: $uri"); |
91 } | 92 } |
92 | 93 |
| 94 HttpClient _sharedHttpClient = new HttpClient()..maxConnectionsPerHost = 6; |
| 95 |
93 Future<HttpClientResponse> _httpGetBytes(Uri uri) async { | 96 Future<HttpClientResponse> _httpGetBytes(Uri uri) async { |
94 HttpClientRequest request = await new HttpClient().getUrl(uri); | 97 HttpClientRequest request = await _sharedHttpClient.getUrl(uri); |
95 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); | 98 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); |
96 return request.close(); | 99 return request.close(); |
97 } | 100 } |
98 | 101 |
99 void _throwIfFailed(HttpClientResponse response, Uri uri) { | 102 void _throwIfFailed(HttpClientResponse response, Uri uri) { |
100 var statusCode = response.statusCode; | 103 var statusCode = response.statusCode; |
101 if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) { | 104 if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) { |
102 throw new HttpException(response.reasonPhrase, uri: uri); | 105 throw new HttpException(response.reasonPhrase, uri: uri); |
103 } | 106 } |
104 } | 107 } |
OLD | NEW |