Bind to the correct address for
diff --git a/lib/http_multi_server.dart b/lib/http_multi_server.dart index bd8a6a7..1a8be3b 100644 --- a/lib/http_multi_server.dart +++ b/lib/http_multi_server.dart
@@ -133,21 +133,31 @@ /// Bind an [HttpServer] with handling for special addresses 'localhost' and /// 'any'. /// - /// For address 'localhost' behaves like [loopback]. For 'any' listens on - /// [InternetAddress.anyIPv6] which listens on all hostnames for both IPv4 and - /// IPV6. For any other address forwards directly to `HttpServer.bind` where + /// For address 'localhost' behaves like [loopback]. + /// + /// For 'any' listens on [InternetAddress.anyIPv6] if the system supports IPv6 + /// otherwise [InternetAddress.anyIPv4]. Note [InternetAddress.anyIPv6] + /// listens on all hostnames for both IPv4 and IPV6. + /// + /// For any other address forwards directly to `HttpServer.bind` where /// the IPvX support may vary. /// /// See [HttpServer.bind]. static Future<HttpServer> bind(dynamic address, int port, - {int backlog = 0, bool v6Only = false, bool shared = false}) { + {int backlog = 0, bool v6Only = false, bool shared = false}) async { if (address == 'localhost') { return HttpMultiServer.loopback(port, backlog: backlog, v6Only: v6Only, shared: shared); } if (address == 'any') { - return HttpServer.bind(InternetAddress.anyIPv6, port, - backlog: backlog, v6Only: v6Only, shared: shared); + return HttpServer.bind( + await supportsIPv6 + ? InternetAddress.anyIPv6 + : InternetAddress.anyIPv4, + port, + backlog: backlog, + v6Only: v6Only, + shared: shared); } return HttpServer.bind(address, port, backlog: backlog, v6Only: v6Only, shared: shared);
diff --git a/test/http_multi_server_test.dart b/test/http_multi_server_test.dart index 749980e..95f0b40 100644 --- a/test/http_multi_server_test.dart +++ b/test/http_multi_server_test.dart
@@ -219,6 +219,18 @@ } }); + test("uses the correct server address for 'any'", () async { + final server = await HttpMultiServer.bind('any', 0); + + if (await supportsIPv4) { + expect(server.address, InternetAddress.anyIPv4); + } + + if (await supportsIPv6) { + expect(server.address, InternetAddress.anyIPv6); + } + }); + test('listens on specified hostname', () async { if (!await supportsIPv4) return; final server = await HttpMultiServer.bind(InternetAddress.anyIPv4, 0);