Skip to content

Commit

Permalink
Workaround for hub4j#669 - remove If-Modified-Since header
Browse files Browse the repository at this point in the history
This is a first cut at working round hub4j#669.  It is hacky as hell and I hate it.
  • Loading branch information
bitwiseman committed Jan 17, 2020
1 parent 72aedbb commit 90d7fea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import okio.Okio;
import okio.Pipe;
import okio.Timeout;
import org.jetbrains.annotations.NotNull;

/*
* Copyright (C) 2014 Square, Inc.
Expand Down Expand Up @@ -617,7 +618,18 @@ private Call buildCall() throws IOException {
OkHttpClient.Builder clientBuilder = client.newBuilder();
clientBuilder.interceptors().clear();
clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);

clientBuilder.networkInterceptors().clear();

// network interceptors are generally not allowed
// (probably buggy when used with this hacked in adapter)
// but we have to use this one to handle github issue:
// https://github.com/isaacs/github/issues/692
// #669 in this project
if (client.cache() != null && getUseCaches()) {
clientBuilder.addNetworkInterceptor(new OkHttpConnector.RemoveIfModifiedSinceRequestHeader());
}

clientBuilder.networkInterceptors().add(networkInterceptor);

// Use a separate dispatcher so that limits aren't impacted. But use the same executor service!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.kohsuke.github.extras.okhttp3;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.CacheControl;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -51,12 +54,18 @@ public OkHttpConnector(OkHttpClient client, int cacheMaxAge) {
OkHttpClient.Builder builder = client.newBuilder();

builder.connectionSpecs(TlsConnectionSpecs());
this.client = builder.build();
if (cacheMaxAge >= 0 && this.client != null && this.client.cache() != null) {

if (cacheMaxAge >= 0 && client.cache() != null) {
maxAgeHeaderValue = new CacheControl.Builder().maxAge(cacheMaxAge, TimeUnit.SECONDS).build().toString();
// HttpURLConnection does not support networkInterceptors, so this would not work
// However, we hacked ObsoleteUrlFactory to do this automatically for us.
// builder.addNetworkInterceptor(new RemoveIfModifiedSinceRequestHeader());
} else {
maxAgeHeaderValue = null;
}

this.client = builder.build();

this.urlFactory = new ObsoleteUrlFactory(this.client);
}

Expand All @@ -79,4 +88,17 @@ public HttpURLConnection connect(URL url) throws IOException {
private List<ConnectionSpec> TlsConnectionSpecs() {
return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
}

static class RemoveIfModifiedSinceRequestHeader implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request currentRequest = chain.request();
if (currentRequest.header("If-Modified-Since") != null) {
currentRequest = currentRequest.newBuilder()
.removeHeader("If-Modified-Since")
.build();
}
return chain.proceed(currentRequest);
}
}
}

0 comments on commit 90d7fea

Please sign in to comment.