Change signature of HttpClient.authenticate{,Proxy}.

Allow the `realm` argument to callbacks to be `null`.
This is a breaking change, but it only affects null-safe code.

Also fix a bug in http_impl.dart where an error function with
an *optional* stack trace parameter would be called without a stack trace.

Bug: https://github.com/dart-lang/sdk/issues/44039
Change-Id: I4b38382328e26478661bf45f46cd3017631f4ebd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170094
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2021-05-20 13:13:42 +00:00 committed by commit-bot@chromium.org
parent f18c1bfb84
commit cf627eb01c
6 changed files with 43 additions and 31 deletions

View file

@ -14,6 +14,12 @@
daylight saving changes that are not precisely one hour.
(No change on the Web which uses the JavaScript `Date` object.)
#### `dart:io`
* BREAKING CHANGE (for pre-migrated null safe code):
`HttpClient`'s `.authenticate` and `.authenticateProxy` setter callbacks
must now accept a nullable `realm` argument.
#### `dart:typed_data`
* **BREAKING CHANGE** (https://github.com/dart-lang/sdk/issues/45115)

View file

@ -1721,8 +1721,10 @@ abstract class HttpClient {
/**
* Sets the function to be called when a site is requesting
* authentication. The URL requested and the security realm from the
* server are passed in the arguments [url] and [realm].
* authentication.
*
* The URL requested, the authentication scheme and the security realm
* from the server are passed in the arguments [url], [scheme] and [realm].
*
* The function returns a [Future] which should complete when the
* authentication has been resolved. If credentials cannot be
@ -1742,7 +1744,7 @@ abstract class HttpClient {
* of a failed request, or issues due to missing request payload on retried
* request.
*/
void set authenticate(Future<bool> f(Uri url, String scheme, String realm)?);
void set authenticate(Future<bool> f(Uri url, String scheme, String? realm)?);
/**
* Add credentials to be used for authorizing HTTP requests.
@ -1838,9 +1840,11 @@ abstract class HttpClient {
/**
* Sets the function to be called when a proxy is requesting
* authentication. Information on the proxy in use and the security
* realm for the authentication are passed in the arguments [host],
* [port] and [realm].
* authentication.
*
* Information on the proxy in use, the authentication scheme
* and the security realm for the authentication
* are passed in the arguments [host], [port], [scheme] and [realm].
*
* The function returns a [Future] which should complete when the
* authentication has been resolved. If credentials cannot be
@ -1854,7 +1858,7 @@ abstract class HttpClient {
* continue normally.
*/
void set authenticateProxy(
Future<bool> f(String host, int port, String scheme, String realm)?);
Future<bool> f(String host, int port, String scheme, String? realm)?);
/**
* Add credentials to be used for authorizing HTTP proxies.

View file

@ -705,11 +705,11 @@ class _HttpClientResponse extends _HttpInboundMessageListInt
if (onError == null) {
return;
}
if (onError is void Function(Object)) {
onError(e);
} else {
assert(onError is void Function(Object, StackTrace));
if (onError is void Function(Object, StackTrace)) {
onError(e, st);
} else {
assert(onError is void Function(Object));
onError(e);
}
}, onDone: () {
_profileData?.finishResponse();
@ -784,15 +784,16 @@ class _HttpClientResponse extends _HttpInboundMessageListInt
return new Future.value(false);
}
var proxy = _httpRequest._proxy;
return authenticateProxy(
proxy.host, proxy.port, scheme.toString(), realm);
} else {
var authenticate = _httpClient._authenticate;
if (authenticate == null) {
return new Future.value(false);
if (!proxy.isDirect) {
return authenticateProxy(
proxy.host!, proxy.port!, scheme.toString(), realm);
}
return authenticate(_httpRequest.uri, scheme.toString(), realm);
}
var authenticate = _httpClient._authenticate;
if (authenticate == null) {
return new Future.value(false);
}
return authenticate(_httpRequest.uri, scheme.toString(), realm);
}
List<String> challenge = authChallenge()!;
@ -2499,9 +2500,10 @@ class _HttpClient implements HttpClient {
final List<_Credentials> _credentials = [];
final List<_ProxyCredentials> _proxyCredentials = [];
final SecurityContext? _context;
Function? _authenticate;
Function? _authenticateProxy;
Function? _findProxy = HttpClient.findProxyFromEnvironment;
Future<bool> Function(Uri, String scheme, String? realm)? _authenticate;
Future<bool> Function(String host, int port, String scheme, String? realm)?
_authenticateProxy;
String Function(Uri)? _findProxy = HttpClient.findProxyFromEnvironment;
Duration _idleTimeout = const Duration(seconds: 15);
BadCertificateCallback? _badCertificateCallback;
@ -2600,7 +2602,7 @@ class _HttpClient implements HttpClient {
!force || !_connectionTargets.values.any((s) => s._active.isNotEmpty));
}
set authenticate(Future<bool> f(Uri url, String scheme, String realm)?) {
set authenticate(Future<bool> f(Uri url, String scheme, String? realm)?) {
_authenticate = f;
}
@ -2610,7 +2612,7 @@ class _HttpClient implements HttpClient {
}
set authenticateProxy(
Future<bool> f(String host, int port, String scheme, String realm)?) {
Future<bool> f(String host, int port, String scheme, String? realm)?) {
_authenticateProxy = f;
}

View file

@ -207,7 +207,7 @@ void testAuthenticateCallback(String? algorithm, String? qop) {
Server.start(algorithm, qop).then((server) {
HttpClient client = new HttpClient();
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (url, scheme, realm) {
Expect.equals("Digest", scheme);
Expect.equals("test", realm);
final completer = new Completer<bool>();
@ -348,7 +348,7 @@ void testLocalServerDigest() {
client.addCredentials(Uri.parse("http://127.0.0.1/digest"), "test",
new HttpClientDigestCredentials("dart", "password"));
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (url, scheme, realm) {
client.addCredentials(Uri.parse("http://127.0.0.1/digest"), "test",
new HttpClientDigestCredentials("dart", "password"));
return new Future.value(true);

View file

@ -156,7 +156,7 @@ void testBasicAuthenticateCallback() {
HttpClient client = new HttpClient();
bool passwordChanged = false;
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (Uri url, String scheme, String? realm) {
Expect.equals("Basic", scheme);
Expect.equals("realm", realm);
String username = url.path.substring(1, 6);
@ -165,7 +165,7 @@ void testBasicAuthenticateCallback() {
final completer = new Completer<bool>();
new Timer(const Duration(milliseconds: 10), () {
client.addCredentials(
url, realm, new HttpClientBasicCredentials(username, password));
url, realm!, new HttpClientBasicCredentials(username, password));
completer.complete(true);
});
return completer.future;
@ -208,7 +208,7 @@ void testBasicAuthenticateCallback() {
void testLocalServerBasic() {
HttpClient client = new HttpClient();
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (Uri url, String scheme, String? realm) {
client.addCredentials(Uri.parse("http://127.0.0.1/basic"), "test",
new HttpClientBasicCredentials("test", "test"));
return new Future.value(true);
@ -228,7 +228,7 @@ void testLocalServerBasic() {
void testLocalServerDigest() {
HttpClient client = new HttpClient();
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (Uri url, String scheme, String? realm) {
print("url: $url, scheme: $scheme, realm: $realm");
client.addCredentials(Uri.parse("http://127.0.0.1/digest"), "test",
new HttpClientDigestCredentials("test", "test"));

View file

@ -209,7 +209,7 @@ void testAuthenticateCallback(String algorithm, String qop) {
Server.start(algorithm, qop).then((server) {
HttpClient client = new HttpClient();
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (url, scheme, realm) {
Expect.equals("Digest", scheme);
Expect.equals("test", realm);
Completer completer = new Completer<bool>();
@ -350,7 +350,7 @@ void testLocalServerDigest() {
client.addCredentials(Uri.parse("http://127.0.0.1/digest"), "test",
new HttpClientDigestCredentials("dart", "password"));
client.authenticate = (Uri url, String scheme, String realm) {
client.authenticate = (url, scheme, realm) {
client.addCredentials(Uri.parse("http://127.0.0.1/digest"), "test",
new HttpClientDigestCredentials("dart", "password"));
return new Future.value(true);