Skip to content

Commit 8a66e1e

Browse files
committed
🐛 Address PR cfug#2512 review: keep min SDK at 2.18, drop fromSocket
Per @AlexV525: 1. Revert dio/pubspec.yaml + dio_test/pubspec.yaml min SDK back to `>=2.18.0 <4.0.0` (the bump to 3.5.0 is no longer needed). 2. Revert dio/lib/src/dio_mixin.dart to `abstract class DioMixin` with the original TODO comment. 3. Replace the Dart-3.5+ `ConnectionTask.fromSocket(...)` call in `_pinnedConnectionFactory` with `SecureSocket.startConnect(...)` and return the stock SDK [ConnectionTask] it produces. We await `task.socket` once to do the leaf-cert validation; HttpClient re-awaits the same `Future<SecureSocket>` and gets the resolved value immediately (Future await is idempotent). This works on every Dart >=2.18.0 because `SecureSocket.startConnect` has been in `dart:io` well before the minimum SDK, and the `ConnectionTask` generic is covariant in its type parameter so `ConnectionTask<SecureSocket>` is implicitly assignable to `ConnectionTask<Socket>` at the factory's return position. No vendoring or `implements ConnectionTask` is required — and `ConnectionTask` being `final class` from Dart 3.0 onward is no longer a problem since we never inherit from it. The maintainer's suggestion to literally "copy ConnectionTask into the code base" cannot be done across the Dart 3.0–3.4 window (final class blocks implements; fromSocket didn't exist yet), but `SecureSocket.startConnect` achieves the same end with zero new types and zero SDK floor change. 4. Drop the two CHANGELOG bullets that referenced the SDK bump and the mixin migration. The rest of the entry (the actual cfug#2418 fix) stays. No test changes — the factory's observable behavior is unchanged (approval yields a validated SecureSocket; rejection still throws `_BadCertificateException` from the same point in `_fetch`'s try block). All 222 tests pass; `dart analyze lib/` is clean.
1 parent 23aa0c0 commit 8a66e1e

5 files changed

Lines changed: 33 additions & 27 deletions

File tree

‎dio/CHANGELOG.md‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ See the [Migration Guide][] for the complete breaking changes list.**
2727
users already configure via `badCertificateCallback: (_, _, _) => true`
2828
in the existing pinning example), so self-signed and pinned-CA setups
2929
work without supplying `createHttpClient`.
30-
- Raise the minimum Dart SDK to 3.5.0 (Flutter 3.24+). Required for the
31-
`ConnectionTask.fromSocket` API used by the pre-emission path.
32-
- Migrate `DioMixin` to `mixin class` syntax now that the SDK floor is
33-
Dart 3.
3430

3531
## 5.9.2
3632

‎dio/lib/src/adapters/io_adapter.dart‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -361,26 +361,35 @@ class IOHttpClientAdapter implements HttpClientAdapter {
361361
// is honored on every new connection.
362362
final validator = validateCertificate;
363363

364-
final socketFuture = () async {
365-
final ss = await SecureSocket.connect(
366-
url.host,
367-
url.port,
368-
supportedProtocols: const ['http/1.1'],
369-
// When [validateCertificate] is set, defer all certificate
370-
// validation to the user's callback. This makes self-signed and
371-
// pinned-CA setups work without forcing the user to also supply
372-
// [createHttpClient].
373-
onBadCertificate: validator == null ? null : (_) => true,
374-
);
375-
if (validator != null) {
376-
final cert = ss.peerCertificate;
377-
if (!validator(cert, url.host, url.port)) {
378-
ss.destroy();
379-
throw _BadCertificateException(url.host, url.port, cert);
380-
}
364+
// [SecureSocket.startConnect] returns a stock SDK [ConnectionTask] —
365+
// available since well before dio's min SDK of 2.18 — so we don't
366+
// need [ConnectionTask.fromSocket] (Dart 3.5+) and don't need to
367+
// subclass/vendor [ConnectionTask] (which is `final class` from Dart
368+
// 3.0). We await `task.socket` once here to do the leaf-cert check,
369+
// then return the same task; [HttpClient] re-awaits `task.socket`
370+
// and gets the already-resolved [SecureSocket] immediately.
371+
//
372+
// Covariance: [ConnectionTask] has no bound on its type parameter,
373+
// so [ConnectionTask<SecureSocket>] is implicitly assignable to
374+
// [ConnectionTask<Socket>] at the return position.
375+
final task = await SecureSocket.startConnect(
376+
url.host,
377+
url.port,
378+
supportedProtocols: const ['http/1.1'],
379+
// When [validateCertificate] is set, defer all certificate
380+
// validation to the user's callback. This makes self-signed and
381+
// pinned-CA setups work without forcing the user to also supply
382+
// [createHttpClient].
383+
onBadCertificate: validator == null ? null : (_) => true,
384+
);
385+
if (validator != null) {
386+
final ss = await task.socket;
387+
final cert = ss.peerCertificate;
388+
if (!validator(cert, url.host, url.port)) {
389+
ss.destroy();
390+
throw _BadCertificateException(url.host, url.port, cert);
381391
}
382-
return ss;
383-
}();
384-
return ConnectionTask.fromSocket(socketFuture, () {});
392+
}
393+
return task;
385394
}
386395
}

‎dio/lib/src/dio_mixin.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import 'transformer.dart';
2424

2525
part 'interceptor.dart';
2626

27-
abstract mixin class DioMixin implements Dio {
27+
// TODO(EVERYONE): Use `mixin class` when the lower bound of SDK is raised to 3.0.0.
28+
abstract class DioMixin implements Dio {
2829
/// The base request config for the instance.
2930
@override
3031
late BaseOptions options;

‎dio/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repository: https://github.com/cfug/dio/blob/main/dio
1717
issue_tracker: https://github.com/cfug/dio/issues
1818

1919
environment:
20-
sdk: '>=3.5.0 <4.0.0'
20+
sdk: '>=2.18.0 <4.0.0'
2121

2222
dependencies:
2323
async: ^2.8.2

‎dio_test/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository: https://github.com/cfug/dio/blob/main/dio_test
66
issue_tracker: https://github.com/cfug/dio/issues
77

88
environment:
9-
sdk: '>=3.5.0 <4.0.0'
9+
sdk: '>=2.18.0 <4.0.0'
1010

1111
dependencies:
1212
dio: any

0 commit comments

Comments
 (0)