onRequest method
The callback will be executed before the request is initiated.
If you want to continue the request, call handler.next
.
If you want to complete the request with some custom data,
you can resolve a Response
object with handler.resolve
.
If you want to complete the request with an error message,
you can reject a DioError
object with handler.reject
.
Implementation
@override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
if ((!df.enable) || options.uri.scheme.toLowerCase() != 'https') {
handler.next(options);
return;
}
final dynamic domainFronting =
options.extra['domainFronting'] ?? (!df.manual);
final host = options.uri.host;
String? ip;
if (domainFronting is bool) {
if (domainFronting) {
try {
ip = await df.lookup(host);
} catch (error, stackTrace) {
final err = DioError(requestOptions: options, error: error);
err.stackTrace = stackTrace;
handler.reject(err, true);
return;
}
} else {
handler.next(options);
return;
}
} else if (domainFronting is String) {
ip = domainFronting;
}
if (ip == null) {
if (df.noIpSkip) {
handler.next(options);
return;
}
final err = DioError(
requestOptions: options,
error: '[DomainFronting] Unable to get IP address');
err.stackTrace = StackTrace.current;
handler.reject(err, true);
return;
}
final newUri = options.uri.replace(host: ip);
final headers = {...options.headers, 'host': host};
final extra = {...options.extra, 'domainFrontingRawOptions': options};
handler.next(options.copyWith(
path: newUri.toString(), headers: headers, extra: extra));
}