Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http2_adapter] ✨ Support non-TLS connection requests #2044

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/http2_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Support non-TLS connection requests.

## 2.3.2

Expand Down
21 changes: 15 additions & 6 deletions plugins/http2_adapter/lib/src/connection_manager_imp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class _ConnectionManager implements ConnectionManager {
onClientCreate!(uri, clientConfig);
}

late final SecureSocket socket;

// Allow [Socket] for non-TLS connections
// or [SecureSocket] for TLS connections.
late final Socket socket;
try {
socket = await _createSocket(uri, options, clientConfig);
} on SocketException catch (e) {
Expand All @@ -94,16 +95,18 @@ class _ConnectionManager implements ConnectionManager {
}

if (clientConfig.validateCertificate != null) {
final certificate =
socket is SecureSocket ? socket.peerCertificate : null;
final isCertApproved = clientConfig.validateCertificate!(
socket.peerCertificate,
certificate,
uri.host,
uri.port,
);
if (!isCertApproved) {
throw DioException(
requestOptions: options,
type: DioExceptionType.badCertificate,
error: socket.peerCertificate,
error: certificate,
message: 'The certificate of the response is not approved.',
);
}
Expand All @@ -119,7 +122,6 @@ class _ConnectionManager implements ConnectionManager {
}
};

//
transportState.delayClose(
_closed ? Duration(milliseconds: 50) : _idleTimeout,
() {
Expand All @@ -130,7 +132,7 @@ class _ConnectionManager implements ConnectionManager {
return transportState;
}

Future<SecureSocket> _createSocket(
Future<Socket> _createSocket(
Uri target,
RequestOptions options,
ClientSetting clientConfig,
Expand All @@ -141,6 +143,13 @@ class _ConnectionManager implements ConnectionManager {
final proxy = clientConfig.proxy;

if (proxy == null) {
if (target.scheme != 'https') {
return Socket.connect(
target.host,
target.port,
timeout: timeout,
);
}
return SecureSocket.connect(
target.host,
target.port,
Expand Down
6 changes: 6 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import 'package:dio_http2_adapter/dio_http2_adapter.dart';
import 'package:test/test.dart';

void main() {
test('works with non-TLS requests', () async {
final dio = Dio()..httpClientAdapter = Http2Adapter(ConnectionManager());
await dio.get('http://flutter.cn/');
await dio.get('https://flutter.cn/non-exist-destination');
});

test('adds one to input values', () async {
final dio = Dio()
..options.baseUrl = 'https://pub.dev/'
Expand Down