Skip to content

Commit bdf0f64

Browse files
kevmooCommit Queue
authored andcommitted
[io] http_impl cleanup
Turned _AuthenticationScheme into an enum Made a few fields in _Credentials final Test=no behavior change Change-Id: I14f31182d3434aaf0d7292c932b8da278f13d57b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419926 Auto-Submit: Kevin Moore <[email protected]> Commit-Queue: Kevin Moore <[email protected]> Reviewed-by: Brian Quinlan <[email protected]>
1 parent 174acd4 commit bdf0f64

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

sdk/lib/_http/http_impl.dart

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,13 @@ class _HttpClientResponse extends _HttpInboundMessageListInt
874874
// For basic authentication don't retry already used credentials
875875
// as they must have already been added to the request causing
876876
// this authenticate response.
877-
if (cr.scheme == _AuthenticationScheme.BASIC && !cr.used) {
877+
if (cr.scheme == _AuthenticationScheme.Basic && !cr.used) {
878878
// Credentials were found, prepare for retrying the request.
879879
return retry();
880880
}
881881

882882
// Digest authentication only supports the MD5 algorithm.
883-
if (cr.scheme == _AuthenticationScheme.DIGEST) {
883+
if (cr.scheme == _AuthenticationScheme.Digest) {
884884
var algorithm = header.parameters["algorithm"];
885885
if (algorithm == null || algorithm.toLowerCase() == "md5") {
886886
var nonce = cr.nonce;
@@ -2390,7 +2390,7 @@ class _HttpClientConnection {
23902390
// requests the client to start using a new nonce for proxy
23912391
// authentication.
23922392
if (proxyCreds != null &&
2393-
proxyCreds.scheme == _AuthenticationScheme.DIGEST) {
2393+
proxyCreds.scheme == _AuthenticationScheme.Digest) {
23942394
var authInfo = incoming.headers["proxy-authentication-info"];
23952395
if (authInfo != null && authInfo.length == 1) {
23962396
var header = _HeaderValue.parse(
@@ -2403,7 +2403,7 @@ class _HttpClientConnection {
24032403
}
24042404
// For digest authentication check if the server requests the
24052405
// client to start using a new nonce.
2406-
if (creds != null && creds.scheme == _AuthenticationScheme.DIGEST) {
2406+
if (creds != null && creds.scheme == _AuthenticationScheme.Digest) {
24072407
var authInfo = incoming.headers["authentication-info"];
24082408
if (authInfo != null && authInfo.length == 1) {
24092409
var header = _HeaderValue.parse(
@@ -3909,45 +3909,37 @@ class _DetachedSocket extends Stream<Uint8List> implements Socket {
39093909
}
39103910
}
39113911

3912-
class _AuthenticationScheme {
3913-
final int _scheme;
3914-
3915-
static const UNKNOWN = _AuthenticationScheme(-1);
3916-
static const BASIC = _AuthenticationScheme(0);
3917-
static const BEARER = _AuthenticationScheme(1);
3918-
static const DIGEST = _AuthenticationScheme(2);
3919-
3920-
const _AuthenticationScheme(this._scheme);
3912+
enum _AuthenticationScheme {
3913+
Unknown(),
3914+
Basic(),
3915+
Bearer(),
3916+
Digest();
39213917

39223918
factory _AuthenticationScheme.fromString(String scheme) {
3923-
if (scheme.toLowerCase() == "basic") return BASIC;
3924-
if (scheme.toLowerCase() == "bearer") return BEARER;
3925-
if (scheme.toLowerCase() == "digest") return DIGEST;
3926-
return UNKNOWN;
3919+
final lower = scheme.toLowerCase();
3920+
return _AuthenticationScheme.values
3921+
.where((e) => e.name.toLowerCase() == lower)
3922+
.singleOrNull ??
3923+
Unknown;
39273924
}
39283925

3929-
String toString() {
3930-
if (this == BASIC) return "Basic";
3931-
if (this == BEARER) return "Bearer";
3932-
if (this == DIGEST) return "Digest";
3933-
return "Unknown";
3934-
}
3926+
String toString() => name;
39353927
}
39363928

39373929
abstract class _Credentials {
3938-
_HttpClientCredentials credentials;
3939-
String realm;
3930+
final _HttpClientCredentials credentials;
3931+
final String realm;
39403932
bool used = false;
39413933

39423934
// Digest specific fields.
3943-
String? ha1;
3935+
late final String? ha1;
39443936
String? nonce;
39453937
String? algorithm;
39463938
String? qop;
39473939
int? nonceCount;
39483940

39493941
_Credentials(this.credentials, this.realm) {
3950-
if (credentials.scheme == _AuthenticationScheme.DIGEST) {
3942+
if (credentials.scheme == _AuthenticationScheme.Digest) {
39513943
// Calculate the H(A1) value once. There is no mentioning of
39523944
// username/password encoding in RFC 2617. However there is an
39533945
// open draft for adding an additional accept-charset parameter to
@@ -3990,7 +3982,7 @@ class _SiteCredentials extends _Credentials {
39903982
void authorize(HttpClientRequest request) {
39913983
// Digest credentials cannot be used without a nonce from the
39923984
// server.
3993-
if (credentials.scheme == _AuthenticationScheme.DIGEST && nonce == null) {
3985+
if (credentials.scheme == _AuthenticationScheme.Digest && nonce == null) {
39943986
return;
39953987
}
39963988
credentials.authorize(this, request as _HttpClientRequest);
@@ -4013,7 +4005,7 @@ class _ProxyCredentials extends _Credentials {
40134005
void authorize(HttpClientRequest request) {
40144006
// Digest credentials cannot be used without a nonce from the
40154007
// server.
4016-
if (credentials.scheme == _AuthenticationScheme.DIGEST && nonce == null) {
4008+
if (credentials.scheme == _AuthenticationScheme.Digest && nonce == null) {
40174009
return;
40184010
}
40194011
credentials.authorizeProxy(this, request as _HttpClientRequest);
@@ -4033,7 +4025,7 @@ final class _HttpClientBasicCredentials extends _HttpClientCredentials
40334025

40344026
_HttpClientBasicCredentials(this.username, this.password);
40354027

4036-
_AuthenticationScheme get scheme => _AuthenticationScheme.BASIC;
4028+
_AuthenticationScheme get scheme => _AuthenticationScheme.Basic;
40374029

40384030
String authorization() {
40394031
// There is no mentioning of username/password encoding in RFC
@@ -4067,7 +4059,7 @@ final class _HttpClientBearerCredentials extends _HttpClientCredentials
40674059
}
40684060
}
40694061

4070-
_AuthenticationScheme get scheme => _AuthenticationScheme.BEARER;
4062+
_AuthenticationScheme get scheme => _AuthenticationScheme.Bearer;
40714063

40724064
String authorization() {
40734065
return "Bearer $token";
@@ -4089,7 +4081,7 @@ final class _HttpClientDigestCredentials extends _HttpClientCredentials
40894081

40904082
_HttpClientDigestCredentials(this.username, this.password);
40914083

4092-
_AuthenticationScheme get scheme => _AuthenticationScheme.DIGEST;
4084+
_AuthenticationScheme get scheme => _AuthenticationScheme.Digest;
40934085

40944086
String authorization(_Credentials credentials, _HttpClientRequest request) {
40954087
String requestUri = request._requestUri();

0 commit comments

Comments
 (0)