Allow OkHttpClient overrides in Interceptor#9108
Conversation
okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http/RealInterceptorChain.kt
Outdated
Show resolved
Hide resolved
okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http/RealInterceptorChain.kt
Outdated
Show resolved
Hide resolved
okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/http/RealInterceptorChain.kt
Outdated
Show resolved
Hide resolved
# Conflicts: # okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/RealCall.kt # okhttp/src/jvmTest/kotlin/okhttp3/internal/connection/RouteSelectorTest.kt
swankjesse
left a comment
There was a problem hiding this comment.
I think this is pretty rad actually. The most difficult part is gonna be making the tests?
okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/RealCall.kt
Outdated
Show resolved
Hide resolved
|
@swankjesse I'll follow up with more specific samples, such as Android Network Pinning. |
swankjesse
left a comment
There was a problem hiding this comment.
Love it
I would like more confidence that the override value is actually used, different from whether it’s reflected in the chain’s own properties. That’s necessary but not sufficient.
okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/RealCall.kt
Outdated
Show resolved
Hide resolved
|
@swankjesse still have a few tests to fix, but ready for review |
| if (sink | ||
| .timeout() | ||
| .timeoutNanos() | ||
| .nanoseconds.inWholeMilliseconds == 10L |
There was a problem hiding this comment.
@swankjesse I tried to implement this using
.socketFactory(
DelayingSocketFactory(onWrite = {
Thread.sleep(100L)
}),
But if I break point at Thread.sleep, then socket.sink().timeout().timeoutNanos() is always cleared (0).
I suspect a bug with write timeout, but this replicates a timeout just by checking if we have the bad timeout value.
| interceptors += RetryAndFollowUpInterceptor(client) | ||
| interceptors += BridgeInterceptor(client.cookieJar) | ||
| interceptors += CacheInterceptor(this, client.cache) | ||
| interceptors += RetryAndFollowUpInterceptor() |
There was a problem hiding this comment.
@swankjesse this seems like a nice win.
|
@swankjesse requesting post review. I think I've addressed major comments, and taking a previous positive impression. Happy to revert or fix forward with any feedback. |
| interceptors += RetryAndFollowUpInterceptor(client) | ||
| interceptors += BridgeInterceptor(client.cookieJar) | ||
| interceptors += CacheInterceptor(this, client.cache) | ||
| interceptors += RetryAndFollowUpInterceptor() |
|
|
||
| override fun withReadTimeout( | ||
| timeout: Int, | ||
| timeout: Long, |
There was a problem hiding this comment.
Ugh, this is binary-incompatible and we must revert to Int
There was a problem hiding this comment.
Not sure what I was thinking
| public abstract fun request ()Lokhttp3/Request; | ||
| public abstract fun withConnectTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain; | ||
| public abstract fun withReadTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain; | ||
| public abstract fun withWriteTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain; |
There was a problem hiding this comment.
This file must never have red in it
| return copy(proxyAuthenticator = proxyAuthenticator) | ||
| } | ||
|
|
||
| override fun withSslSocketFactory( |
| fun call(): Call | ||
|
|
||
| /** | ||
| * Returns the connect timeout in milliseconds. |
There was a problem hiding this comment.
| fun withAuthenticator(authenticator: Authenticator): Chain | ||
|
|
||
| /** | ||
| * Returns the [CookieJar] for the OkHttpClient, or an override from the Call.Chain. |
There was a problem hiding this comment.
These docs describe the implementation mechanism, but not the purpose.
I’d prefer, ‘Returns the cookie jar that'll be used for this call’
| @@ -0,0 +1,858 @@ | |||
| package okhttp3 | |||
There was a problem hiding this comment.
We should attribute copyright to somebody here.
| ), | ||
| isDefault: Boolean, | ||
| ) { | ||
| fun <T> Override<T>.testApplicationInterceptor(chain: Interceptor.Chain): Response { |
| // Set the bad override directly on the client | ||
| .withOverride(badValue) | ||
| .addInterceptor { chain -> | ||
| // the only way to stop a bad override of a client is with a good override of an interceptor |
There was a problem hiding this comment.
The only way to stop a bad override with a gun...
This pull request introduces significant enhancements to the
Interceptor.ChainAPI in OkHttp, allowing much more fine-grained and dynamic control over the HTTP client's configuration on a per-call basis.Let's delve into it;
API Extensions for Interceptor.Chain
Interceptor.Chaininterface for properties such as DNS, proxy, cache, authenticators, SSL factories, certificate pinners, connection pool, cookie jar, hostname verifier, and retry settings. These methods allow interceptors and clients to override or access the OkHttpClient's configuration dynamically within the interceptor chain.Internal Refactoring to Use Chain Properties
BridgeInterceptorandCacheInterceptorto use the new properties fromInterceptor.Chaininstead of being directly constructed with dependencies likeCookieJarorCache. This makes interceptors more decoupled and flexible.Extensive test of overrides
InterceptorOverridesTestexhaustively tests that overrides can be applied by an application interceptor and observed from a network interceptor. That they can't be set in a network interceptor. And finally that bad defaults on OkHttpClient, can be saved by good overrides.