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) { |