Chromium Code Reviews| Index: utils/pub/io.dart |
| diff --git a/utils/pub/io.dart b/utils/pub/io.dart |
| index 3f3c9d0a37f469ac7f25a3b1e6261cc5ce9fdc75..cf2aa8dc760167d34802c5d26d0a2f2e08b2a6e7 100644 |
| --- a/utils/pub/io.dart |
| +++ b/utils/pub/io.dart |
| @@ -8,6 +8,7 @@ |
| #library('io'); |
| #import('dart:io'); |
| +#import('dart:uri'); |
| /** Gets the current working directory. */ |
| String get workingDir() => new File('.').fullPathSync(); |
| @@ -263,6 +264,52 @@ Future<File> createSymlink(from, to) { |
| String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| /** |
| + * Opens an input stream for a HTTP GET request to [url], which may be a |
| + * [String] or [Uri]. |
| + */ |
| +InputStream httpGet(url) { |
| + var resultStream = new ListInputStream(); |
| + var connection = new HttpClient().getUrl(_getUri(url)); |
| + |
| + // TODO(nweiz): propagate this error to the return value. See issue 3657. |
| + connection.onError = (e) { throw e; }; |
| + connection.onResponse = (response) { |
| + if (response.statusCode >= 400) { |
| + // TODO(nweiz): propagate this error to the return value. See issue 3657. |
| + throw new Exception( |
| + "HTTP request for $url failed with status ${response.statusCode}"); |
| + return; |
|
Bob Nystrom
2012/06/22 22:35:44
The return is unnecessary.
nweiz
2012/06/25 22:25:17
Done.
|
| + } |
| + |
| + pipeInputToInput(response.inputStream, resultStream); |
| + }; |
| + |
| + return resultStream; |
| +} |
| + |
| +/** |
| + * Takes all input from [source] and writes it to [sink]. |
| + */ |
| +void pipeInputToInput(InputStream source, ListInputStream sink) { |
| + source.onClosed = sink.markEndOfStream; |
| + source.onData = () => sink.write(source.read()); |
| + // TODO(nweiz): propagate this error to the sink. See issue 3657. |
| + source.onError = (e) { throw e; }; |
| +} |
| + |
| +/** |
| + * Buffers all input from an InputStream and returns it as a future. |
| + */ |
| +Future<List<int>> consumeInputStream(InputStream stream) { |
| + var completer = new Completer<List<int>>(); |
| + var buffer = <int>[]; |
| + stream.onClosed = () => completer.complete(buffer); |
| + stream.onData = () => buffer.addAll(stream.read()); |
| + stream.onError = (e) => completer.completeException(e); |
| + return completer.future; |
| +} |
| + |
| +/** |
| * Spawns and runs the process located at [executable], passing in [args]. |
| * Returns a [Future] that will complete the results of the process after it |
| * has ended. |
| @@ -389,3 +436,11 @@ Directory _getDirectory(entry) { |
| if (entry is Directory) return entry; |
| return new Directory(entry); |
| } |
| + |
| +/** |
| + * Gets a [Uri] for [url], which can either already be one, or be a [String]. |
| + */ |
| +Uri _getUri(url) { |
|
Bob Nystrom
2012/06/22 22:35:44
Mixing "uri" and "url" here is a bit confusing. Pi
nweiz
2012/06/25 22:25:17
Done.
|
| + if (url is Uri) return url; |
| + return new Uri.fromString(url); |
| +} |