Skip to content

Commit 7c4b35b

Browse files
authored
Allow OkHttpClient overrides in Interceptor (#9108)
Allow OkHttpClient overrides in Interceptor, generally anything settable on OkHttpClient.Builder can be overridden in application interceptor. Tests that the effect of overrides takes effect and can be reversed.
1 parent a587ecd commit 7c4b35b

12 files changed

Lines changed: 1497 additions & 65 deletions

File tree

okhttp-testing-support/src/main/kotlin/okhttp3/TestValueFactory.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ class TestValueFactory : Closeable {
170170
index = 0,
171171
exchange = null,
172172
request = call.request(),
173-
connectTimeoutMillis = 10_000,
174-
readTimeoutMillis = 10_000,
175-
writeTimeoutMillis = 10_000,
176173
)
177174

178175
fun newRoutePlanner(

okhttp/api/android/okhttp.api

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,42 @@ public abstract interface class okhttp3/Interceptor$Chain {
749749
public abstract fun call ()Lokhttp3/Call;
750750
public abstract fun connectTimeoutMillis ()I
751751
public abstract fun connection ()Lokhttp3/Connection;
752+
public abstract fun getAuthenticator ()Lokhttp3/Authenticator;
753+
public abstract fun getCache ()Lokhttp3/Cache;
754+
public abstract fun getCertificatePinner ()Lokhttp3/CertificatePinner;
755+
public abstract fun getConnectionPool ()Lokhttp3/ConnectionPool;
756+
public abstract fun getCookieJar ()Lokhttp3/CookieJar;
757+
public abstract fun getDns ()Lokhttp3/Dns;
758+
public abstract fun getEventListener ()Lokhttp3/EventListener;
759+
public abstract fun getFollowRedirects ()Z
760+
public abstract fun getFollowSslRedirects ()Z
761+
public abstract fun getHostnameVerifier ()Ljavax/net/ssl/HostnameVerifier;
762+
public abstract fun getProxy ()Ljava/net/Proxy;
763+
public abstract fun getProxyAuthenticator ()Lokhttp3/Authenticator;
764+
public abstract fun getProxySelector ()Ljava/net/ProxySelector;
765+
public abstract fun getRetryOnConnectionFailure ()Z
766+
public abstract fun getSocketFactory ()Ljavax/net/SocketFactory;
767+
public abstract fun getSslSocketFactoryOrNull ()Ljavax/net/ssl/SSLSocketFactory;
768+
public abstract fun getX509TrustManagerOrNull ()Ljavax/net/ssl/X509TrustManager;
752769
public abstract fun proceed (Lokhttp3/Request;)Lokhttp3/Response;
753770
public abstract fun readTimeoutMillis ()I
754771
public abstract fun request ()Lokhttp3/Request;
755-
public abstract fun withConnectTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
756-
public abstract fun withReadTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
757-
public abstract fun withWriteTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
772+
public abstract fun withAuthenticator (Lokhttp3/Authenticator;)Lokhttp3/Interceptor$Chain;
773+
public abstract fun withCache (Lokhttp3/Cache;)Lokhttp3/Interceptor$Chain;
774+
public abstract fun withCertificatePinner (Lokhttp3/CertificatePinner;)Lokhttp3/Interceptor$Chain;
775+
public abstract fun withConnectTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
776+
public abstract fun withConnectionPool (Lokhttp3/ConnectionPool;)Lokhttp3/Interceptor$Chain;
777+
public abstract fun withCookieJar (Lokhttp3/CookieJar;)Lokhttp3/Interceptor$Chain;
778+
public abstract fun withDns (Lokhttp3/Dns;)Lokhttp3/Interceptor$Chain;
779+
public abstract fun withHostnameVerifier (Ljavax/net/ssl/HostnameVerifier;)Lokhttp3/Interceptor$Chain;
780+
public abstract fun withProxy (Ljava/net/Proxy;)Lokhttp3/Interceptor$Chain;
781+
public abstract fun withProxyAuthenticator (Lokhttp3/Authenticator;)Lokhttp3/Interceptor$Chain;
782+
public abstract fun withProxySelector (Ljava/net/ProxySelector;)Lokhttp3/Interceptor$Chain;
783+
public abstract fun withReadTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
784+
public abstract fun withRetryOnConnectionFailure (Z)Lokhttp3/Interceptor$Chain;
785+
public abstract fun withSocketFactory (Ljavax/net/SocketFactory;)Lokhttp3/Interceptor$Chain;
786+
public abstract fun withSslSocketFactory (Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lokhttp3/Interceptor$Chain;
787+
public abstract fun withWriteTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
758788
public abstract fun writeTimeoutMillis ()I
759789
}
760790

okhttp/api/jvm/okhttp.api

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,42 @@ public abstract interface class okhttp3/Interceptor$Chain {
749749
public abstract fun call ()Lokhttp3/Call;
750750
public abstract fun connectTimeoutMillis ()I
751751
public abstract fun connection ()Lokhttp3/Connection;
752+
public abstract fun getAuthenticator ()Lokhttp3/Authenticator;
753+
public abstract fun getCache ()Lokhttp3/Cache;
754+
public abstract fun getCertificatePinner ()Lokhttp3/CertificatePinner;
755+
public abstract fun getConnectionPool ()Lokhttp3/ConnectionPool;
756+
public abstract fun getCookieJar ()Lokhttp3/CookieJar;
757+
public abstract fun getDns ()Lokhttp3/Dns;
758+
public abstract fun getEventListener ()Lokhttp3/EventListener;
759+
public abstract fun getFollowRedirects ()Z
760+
public abstract fun getFollowSslRedirects ()Z
761+
public abstract fun getHostnameVerifier ()Ljavax/net/ssl/HostnameVerifier;
762+
public abstract fun getProxy ()Ljava/net/Proxy;
763+
public abstract fun getProxyAuthenticator ()Lokhttp3/Authenticator;
764+
public abstract fun getProxySelector ()Ljava/net/ProxySelector;
765+
public abstract fun getRetryOnConnectionFailure ()Z
766+
public abstract fun getSocketFactory ()Ljavax/net/SocketFactory;
767+
public abstract fun getSslSocketFactoryOrNull ()Ljavax/net/ssl/SSLSocketFactory;
768+
public abstract fun getX509TrustManagerOrNull ()Ljavax/net/ssl/X509TrustManager;
752769
public abstract fun proceed (Lokhttp3/Request;)Lokhttp3/Response;
753770
public abstract fun readTimeoutMillis ()I
754771
public abstract fun request ()Lokhttp3/Request;
755-
public abstract fun withConnectTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
756-
public abstract fun withReadTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
757-
public abstract fun withWriteTimeout (ILjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
772+
public abstract fun withAuthenticator (Lokhttp3/Authenticator;)Lokhttp3/Interceptor$Chain;
773+
public abstract fun withCache (Lokhttp3/Cache;)Lokhttp3/Interceptor$Chain;
774+
public abstract fun withCertificatePinner (Lokhttp3/CertificatePinner;)Lokhttp3/Interceptor$Chain;
775+
public abstract fun withConnectTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
776+
public abstract fun withConnectionPool (Lokhttp3/ConnectionPool;)Lokhttp3/Interceptor$Chain;
777+
public abstract fun withCookieJar (Lokhttp3/CookieJar;)Lokhttp3/Interceptor$Chain;
778+
public abstract fun withDns (Lokhttp3/Dns;)Lokhttp3/Interceptor$Chain;
779+
public abstract fun withHostnameVerifier (Ljavax/net/ssl/HostnameVerifier;)Lokhttp3/Interceptor$Chain;
780+
public abstract fun withProxy (Ljava/net/Proxy;)Lokhttp3/Interceptor$Chain;
781+
public abstract fun withProxyAuthenticator (Lokhttp3/Authenticator;)Lokhttp3/Interceptor$Chain;
782+
public abstract fun withProxySelector (Ljava/net/ProxySelector;)Lokhttp3/Interceptor$Chain;
783+
public abstract fun withReadTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
784+
public abstract fun withRetryOnConnectionFailure (Z)Lokhttp3/Interceptor$Chain;
785+
public abstract fun withSocketFactory (Ljavax/net/SocketFactory;)Lokhttp3/Interceptor$Chain;
786+
public abstract fun withSslSocketFactory (Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lokhttp3/Interceptor$Chain;
787+
public abstract fun withWriteTimeout (JLjava/util/concurrent/TimeUnit;)Lokhttp3/Interceptor$Chain;
758788
public abstract fun writeTimeoutMillis ()I
759789
}
760790

okhttp/src/commonJvmAndroid/kotlin/okhttp3/Interceptor.kt

Lines changed: 190 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
package okhttp3
1717

1818
import java.io.IOException
19+
import java.net.Proxy
20+
import java.net.ProxySelector
1921
import java.util.concurrent.TimeUnit
22+
import javax.net.SocketFactory
23+
import javax.net.ssl.HostnameVerifier
24+
import javax.net.ssl.SSLSocketFactory
25+
import javax.net.ssl.X509TrustManager
26+
import okhttp3.internal.tls.CertificateChainCleaner
2027

2128
/**
2229
* Observes, modifies, and potentially short-circuits requests going out and the corresponding
@@ -82,31 +89,210 @@ fun interface Interceptor {
8289

8390
/**
8491
* Returns the connection the request will be executed on. This is only available in the chains
85-
* of network interceptors; for application interceptors this is always null.
92+
* of network interceptors. For application interceptors this is always null.
8693
*/
8794
fun connection(): Connection?
8895

96+
/**
97+
* Returns the `Call` to which this chain belongs.
98+
*/
8999
fun call(): Call
90100

101+
/**
102+
* Returns the connect timeout in milliseconds.
103+
*/
91104
fun connectTimeoutMillis(): Int
92105

106+
/**
107+
* Returns a new chain with the specified connect timeout.
108+
*/
93109
fun withConnectTimeout(
94-
timeout: Int,
110+
timeout: Long,
95111
unit: TimeUnit,
96112
): Chain
97113

114+
/**
115+
* Returns the read timeout in milliseconds.
116+
*/
98117
fun readTimeoutMillis(): Int
99118

119+
/**
120+
* Returns a new chain with the specified read timeout.
121+
*/
100122
fun withReadTimeout(
101-
timeout: Int,
123+
timeout: Long,
102124
unit: TimeUnit,
103125
): Chain
104126

127+
/**
128+
* Returns the write timeout in milliseconds.
129+
*/
105130
fun writeTimeoutMillis(): Int
106131

132+
/**
133+
* Returns a new chain with the specified write timeout.
134+
*/
107135
fun withWriteTimeout(
108-
timeout: Int,
136+
timeout: Long,
109137
unit: TimeUnit,
110138
): Chain
139+
140+
val followSslRedirects: Boolean
141+
142+
val followRedirects: Boolean
143+
144+
/**
145+
* Get the [DNS] instance for the OkHttpClient, or an override from the Call.Chain.
146+
*/
147+
val dns: Dns
148+
149+
/**
150+
* Override the [DNS] for the Call.Chain.
151+
*
152+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
153+
*/
154+
fun withDns(dns: Dns): Chain
155+
156+
/**
157+
* Returns the [SocketFactory] for the OkHttpClient, or an override from the Call.Chain.
158+
*/
159+
val socketFactory: SocketFactory
160+
161+
/**
162+
* Override the [SocketFactory] for the Call.Chain.
163+
*
164+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
165+
*/
166+
fun withSocketFactory(socketFactory: SocketFactory): Chain
167+
168+
/**
169+
* Returns true if the call should retry on connection failures.
170+
*/
171+
val retryOnConnectionFailure: Boolean
172+
173+
/**
174+
* Returns a new chain with the specified retry on connection failure setting.
175+
*/
176+
fun withRetryOnConnectionFailure(retryOnConnectionFailure: Boolean): Chain
177+
178+
/**
179+
* Returns the [Authenticator] for the OkHttpClient, or an override from the Call.Chain.
180+
*/
181+
val authenticator: Authenticator
182+
183+
/**
184+
* Override the [Authenticator] for the Call.Chain.
185+
*
186+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
187+
*/
188+
fun withAuthenticator(authenticator: Authenticator): Chain
189+
190+
/**
191+
* Returns the [CookieJar] for the OkHttpClient, or an override from the Call.Chain.
192+
*/
193+
val cookieJar: CookieJar
194+
195+
/**
196+
* Returns a new chain with the specified [CookieJar].
197+
*/
198+
fun withCookieJar(cookieJar: CookieJar): Chain
199+
200+
/**
201+
* Returns the [Cache] for the OkHttpClient, or an override from the Call.Chain.
202+
*/
203+
val cache: Cache?
204+
205+
/**
206+
* Override the [Cache] for the Call.Chain.
207+
*
208+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
209+
*/
210+
fun withCache(cache: Cache?): Chain
211+
212+
/**
213+
* Returns the [Proxy] for the OkHttpClient, or an override from the Call.Chain.
214+
*/
215+
val proxy: Proxy?
216+
217+
/**
218+
* Returns a new chain with the specified [Proxy].
219+
*/
220+
fun withProxy(proxy: Proxy?): Chain
221+
222+
/**
223+
* Returns the [ProxySelector] for the OkHttpClient, or an override from the Call.Chain.
224+
*/
225+
val proxySelector: ProxySelector
226+
227+
/**
228+
* Override the [ProxySelector] for the Call.Chain.
229+
*
230+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
231+
*/
232+
fun withProxySelector(proxySelector: ProxySelector): Chain
233+
234+
/**
235+
* Returns the proxy [Authenticator] for the OkHttpClient, or an override from the Call.Chain.
236+
*/
237+
val proxyAuthenticator: Authenticator
238+
239+
/**
240+
* Returns a new chain with the specified proxy [Authenticator].
241+
*/
242+
fun withProxyAuthenticator(proxyAuthenticator: Authenticator): Chain
243+
244+
/**
245+
* Returns the [SSLSocketFactory] for the OkHttpClient, or an override from the Call.Chain.
246+
*/
247+
val sslSocketFactoryOrNull: SSLSocketFactory?
248+
249+
/**
250+
* Returns a new chain with the specified [SSLSocketFactory].
251+
*
252+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
253+
*/
254+
fun withSslSocketFactory(
255+
sslSocketFactory: SSLSocketFactory?,
256+
x509TrustManager: X509TrustManager?,
257+
): Chain
258+
259+
/**
260+
* Returns the [X509TrustManager] for the OkHttpClient, or an override from the Call.Chain.
261+
*/
262+
val x509TrustManagerOrNull: X509TrustManager?
263+
264+
/**
265+
* Returns the [HostnameVerifier] for the OkHttpClient, or an override from the Call.Chain.
266+
*/
267+
val hostnameVerifier: HostnameVerifier
268+
269+
/**
270+
* Override the [HostnameVerifier] for the Call.Chain.
271+
*
272+
* @throws IllegalStateException if this is a Network Interceptor, since the override is too late.
273+
*/
274+
fun withHostnameVerifier(hostnameVerifier: HostnameVerifier): Chain
275+
276+
/**
277+
* Returns the [CertificatePinner] for the OkHttpClient, or an override from the Call.Chain.
278+
*/
279+
val certificatePinner: CertificatePinner
280+
281+
/**
282+
* Returns a new chain with the specified [CertificatePinner].
283+
*/
284+
fun withCertificatePinner(certificatePinner: CertificatePinner): Chain
285+
286+
/**
287+
* Returns the [ConnectionPool] for the OkHttpClient, or an override from the Call.Chain.
288+
*/
289+
val connectionPool: ConnectionPool
290+
291+
/**
292+
* Returns a new chain with the specified [ConnectionPool].
293+
*/
294+
fun withConnectionPool(connectionPool: ConnectionPool): Chain
295+
296+
val eventListener: EventListener
111297
}
112298
}

okhttp/src/commonJvmAndroid/kotlin/okhttp3/OkHttpClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ open class OkHttpClient internal constructor(
201201
@get:JvmName("socketFactory")
202202
val socketFactory: SocketFactory = builder.socketFactory
203203

204-
private val sslSocketFactoryOrNull: SSLSocketFactory?
204+
internal val sslSocketFactoryOrNull: SSLSocketFactory?
205205

206206
@get:JvmName("sslSocketFactory")
207207
val sslSocketFactory: SSLSocketFactory

0 commit comments

Comments
 (0)