onRequest method
Called when the request is about to be sent.
Implementation
@override
Future onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
try {
// iOS bug: Alamofire is failing to return parallel requests for certificate validation
if (Platform.isIOS && secure != null) {
await secure;
}
var baseUrl = options.baseUrl;
if (options.path.contains('http') || options.baseUrl.isEmpty) {
baseUrl = options.path;
}
secure = HttpCertificatePinning.check(
serverURL: baseUrl,
headerHttp: {},
sha: SHA.SHA256,
allowedSHAFingerprints: _allowedSHAFingerprints,
timeout: _timeout,
);
secure?.whenComplete(() => secure = null);
final secureString = await secure ?? '';
if (secureString.contains('CONNECTION_SECURE')) {
return super.onRequest(options, handler);
} else {
handler.reject(
DioException(
requestOptions: options,
error: CertificateNotVerifiedException(),
),
callFollowingErrorInterceptor,
);
}
} on Exception catch (e) {
dynamic error;
if (e is PlatformException && e.code == 'CONNECTION_NOT_SECURE') {
error = const CertificateNotVerifiedException();
} else {
error = CertificateCouldNotBeVerifiedException(e);
}
handler.reject(
DioException(
requestOptions: options,
error: error,
),
callFollowingErrorInterceptor,
);
}
}