Skip to content

Commit

Permalink
In JDK I'm using (Java8), I get a delegating HttpURLConnection that b…
Browse files Browse the repository at this point in the history
…reaks the hack to set the method.

This change makes this hack even worse, but this is the only way I can think of, since I cannot update HttpURLConnection.methods that is static final.
  • Loading branch information
kohsuke committed Jun 3, 2016
1 parent 5c9ea9b commit 3d1bed0
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -468,6 +467,11 @@ private void setupConnection(URL url) throws IOException {
uc.setRequestProperty(e.getKey(), v);
}

setRequestMethod(uc);
uc.setRequestProperty("Accept-Encoding", "gzip");
}

private void setRequestMethod(HttpURLConnection uc) throws IOException {
try {
uc.setRequestMethod(method);
} catch (ProtocolException e) {
Expand All @@ -479,8 +483,23 @@ private void setupConnection(URL url) throws IOException {
} catch (Exception x) {
throw (IOException)new IOException("Failed to set the custom verb").initCause(x);
}
// sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
try {
Field $delegate = uc.getClass().getDeclaredField("delegate");
$delegate.setAccessible(true);
Object delegate = $delegate.get(uc);
if (delegate instanceof HttpURLConnection) {
HttpURLConnection nested = (HttpURLConnection) delegate;
setRequestMethod(nested);
}
} catch (NoSuchFieldException x) {
// no problem
} catch (IllegalAccessException x) {
throw (IOException)new IOException("Failed to set the custom verb").initCause(x);
}
}
uc.setRequestProperty("Accept-Encoding", "gzip");
if (!uc.getRequestMethod().equals(method))
throw new IllegalStateException("Failed to set the request method to "+method);
}

private <T> T parse(Class<T> type, T instance) throws IOException {
Expand Down

0 comments on commit 3d1bed0

Please sign in to comment.