11package org .kohsuke .github .extras .okhttp3 ;
22
3+ import okhttp3 .Interceptor ;
4+ import okhttp3 .Request ;
5+ import okhttp3 .Response ;
36import okhttp3 .CacheControl ;
47import okhttp3 .ConnectionSpec ;
58import okhttp3 .OkHttpClient ;
@@ -51,12 +54,18 @@ public OkHttpConnector(OkHttpClient client, int cacheMaxAge) {
5154 OkHttpClient .Builder builder = client .newBuilder ();
5255
5356 builder .connectionSpecs (TlsConnectionSpecs ());
54- this . client = builder . build ();
55- if (cacheMaxAge >= 0 && this . client != null && this . client .cache () != null ) {
57+
58+ if (cacheMaxAge >= 0 && client .cache () != null ) {
5659 maxAgeHeaderValue = new CacheControl .Builder ().maxAge (cacheMaxAge , TimeUnit .SECONDS ).build ().toString ();
60+ // HttpURLConnection does not support networkInterceptors, so this would not work
61+ // However, we hacked ObsoleteUrlFactory to do this automatically for us.
62+ // builder.addNetworkInterceptor(new RemoveIfModifiedSinceRequestHeader());
5763 } else {
5864 maxAgeHeaderValue = null ;
5965 }
66+
67+ this .client = builder .build ();
68+
6069 this .urlFactory = new ObsoleteUrlFactory (this .client );
6170 }
6271
@@ -79,4 +88,17 @@ public HttpURLConnection connect(URL url) throws IOException {
7988 private List <ConnectionSpec > TlsConnectionSpecs () {
8089 return Arrays .asList (ConnectionSpec .MODERN_TLS , ConnectionSpec .CLEARTEXT );
8190 }
91+
92+ static class RemoveIfModifiedSinceRequestHeader implements Interceptor {
93+ @ Override
94+ public Response intercept (Chain chain ) throws IOException {
95+ Request currentRequest = chain .request ();
96+ if (currentRequest .header ("If-Modified-Since" ) != null ) {
97+ currentRequest = currentRequest .newBuilder ()
98+ .removeHeader ("If-Modified-Since" )
99+ .build ();
100+ }
101+ return chain .proceed (currentRequest );
102+ }
103+ }
82104}
0 commit comments