mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:45:06 +00:00
33e174084a
Use `hasScheme` in place of comparing against the empty string, and `isScheme` to compare against all other schemes. TEST=No behavior changes. Change-Id: Ifc9fd13c6cf37933ebd4a754c4b500dedbcb291b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/231185 Reviewed-by: Kevin Moore <kevmoo@google.com> Commit-Queue: Nate Bosch <nbosch@google.com>
80 lines
2.3 KiB
Dart
80 lines
2.3 KiB
Dart
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'dart:async';
|
|
import 'dart:io' as io;
|
|
|
|
import 'package:front_end/src/api_unstable/vm.dart';
|
|
|
|
class HttpAwareFileSystem implements FileSystem {
|
|
FileSystem original;
|
|
|
|
HttpAwareFileSystem(this.original);
|
|
|
|
@override
|
|
FileSystemEntity entityForUri(Uri uri) {
|
|
if (uri.isScheme('http') || uri.isScheme('https')) {
|
|
return new HttpFileSystemEntity(this, uri);
|
|
} else {
|
|
return original.entityForUri(uri);
|
|
}
|
|
}
|
|
}
|
|
|
|
class HttpFileSystemEntity implements FileSystemEntity {
|
|
HttpAwareFileSystem fileSystem;
|
|
Uri uri;
|
|
|
|
HttpFileSystemEntity(this.fileSystem, this.uri);
|
|
|
|
@override
|
|
Future<bool> exists() async {
|
|
return connectAndRun((io.HttpClient httpClient) async {
|
|
io.HttpClientRequest request = await httpClient.headUrl(uri);
|
|
io.HttpClientResponse response = await request.close();
|
|
await response.drain();
|
|
return response.statusCode == io.HttpStatus.ok;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<bool> existsAsyncIfPossible() => exists();
|
|
|
|
@override
|
|
Future<List<int>> readAsBytes() async {
|
|
return connectAndRun((io.HttpClient httpClient) async {
|
|
io.HttpClientRequest request = await httpClient.getUrl(uri);
|
|
io.HttpClientResponse response = await request.close();
|
|
if (response.statusCode != io.HttpStatus.ok) {
|
|
await response.drain();
|
|
throw new FileSystemException(uri, response.toString());
|
|
}
|
|
List<List<int>> list = await response.toList();
|
|
return list.expand((list) => list).toList();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
|
|
|
|
@override
|
|
Future<String> readAsString() async {
|
|
return String.fromCharCodes(await readAsBytes());
|
|
}
|
|
|
|
Future<T> connectAndRun<T>(Future<T> body(io.HttpClient httpClient)) async {
|
|
io.HttpClient? httpClient;
|
|
try {
|
|
httpClient = new io.HttpClient();
|
|
// Set timeout to be shorter than anticipated OS default
|
|
httpClient.connectionTimeout = const Duration(seconds: 5);
|
|
return await body(httpClient);
|
|
} on Exception catch (e) {
|
|
throw new FileSystemException(uri, e.toString());
|
|
} finally {
|
|
httpClient?.close(force: true);
|
|
}
|
|
}
|
|
}
|