Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: lib/src/io_io.dart

Issue 2638403002: Make failing HTTP requests thrown when using dart:io. (Closed)
Patch Set: Fix typo in changelog Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 7 import "dart:io" show File,
8 File, HttpClient, HttpClientResponse, HttpClientRequest, HttpHeaders; 8 HttpStatus,
9 HttpClient,
10 HttpClientResponse,
11 HttpClientRequest,
12 HttpHeaders;
9 13
10 import "package:typed_data/typed_buffers.dart" show Uint8Buffer; 14 import "package:typed_data/typed_buffers.dart" show Uint8Buffer;
11 15
12 /// Read the bytes of a URI as a stream of bytes. 16 /// Read the bytes of a URI as a stream of bytes.
13 Stream<List<int>> readAsStream(Uri uri) async* { 17 Stream<List<int>> readAsStream(Uri uri) async* {
14 if (uri.scheme == "file") { 18 if (uri.scheme == "file") {
15 yield* new File.fromUri(uri).openRead(); 19 yield* new File.fromUri(uri).openRead();
16 return; 20 return;
17 } 21 }
18 if (uri.scheme == "http" || uri.scheme == "https") { 22 if (uri.scheme == "http" || uri.scheme == "https") {
19 HttpClientResponse response = await _httpGetBytes(uri); 23 HttpClientResponse response = await _httpGetBytes(uri);
24 _throwIfFailed(response, uri);
20 yield* response; 25 yield* response;
21 return; 26 return;
22 } 27 }
23 if (uri.scheme == "data") { 28 if (uri.scheme == "data") {
24 yield uri.data.contentAsBytes(); 29 yield uri.data.contentAsBytes();
25 return; 30 return;
26 } 31 }
27 throw new UnsupportedError("Unsupported scheme: $uri"); 32 throw new UnsupportedError("Unsupported scheme: $uri");
28 } 33 }
29 34
30 /// Read the bytes of a URI as a list of bytes. 35 /// Read the bytes of a URI as a list of bytes.
31 Future<List<int>> readAsBytes(Uri uri) async { 36 Future<List<int>> readAsBytes(Uri uri) async {
32 if (uri.scheme == "file") { 37 if (uri.scheme == "file") {
33 return new File.fromUri(uri).readAsBytes(); 38 return new File.fromUri(uri).readAsBytes();
34 } 39 }
35 if (uri.scheme == "http" || uri.scheme == "https") { 40 if (uri.scheme == "http" || uri.scheme == "https") {
36 HttpClientResponse response = await _httpGetBytes(uri); 41 HttpClientResponse response = await _httpGetBytes(uri);
42 _throwIfFailed(response, uri);
37 int length = response.contentLength; 43 int length = response.contentLength;
38 if (length < 0) length = 0; 44 if (length < 0) length = 0;
39 var buffer = new Uint8Buffer(length); 45 var buffer = new Uint8Buffer(length);
40 await for (var bytes in response) { 46 await for (var bytes in response) {
41 buffer.addAll(bytes); 47 buffer.addAll(bytes);
42 } 48 }
43 return buffer.toList(); 49 return buffer.toList();
44 } 50 }
45 if (uri.scheme == "data") { 51 if (uri.scheme == "data") {
46 return uri.data.contentAsBytes(); 52 return uri.data.contentAsBytes();
47 } 53 }
48 throw new UnsupportedError("Unsupported scheme: $uri"); 54 throw new UnsupportedError("Unsupported scheme: $uri");
49 } 55 }
50 56
51 /// Read the bytes of a URI as a string. 57 /// Read the bytes of a URI as a string.
52 Future<String> readAsString(Uri uri, Encoding encoding) async { 58 Future<String> readAsString(Uri uri, Encoding encoding) async {
53 if (uri.scheme == "file") { 59 if (uri.scheme == "file") {
54 if (encoding == null) encoding = UTF8; 60 if (encoding == null) encoding = UTF8;
55 return new File.fromUri(uri).readAsString(encoding: encoding); 61 return new File.fromUri(uri).readAsString(encoding: encoding);
56 } 62 }
57 if (uri.scheme == "http" || uri.scheme == "https") { 63 if (uri.scheme == "http" || uri.scheme == "https") {
58 HttpClientRequest request = await new HttpClient().getUrl(uri); 64 HttpClientRequest request = await new HttpClient().getUrl(uri);
59 // Prefer text/plain, text/* if possible, otherwise take whatever is there. 65 // Prefer text/plain, text/* if possible, otherwise take whatever is there.
60 request.headers.set(HttpHeaders.ACCEPT, "text/plain, text/*, */*"); 66 request.headers.set(HttpHeaders.ACCEPT, "text/plain, text/*, */*");
61 if (encoding != null) { 67 if (encoding != null) {
62 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name); 68 request.headers.set(HttpHeaders.ACCEPT_CHARSET, encoding.name);
63 } 69 }
64 HttpClientResponse response = await request.close(); 70 HttpClientResponse response = await request.close();
71 _throwIfFailed(response, uri);
65 encoding ??= Encoding.getByName(response.headers.contentType?.charset); 72 encoding ??= Encoding.getByName(response.headers.contentType?.charset);
66 if (encoding == null || encoding == LATIN1) { 73 if (encoding == null || encoding == LATIN1) {
67 // Default to LATIN-1 if no encoding found. 74 // Default to LATIN-1 if no encoding found.
68 // Special case LATIN-1 since it is common and doesn't need decoding. 75 // Special case LATIN-1 since it is common and doesn't need decoding.
69 int length = response.contentLength; 76 int length = response.contentLength;
70 if (length < 0) length = 0; 77 if (length < 0) length = 0;
71 var buffer = new Uint8Buffer(length); 78 var buffer = new Uint8Buffer(length);
72 await for (var bytes in response) { 79 await for (var bytes in response) {
73 buffer.addAll(bytes); 80 buffer.addAll(bytes);
74 } 81 }
75 var byteList = buffer.buffer.asUint8List(0, buffer.length); 82 var byteList = buffer.buffer.asUint8List(0, buffer.length);
76 return new String.fromCharCodes(byteList); 83 return new String.fromCharCodes(byteList);
77 } 84 }
78 return response.transform(encoding.decoder).join(); 85 return response.transform(encoding.decoder).join();
79 } 86 }
80 if (uri.scheme == "data") { 87 if (uri.scheme == "data") {
81 return uri.data.contentAsString(encoding: encoding); 88 return uri.data.contentAsString(encoding: encoding);
82 } 89 }
83 throw new UnsupportedError("Unsupported scheme: $uri"); 90 throw new UnsupportedError("Unsupported scheme: $uri");
84 } 91 }
85 92
86 Future<HttpClientResponse> _httpGetBytes(Uri uri) async { 93 Future<HttpClientResponse> _httpGetBytes(Uri uri) async {
87 HttpClientRequest request = await new HttpClient().getUrl(uri); 94 HttpClientRequest request = await new HttpClient().getUrl(uri);
88 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*"); 95 request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*");
89 return request.close(); 96 return request.close();
90 } 97 }
98
99 void _throwIfFailed(HttpClientResponse response, Uri uri) {
100 var statusCode = response.statusCode;
101 if (statusCode < HttpStatus.OK || statusCode > HttpStatus.NO_CONTENT) {
102 throw new HttpException(response.reasonPhrase, uri: uri);
103 }
104 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698