Skip to content

Commit 90d7fea

Browse files
committed
Workaround for hub4j#669 - remove If-Modified-Since header
This is a first cut at working round hub4j#669. It is hacky as hell and I hate it.
1 parent 72aedbb commit 90d7fea

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/main/java/org/kohsuke/github/extras/okhttp3/ObsoleteUrlFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import okio.Okio;
2020
import okio.Pipe;
2121
import okio.Timeout;
22+
import org.jetbrains.annotations.NotNull;
2223

2324
/*
2425
* Copyright (C) 2014 Square, Inc.
@@ -617,7 +618,18 @@ private Call buildCall() throws IOException {
617618
OkHttpClient.Builder clientBuilder = client.newBuilder();
618619
clientBuilder.interceptors().clear();
619620
clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
621+
620622
clientBuilder.networkInterceptors().clear();
623+
624+
// network interceptors are generally not allowed
625+
// (probably buggy when used with this hacked in adapter)
626+
// but we have to use this one to handle github issue:
627+
// https://github.com/isaacs/github/issues/692
628+
// #669 in this project
629+
if (client.cache() != null && getUseCaches()) {
630+
clientBuilder.addNetworkInterceptor(new OkHttpConnector.RemoveIfModifiedSinceRequestHeader());
631+
}
632+
621633
clientBuilder.networkInterceptors().add(networkInterceptor);
622634

623635
// Use a separate dispatcher so that limits aren't impacted. But use the same executor service!

src/main/java/org/kohsuke/github/extras/okhttp3/OkHttpConnector.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.kohsuke.github.extras.okhttp3;
22

3+
import okhttp3.Interceptor;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
36
import okhttp3.CacheControl;
47
import okhttp3.ConnectionSpec;
58
import 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

Comments
 (0)