Skip to content

Commit

Permalink
configure call to follow custom redirection interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Anikate-De committed Jun 11, 2024
1 parent 673f1fd commit a12a7fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ class RedirectInterceptor {
clientBuilder: OkHttpClient.Builder, maxRedirects: Int, followRedirects: Boolean
): OkHttpClient.Builder {
return clientBuilder.addInterceptor(Interceptor { chain ->

var req = chain.request()
var response = chain.proceed(req)
var redirectCount = 0

while (response.isRedirect && followRedirects) {
if (redirectCount > maxRedirects) {
if (redirectCount >= maxRedirects) {
throw IOException("Redirect limit exceeded")
}

val location = response.header("location")
req = req.newBuilder().url(location!!).build()
val location = response.header("location") ?: break
req = req.newBuilder().url(location).build()
response.close()
response = chain.proceed(req)
redirectCount++
Expand Down
24 changes: 12 additions & 12 deletions pkgs/ok_http/example/integration_test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ void main() async {

Future<void> testConformance() async {
group('ok_http client', () {
// testRequestBody(OkHttpClient());
// testResponseBody(OkHttpClient(), canStreamResponseBody: false);
// testRequestHeaders(OkHttpClient());
// testRequestMethods(OkHttpClient(), preservesMethodCase: true);
// testResponseHeaders(OkHttpClient(), supportsFoldedHeaders: false);
// testResponseStatusLine(OkHttpClient());
// testCompressedResponseBody(OkHttpClient());
testRequestBody(OkHttpClient());
testResponseBody(OkHttpClient(), canStreamResponseBody: false);
testRequestHeaders(OkHttpClient());
testRequestMethods(OkHttpClient(), preservesMethodCase: true);
testResponseHeaders(OkHttpClient(), supportsFoldedHeaders: false);
testResponseStatusLine(OkHttpClient());
testCompressedResponseBody(OkHttpClient());
testRedirect(OkHttpClient());
// testServerErrors(OkHttpClient());
// testClose(OkHttpClient.new);
// testIsolate(OkHttpClient.new);
// testRequestCookies(OkHttpClient(), canSendCookieHeaders: true);
// testResponseCookies(OkHttpClient(), canReceiveSetCookieHeaders: true);
testServerErrors(OkHttpClient());
testClose(OkHttpClient.new);
testIsolate(OkHttpClient.new);
testRequestCookies(OkHttpClient(), canSendCookieHeaders: true);
testResponseCookies(OkHttpClient(), canReceiveSetCookieHeaders: true);
});
}
17 changes: 15 additions & 2 deletions pkgs/ok_http/lib/src/ok_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class OkHttpClient extends BaseClient {
var requestHeaders = request.headers;
var requestMethod = request.method;
var requestBody = await request.finalize().toBytes();
var maxRedirects = request.maxRedirects;
var followRedirects = request.followRedirects;

final responseCompleter = Completer<StreamedResponse>();

Expand Down Expand Up @@ -116,8 +118,14 @@ class OkHttpClient extends BaseClient {
// builder associated with `_client`.
// They share the same connection pool and dispatcher.
// https://square.github.io/okhttp/recipes/#per-call-configuration-kt-java
final reqConfiguredClient =
_client.newBuilder().followRedirects(request.followRedirects).build();
//
// `followRedirects` is set to `false` to handle redirects manually.
// (Since OkHttp sets a hard limit of 20 redirects.)
// https://github.com/square/okhttp/blob/54238b4c713080c3fd32fb1a070fb5d6814c9a09/okhttp/src/main/kotlin/okhttp3/internal/http/RetryAndFollowUpInterceptor.kt#L350
final reqConfiguredClient = bindings.RedirectInterceptor.Companion
.addRedirectInterceptor(_client.newBuilder().followRedirects(false),
maxRedirects, followRedirects)
.build();

// `enqueue()` schedules the request to be executed in the future.
// https://square.github.io/okhttp/5.x/okhttp/okhttp3/-call/enqueue.html
Expand Down Expand Up @@ -170,6 +178,11 @@ class OkHttpClient extends BaseClient {
));
},
onFailure: (bindings.Call call, JObject ioException) {
if (ioException.toString().contains('Redirect limit exceeded')) {
responseCompleter.completeError(
ClientException('Redirect limit exceeded', request.url));
return;
}
responseCompleter.completeError(
ClientException(ioException.toString(), request.url));
},
Expand Down

0 comments on commit a12a7fe

Please sign in to comment.