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

Unified Diff: sdk/lib/_internal/pub/lib/src/utils.dart

Issue 311253005: Bind to all available loopback addresses in pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 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: sdk/lib/_internal/pub/lib/src/utils.dart
diff --git a/sdk/lib/_internal/pub/lib/src/utils.dart b/sdk/lib/_internal/pub/lib/src/utils.dart
index 7903d7faea91a7ec92bd10c7bc5c046ed5c2d06d..1203f97b21affc20188624582360ccdd8045d553 100644
--- a/sdk/lib/_internal/pub/lib/src/utils.dart
+++ b/sdk/lib/_internal/pub/lib/src/utils.dart
@@ -190,6 +190,10 @@ String quoteRegExp(String string) {
///
/// Handles properly formatting IPv6 addresses.
Uri baseUrlForAddress(InternetAddress address, int port) {
+ if (address.isLoopback) {
+ return new Uri(scheme: "http", host: "localhost", port: port);
+ }
+
// IPv6 addresses in URLs need to be enclosed in square brackets to avoid
// URL ambiguity with the ":" in the address.
if (address.type == InternetAddressType.IP_V6) {
@@ -199,6 +203,27 @@ Uri baseUrlForAddress(InternetAddress address, int port) {
return new Uri(scheme: "http", host: address.address, port: port);
}
+/// Returns whether [host] is a host for a localhost or loopback URL.
+///
+/// Unlike [InternetAddress.isLoopback], this hostnames from URLs as well as
+/// from [InternetAddress]es, including "localhost".
+bool isLoopback(String host) {
+ if (host == 'localhost') return true;
+
+ // IPv6 hosts in URLs are surrounded by square brackets.
+ if (host.startsWith("[") && host.endsWith("]")) {
+ host = host.substring(1, host.length - 1);
+ }
+
+ try {
+ return new InternetAddress(host).isLoopback;
+ } on ArgumentError catch (_) {
+ // The host isn't an IP address and isn't "localhost', so it's almost
+ // certainly not a loopback host.
+ return false;
+ }
+}
+
/// Flattens nested lists inside an iterable into a single list containing only
/// non-list elements.
List flatten(Iterable nested) {
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/source/hosted.dart ('k') | sdk/lib/_internal/pub/test/cache/repair/handles_failure_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698