From 5adeb03c2fbb61dfdd7337a350c6dfe4ba4a486a Mon Sep 17 00:00:00 2001 From: Kallen Tu Date: Thu, 10 Nov 2022 22:28:48 +0000 Subject: [PATCH] Add ServerSocketBase to remove dynamic invocations of secure sockets in _HttpServer. Change-Id: If8379fe73eeabd3aef99fcb85b4a5fd8414f8a18 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260980 Reviewed-by: Lasse Nielsen Reviewed-by: Leaf Petersen --- CHANGELOG.md | 1 + pkg/compiler/test/analyses/api_allowed.json | 6 ------ sdk/lib/_http/http_impl.dart | 14 ++++++++++---- sdk/lib/io/io.dart | 2 +- sdk/lib/io/secure_server_socket.dart | 3 ++- sdk/lib/io/socket.dart | 2 +- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad33b690ccd..c8000aedbca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ - Deprecated `RangeError.checkValidIndex` in favor of `IndexError.check`. - Deprecated `IndexError` constructor in favor of `IndexError.withLength` constructor. +- `ServerSocket` and `SecureServerSocket` implement `ServerSocketBase`. [#49529]: https://github.com/dart-lang/sdk/issues/49529 [#24644]: https://github.com/dart-lang/sdk/issues/24644 diff --git a/pkg/compiler/test/analyses/api_allowed.json b/pkg/compiler/test/analyses/api_allowed.json index dcb26d7337e..c179a66aab8 100644 --- a/pkg/compiler/test/analyses/api_allowed.json +++ b/pkg/compiler/test/analyses/api_allowed.json @@ -1,10 +1,4 @@ { - "org-dartlang-sdk:///lib/_http/http_impl.dart": { - "Dynamic access of 'address'.": 1, - "Dynamic access of 'port'.": 1, - "Dynamic invocation of 'listen'.": 1, - "Dynamic invocation of 'close'.": 1 - }, "org-dartlang-sdk:///lib/_internal/js_runtime/lib/js_helper.dart": { "Dynamic access of 'length'.": 1 }, diff --git a/sdk/lib/_http/http_impl.dart b/sdk/lib/_http/http_impl.dart index 5199ab95cf0..c319b8b2ce8 100644 --- a/sdk/lib/_http/http_impl.dart +++ b/sdk/lib/_http/http_impl.dart @@ -3136,6 +3136,14 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> String get _serviceTypeName => 'HttpServerConnection'; } +// Common interface of [ServerSocket] and [SecureServerSocket] used by +// [_HttpServer]. +abstract class ServerSocketBase implements Stream { + int get port; + InternetAddress get address; + Future close(); +} + // HTTP server waiting for socket connections. class _HttpServer extends Stream with _ServiceObject @@ -3249,7 +3257,7 @@ class _HttpServer extends Stream Future close({bool force = false}) { closed = true; Future result; - if (_serverSocket != null && _closeServer) { + if (_closeServer) { result = _serverSocket.close(); } else { result = Future.value(); @@ -3348,9 +3356,7 @@ class _HttpServer extends Stream // Indicated if the http server has been closed. bool closed = false; - // The server listen socket. Untyped as it can be both ServerSocket and - // SecureServerSocket. - final dynamic /*ServerSocket|SecureServerSocket*/ _serverSocket; + final ServerSocketBase _serverSocket; final bool _closeServer; // Set of currently connected clients. diff --git a/sdk/lib/io/io.dart b/sdk/lib/io/io.dart index b4226c9652a..662fa226fa5 100644 --- a/sdk/lib/io/io.dart +++ b/sdk/lib/io/io.dart @@ -196,7 +196,7 @@ import 'dart:collection' show HashMap, HashSet, Queue, ListQueue, MapBase, UnmodifiableMapView; import 'dart:convert'; import 'dart:developer' hide log; -import 'dart:_http' show HttpClient, HttpProfiler; +import 'dart:_http' show HttpClient, HttpProfiler, ServerSocketBase; import 'dart:isolate'; import 'dart:math'; import 'dart:typed_data'; diff --git a/sdk/lib/io/secure_server_socket.dart b/sdk/lib/io/secure_server_socket.dart index 7cce8d8e58b..ba73d0f77ea 100644 --- a/sdk/lib/io/secure_server_socket.dart +++ b/sdk/lib/io/secure_server_socket.dart @@ -7,7 +7,8 @@ part of dart.io; /// A server socket, providing a stream of high-level [Socket]s. /// /// See [SecureSocket] for more info. -class SecureServerSocket extends Stream { +class SecureServerSocket extends Stream + implements ServerSocketBase { final RawSecureServerSocket _socket; SecureServerSocket._(this._socket); diff --git a/sdk/lib/io/socket.dart b/sdk/lib/io/socket.dart index b489198716d..9e241896a6e 100644 --- a/sdk/lib/io/socket.dart +++ b/sdk/lib/io/socket.dart @@ -263,7 +263,7 @@ abstract class RawServerSocket implements Stream { /// one for each connection made to the listening socket. /// /// See [Socket] for more info. -abstract class ServerSocket implements Stream { +abstract class ServerSocket implements ServerSocketBase { /// Listens on a given address and port. /// /// When the returned future completes the server socket is bound