observeHttpClient function
A dart:io HttpClient whose connections funnel through the pure
observer (connObserverNote). Debug-only: the factory override
re-implements the connect leg (Socket.startConnect, TLS via
SecureSocket.startConnect); proxy connections are traced at the proxy
endpoint. Only ever installed when the trace is ON.
Implementation
HttpClient observeHttpClient() {
final client = HttpClient();
client.connectionFactory = (uri, proxyHost, proxyPort) async {
final host = proxyHost ?? uri.host;
final fallbackPort = uri.scheme == 'https' ? 443 : 80;
final port = proxyPort ?? (uri.port != 0 ? uri.port : fallbackPort);
final secure = uri.scheme == 'https' && proxyHost == null;
final watch = Stopwatch()..start();
final task = secure
? await SecureSocket.startConnect(host, port)
: await Socket.startConnect(host, port);
unawaited(
task.socket.then(
(socket) =>
connObserverNote(port: socket.port, openedAfter: watch.elapsed),
onError: (Object _) {},
),
);
return task;
};
return client;
}