Remove dependency on http package BUG= Review URL: https://codereview.appspot.com/243950044
diff --git a/pkgs/package_config/lib/discovery.dart b/pkgs/package_config/lib/discovery.dart index f8e45d4..0dd7f55 100644 --- a/pkgs/package_config/lib/discovery.dart +++ b/pkgs/package_config/lib/discovery.dart
@@ -5,9 +5,11 @@ library package_config.discovery; import "dart:async"; -import "dart:io" show Directory, File, FileSystemEntity; +import "dart:io"; +import "dart:typed_data" show Uint8List; + import "package:path/path.dart" as path; -import "package:http/http.dart" as http; + import "packages.dart"; import "packages_file.dart" as pkgfile show parse; import "src/packages_impl.dart"; @@ -39,8 +41,7 @@ /// It must return the *contents* of the file identified by the URI it's given, /// which should be a UTF-8 encoded `.packages` file, and must return an /// error future if loading fails for any reason. -Future<Packages> findPackages( - Uri baseUri, +Future<Packages> findPackages(Uri baseUri, {Future<List<int>> loader(Uri unsupportedUri)}) { if (baseUri.scheme == "file") { return new Future<Packages>.sync(() => findPackagesFromFile(baseUri)); @@ -113,8 +114,8 @@ if (location == null) return Packages.noPackages; if (location is File) { List<int> fileBytes = location.readAsBytesSync(); - Map<String, Uri> map = pkgfile.parse(fileBytes, - new Uri.file(location.path)); + Map<String, Uri> map = + pkgfile.parse(fileBytes, new Uri.file(location.path)); return new MapPackages(map); } assert(location is Directory); @@ -139,7 +140,7 @@ /// of the requested `.packages` file as bytes, which will be assumed to be /// UTF-8 encoded. Future<Packages> findPackagesFromNonFile(Uri nonFileUri, - {Future<List<int>> loader(Uri name)}) { + {Future<List<int>> loader(Uri name)}) { if (loader == null) loader = _httpGet; Uri packagesFileUri = nonFileUri.resolve(".packages"); return loader(packagesFileUri).then((List<int> fileBytes) { @@ -152,10 +153,29 @@ }); } -/// Fetches a file using the http library. +/// Fetches a file over http. Future<List<int>> _httpGet(Uri uri) { - return http.get(uri).then((http.Response response) { - if (response.statusCode == 200) return response.bodyBytes; - throw 0; // The error message isn't being used for anything. + HttpClient client = new HttpClient(); + return client + .getUrl(uri) + .then((HttpClientRequest request) => request.close()) + .then((HttpClientResponse response) { + if (response.statusCode != HttpStatus.OK) { + String msg = 'Failure getting $uri: ' + '${response.statusCode} ${response.reasonPhrase}'; + throw msg; + } + return response.toList(); + }).then((List<List<int>> splitContent) { + int totalLength = splitContent.fold(0, (int old, List list) { + return old + list.length; + }); + Uint8List result = new Uint8List(totalLength); + int offset = 0; + for (List<int> contentPart in splitContent) { + result.setRange(offset, offset + contentPart.length, contentPart); + offset += contentPart.length; + } + return result; }); }
diff --git a/pkgs/package_config/pubspec.lock b/pkgs/package_config/pubspec.lock index 708d891..5cae005 100644 --- a/pkgs/package_config/pubspec.lock +++ b/pkgs/package_config/pubspec.lock
@@ -25,10 +25,6 @@ description: crypto source: hosted version: "0.9.0" - http: - description: http - source: hosted - version: "0.11.2" http_parser: description: http_parser source: hosted
diff --git a/pkgs/package_config/pubspec.yaml b/pkgs/package_config/pubspec.yaml index 338aab5..5eca330 100644 --- a/pkgs/package_config/pubspec.yaml +++ b/pkgs/package_config/pubspec.yaml
@@ -1,5 +1,5 @@ name: package_config -version: 0.0.3 +version: 0.0.3+1 description: Support for working with Package Resolution config files. author: Dart Team <misc@dartlang.org> homepage: https://github.com/dart-lang/package_config @@ -9,7 +9,6 @@ dependencies: charcode: '^1.1.0' - http: '^0.11.0' path: '>=1.0.0 <2.0.0' dev_dependencies: