Skip to content

Commit ec05bf8

Browse files
kevmooCommit Queue
authored andcommitted
http_impl: general cleanup
Removed a lot of assumptions about `null` Removed unused fields Change-Id: Ib82b47b10dcf0a843bd9a1a0f67b70a40e396ad9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429825 Auto-Submit: Kevin Moore <[email protected]> Commit-Queue: Kevin Moore <[email protected]> Reviewed-by: Brian Quinlan <[email protected]>
1 parent 9b58e45 commit ec05bf8

File tree

4 files changed

+53
-121
lines changed

4 files changed

+53
-121
lines changed

sdk/lib/_http/http.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ abstract interface class HttpClient {
12661266
if (enabled != _enableTimelineLogging) {
12671267
if (!const bool.fromEnvironment("dart.vm.product")) {
12681268
postEvent('HttpTimelineLoggingStateChange', {
1269-
'isolateId': Service.getIsolateID(Isolate.current),
1269+
'isolateId': Service.getIsolateId(Isolate.current),
12701270
'enabled': enabled,
12711271
});
12721272
}

sdk/lib/_http/http_headers.dart

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -693,16 +693,10 @@ class _HttpHeaders implements HttpHeaders {
693693

694694
class _HeaderValue implements HeaderValue {
695695
String _value;
696-
Map<String, String?>? _parameters;
696+
final Map<String, String?> _parameters;
697697
Map<String, String?>? _unmodifiableParameters;
698698

699-
_HeaderValue([this._value = "", Map<String, String?> parameters = const {}]) {
700-
// TODO(40614): Remove once non-nullability is sound.
701-
Map<String, String?>? nullableParameters = parameters;
702-
if (nullableParameters != null && nullableParameters.isNotEmpty) {
703-
_parameters = HashMap<String, String?>.from(nullableParameters);
704-
}
705-
}
699+
_HeaderValue([this._value = "", this._parameters = const {}]);
706700

707701
static _HeaderValue parse(
708702
String value, {
@@ -711,18 +705,15 @@ class _HeaderValue implements HeaderValue {
711705
bool preserveBackslash = false,
712706
}) {
713707
// Parse the string.
714-
var result = _HeaderValue();
708+
var result = _HeaderValue('', {});
715709
result._parse(value, parameterSeparator, valueSeparator, preserveBackslash);
716710
return result;
717711
}
718712

719713
String get value => _value;
720714

721-
Map<String, String?> _ensureParameters() =>
722-
_parameters ??= <String, String?>{};
723-
724715
Map<String, String?> get parameters =>
725-
_unmodifiableParameters ??= UnmodifiableMapView(_ensureParameters());
716+
_unmodifiableParameters ??= UnmodifiableMapView(_parameters);
726717

727718
static bool _isToken(String token) {
728719
if (token.isEmpty) {
@@ -741,36 +732,33 @@ class _HeaderValue implements HeaderValue {
741732
String toString() {
742733
StringBuffer sb = StringBuffer();
743734
sb.write(_value);
744-
var parameters = _parameters;
745-
if (parameters != null && parameters.isNotEmpty) {
746-
parameters.forEach((String name, String? value) {
747-
sb
748-
..write("; ")
749-
..write(name);
750-
if (value != null) {
751-
sb.write("=");
752-
if (_isToken(value)) {
753-
sb.write(value);
754-
} else {
755-
sb.write('"');
756-
int start = 0;
757-
for (int i = 0; i < value.length; i++) {
758-
// Can use codeUnitAt here instead.
759-
int codeUnit = value.codeUnitAt(i);
760-
if (codeUnit == 92 /* backslash */ ||
761-
codeUnit == 34 /* double quote */ ) {
762-
sb.write(value.substring(start, i));
763-
sb.write(r'\');
764-
start = i;
765-
}
735+
_parameters.forEach((String name, String? value) {
736+
sb
737+
..write("; ")
738+
..write(name);
739+
if (value != null) {
740+
sb.write("=");
741+
if (_isToken(value)) {
742+
sb.write(value);
743+
} else {
744+
sb.write('"');
745+
int start = 0;
746+
for (int i = 0; i < value.length; i++) {
747+
// Can use codeUnitAt here instead.
748+
int codeUnit = value.codeUnitAt(i);
749+
if (codeUnit == 92 /* backslash */ ||
750+
codeUnit == 34 /* double quote */ ) {
751+
sb.write(value.substring(start, i));
752+
sb.write(r'\');
753+
start = i;
766754
}
767-
sb
768-
..write(value.substring(start))
769-
..write('"');
770755
}
756+
sb
757+
..write(value.substring(start))
758+
..write('"');
771759
}
772-
});
773-
}
760+
}
761+
});
774762
return sb.toString();
775763
}
776764

@@ -821,8 +809,6 @@ class _HeaderValue implements HeaderValue {
821809
}
822810

823811
void parseParameters() {
824-
var parameters = _ensureParameters();
825-
826812
String parseParameterName() {
827813
int start = index;
828814
while (!done()) {
@@ -880,10 +866,10 @@ class _HeaderValue implements HeaderValue {
880866
// Charset parameter of ContentTypes are always lower-case.
881867
value = value.toLowerCase();
882868
}
883-
parameters[name] = value;
869+
_parameters[name] = value;
884870
skipWS();
885871
} else if (name.isNotEmpty) {
886-
parameters[name] = null;
872+
_parameters[name] = null;
887873
}
888874
if (done()) return;
889875
// TODO: Implement support for multi-valued parameters.
@@ -907,36 +893,32 @@ class _ContentType extends _HeaderValue implements ContentType {
907893
String _subType = "";
908894

909895
_ContentType(
910-
String primaryType,
911-
String subType,
896+
this._primaryType,
897+
this._subType,
912898
String? charset,
913899
Map<String, String?> parameters,
914-
) : _primaryType = primaryType,
915-
_subType = subType,
916-
super("") {
917-
// TODO(40614): Remove once non-nullability is sound.
918-
String emptyIfNull(String? string) => string ?? "";
919-
_primaryType = emptyIfNull(_primaryType);
920-
_subType = emptyIfNull(_subType);
921-
_value = "$_primaryType/$_subType";
922-
// TODO(40614): Remove once non-nullability is sound.
923-
Map<String, String?>? nullableParameters = parameters;
924-
if (nullableParameters != null) {
925-
var parameterMap = _ensureParameters();
926-
nullableParameters.forEach((String key, String? value) {
927-
String lowerCaseKey = key.toLowerCase();
928-
if (lowerCaseKey == "charset") {
929-
value = value?.toLowerCase();
930-
}
931-
parameterMap[lowerCaseKey] = value;
932-
});
933-
}
900+
) : super("$_primaryType/$_subType", _createParams(parameters, charset));
901+
902+
static Map<String, String?> _createParams(
903+
Map<String, String?> parameters,
904+
String? charset,
905+
) {
906+
var result = <String, String?>{};
907+
parameters.forEach((String key, String? value) {
908+
String lowerCaseKey = key.toLowerCase();
909+
if (lowerCaseKey == "charset") {
910+
value = value?.toLowerCase();
911+
}
912+
result[lowerCaseKey] = value;
913+
});
934914
if (charset != null) {
935-
_ensureParameters()["charset"] = charset.toLowerCase();
915+
result["charset"] = charset.toLowerCase();
936916
}
917+
918+
return result;
937919
}
938920

939-
_ContentType._();
921+
_ContentType._() : super('', {});
940922

941923
static _ContentType parse(String value) {
942924
var result = _ContentType._();
@@ -1145,7 +1127,6 @@ class _Cookie implements Cookie {
11451127
"{",
11461128
"}",
11471129
];
1148-
if (newName == null) throw ArgumentError.notNull("name");
11491130
for (int i = 0; i < newName.length; i++) {
11501131
int codeUnit = newName.codeUnitAt(i);
11511132
if (codeUnit <= 32 ||
@@ -1162,7 +1143,6 @@ class _Cookie implements Cookie {
11621143
}
11631144

11641145
static String _validateValue(String newValue) {
1165-
if (newValue == null) throw ArgumentError.notNull("value");
11661146
// Per RFC 6265, consider surrounding "" as part of the value, but otherwise
11671147
// double quotes are not allowed.
11681148
int start = 0;

sdk/lib/_http/http_impl.dart

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
part of dart._http;
66

77
abstract final class HttpProfiler {
8-
static const _kType = 'HttpProfile';
9-
108
static final Map<String, _HttpProfileData> _profile = {};
119

1210
static _HttpProfileData? startRequest(
@@ -286,17 +284,6 @@ mixin _ServiceObject {
286284
if (__serviceId == 0) __serviceId = _nextServiceId++;
287285
return __serviceId;
288286
}
289-
290-
String get _servicePath => "$_serviceTypePath/$_serviceId";
291-
292-
String get _serviceTypePath;
293-
294-
String get _serviceTypeName;
295-
296-
String _serviceType(bool ref) {
297-
if (ref) return "@$_serviceTypeName";
298-
return _serviceTypeName;
299-
}
300287
}
301288

302289
class _CopyingBytesBuilder implements BytesBuilder {
@@ -2334,7 +2321,7 @@ class _HttpClientConnection {
23342321
proxyCreds.authorize(request);
23352322
}
23362323
}
2337-
if (uri.userInfo != null && uri.userInfo.isNotEmpty) {
2324+
if (uri.userInfo.isNotEmpty) {
23382325
// If the URL contains user information use that for basic
23392326
// authorization.
23402327
String auth = base64Encode(utf8.encode(uri.userInfo));
@@ -2952,27 +2939,6 @@ class _HttpClient implements HttpClient {
29522939

29532940
set findProxy(String Function(Uri uri)? f) => _findProxy = f;
29542941

2955-
static void _startRequestTimelineEvent(
2956-
TimelineTask? timeline,
2957-
String method,
2958-
Uri uri,
2959-
) {
2960-
timeline?.start(
2961-
'HTTP CLIENT ${method.toUpperCase()}',
2962-
arguments: {'method': method.toUpperCase(), 'uri': uri.toString()},
2963-
);
2964-
}
2965-
2966-
bool _isLoopback(String host) {
2967-
if (host.isEmpty) return false;
2968-
if ("localhost" == host) return true;
2969-
try {
2970-
return InternetAddress(host).isLoopback;
2971-
} on ArgumentError {
2972-
return false;
2973-
}
2974-
}
2975-
29762942
bool _isValidToken(String token) {
29772943
checkNotNullable(token, "token");
29782944
// from https://www.rfc-editor.org/rfc/rfc2616#page-15
@@ -3461,10 +3427,6 @@ final class _HttpConnection extends LinkedListEntry<_HttpConnection>
34613427
bool get _isActive => _state == _ACTIVE;
34623428
bool get _isIdle => _state == _IDLE;
34633429
bool get _isClosing => _state == _CLOSING;
3464-
bool get _isDetached => _state == _DETACHED;
3465-
3466-
String get _serviceTypePath => 'io/http/serverconnections';
3467-
String get _serviceTypeName => 'HttpServerConnection';
34683430
}
34693431

34703432
// Common interface of [ServerSocket] and [SecureServerSocket] used by
@@ -3702,9 +3664,6 @@ class _HttpServer extends Stream<HttpRequest>
37023664
return result;
37033665
}
37043666

3705-
String get _serviceTypePath => 'io/http/servers';
3706-
String get _serviceTypeName => 'HttpServer';
3707-
37083667
_HttpSessionManager? _sessionManagerInstance;
37093668

37103669
// Indicated if the http server has been closed.
@@ -3726,9 +3685,6 @@ class _ProxyConfiguration {
37263685
static const String DIRECT_PREFIX = "DIRECT";
37273686

37283687
_ProxyConfiguration(String configuration) : proxies = <_Proxy>[] {
3729-
if (configuration == null) {
3730-
throw HttpException("Invalid proxy configuration $configuration");
3731-
}
37323688
List<String> list = configuration.split(";");
37333689
for (var proxy in list) {
37343690
proxy = proxy.trim();
@@ -3811,7 +3767,6 @@ class _HttpConnectionInfo implements HttpConnectionInfo {
38113767
_HttpConnectionInfo(this.remoteAddress, this.remotePort, this.localPort);
38123768

38133769
static _HttpConnectionInfo? create(Socket socket) {
3814-
if (socket == null) return null;
38153770
try {
38163771
return _HttpConnectionInfo(
38173772
socket.remoteAddress,

sdk/lib/_http/websocket_impl.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket {
10921092
return (customClient ?? _httpClient)
10931093
.openUrl("GET", uri)
10941094
.then((request) {
1095-
if (uri.userInfo != null && uri.userInfo.isNotEmpty) {
1095+
if (uri.userInfo.isNotEmpty) {
10961096
// If the URL contains user information use that for basic
10971097
// authorization.
10981098
String auth = base64Encode(utf8.encode(uri.userInfo));
@@ -1402,9 +1402,6 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket {
14021402
_webSockets.remove(_serviceId);
14031403
}
14041404

1405-
String get _serviceTypePath => 'io/websockets';
1406-
String get _serviceTypeName => 'WebSocket';
1407-
14081405
static bool _isReservedStatusCode(int? code) {
14091406
return code != null &&
14101407
(code < WebSocketStatus.normalClosure ||

0 commit comments

Comments
 (0)