Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.3

* Fix `HttpMultiServer.loopback()` and `.loopbackSecure()` for environments that
don't support IPv4.

## 2.0.2

* Fix a dependency that was incorrectly marked as dev-only.
Expand Down
8 changes: 6 additions & 2 deletions lib/http_multi_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ class HttpMultiServer extends StreamView<HttpRequest> implements HttpServer {
static Future<HttpServer> _loopback(int port,
Future<HttpServer> bind(InternetAddress address, int port),
[int remainingRetries]) async {
if (remainingRetries == null) remainingRetries = 5;
remainingRetries ??= 5;

if (!await supportsIPv4) {
return await bind(InternetAddress.LOOPBACK_IP_V6, port);
}

var v4Server = await bind(InternetAddress.LOOPBACK_IP_V4, port);
if (!await supportsIpV6) return v4Server;
if (!await supportsIPv6) return v4Server;

try {
// Reuse the IPv4 server's port so that if [port] is 0, both servers use
Expand Down
22 changes: 13 additions & 9 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
import 'dart:async';
import 'dart:io';

/// A cache for [supportsIpV6].
bool _supportsIpV6;

/// Returns whether this computer supports binding to IPv6 addresses.
Future<bool> get supportsIpV6 async {
if (_supportsIpV6 != null) return _supportsIpV6;

final Future<bool> supportsIPv6 = () async {
try {
var socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V6, 0);
_supportsIpV6 = true;
socket.close();
return true;
} on SocketException catch (_) {
_supportsIpV6 = false;
return false;
}
}
}();

/// Returns whether this computer supports binding to IPv4 addresses.
final Future<bool> supportsIPv4 = () async {
try {
var socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0);
socket.close();
return true;
} on SocketException catch (_) {
return false;
}
}();
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http_multi_server
version: 2.0.2
version: 2.0.3
author: "Dart Team <[email protected]>"
homepage: http://github.com/dart-lang/http_multi_server
description:
Expand Down
13 changes: 7 additions & 6 deletions test/http_multi_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,21 @@ void main() {

tearDown(() => server.close());

test("listens on all localhost interfaces", () {
test("listens on all localhost interfaces", () async {
server.listen((request) {
request.response.write("got request");
request.response.close();
});

expect(http.read("http://127.0.0.1:${server.port}/"),
completion(equals("got request")));
if (await supportsIPv4) {
expect(http.read("http://127.0.0.1:${server.port}/"),
completion(equals("got request")));
}

return supportsIpV6.then((supportsIpV6) {
if (!supportsIpV6) return;
if (await supportsIPv4) {
expect(http.read("http://[::1]:${server.port}/"),
completion(equals("got request")));
});
}
});
});
}
Expand Down