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

Unified Diff: utils/pub/io.dart

Issue 10628016: Add a Pub source for pub.dartlang.org. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 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 side-by-side diff with in-line comments
Download patch
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);
+}
« no previous file with comments | « utils/pub/entrypoint.dart ('k') | utils/pub/pub.dart » ('j') | utils/pub/repo_source.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698