-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkgs/ok_http] Add functionality to accept and configure redirects. (#…
- Loading branch information
1 parent
93ff4a9
commit f8d5bf8
Showing
6 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
pkgs/ok_http/android/src/main/kotlin/com/example/ok_http/RedirectInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// To cause a request failure [with a suitable message] due to too many redirects, | ||
// we need to throw an IOException. This cannot be done using Dart JNI bindings, | ||
// which lead to a deadlock and eventually a `java.net.SocketTimeoutException`. | ||
// https://github.com/dart-lang/native/issues/561 | ||
|
||
package com.example.ok_http | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import java.io.IOException | ||
|
||
class RedirectInterceptor { | ||
companion object { | ||
|
||
/** | ||
* Adds a redirect interceptor to the OkHttpClient.Builder | ||
* | ||
* @param clientBuilder The `OkHttpClient.Builder` to add the interceptor to | ||
* @param maxRedirects The maximum number of redirects to follow | ||
* @param followRedirects Whether to follow redirects | ||
* | ||
* @return OkHttpClient.Builder | ||
*/ | ||
fun addRedirectInterceptor( | ||
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) { | ||
throw IOException("Redirect limit exceeded") | ||
} | ||
|
||
val location = response.header("location") ?: break | ||
req = req.newBuilder().url(location).build() | ||
response.close() | ||
response = chain.proceed(req) | ||
redirectCount++ | ||
} | ||
|
||
response | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters