mirror of
https://github.com/square/okhttp.git
synced 2026-01-21 03:41:07 +03:00
Share code between delegating HttpsURLConnections.
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.squareup.okhttp.internal.http;
|
||||
|
||||
import com.squareup.okhttp.Handshake;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.security.Permission;
|
||||
import java.security.Principal;
|
||||
import java.security.cert.Certificate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
/**
|
||||
* Implement an HTTPS connection by delegating to an HTTP connection for
|
||||
* everything but the HTTPS-specific stuff.
|
||||
*/
|
||||
abstract class DelegatingHttpsURLConnection extends HttpsURLConnection {
|
||||
private final HttpURLConnection delegate;
|
||||
|
||||
public DelegatingHttpsURLConnection(HttpURLConnection delegate) {
|
||||
super(delegate.getURL());
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
protected abstract Handshake handshake();
|
||||
|
||||
@Override public abstract void setHostnameVerifier(HostnameVerifier hostnameVerifier);
|
||||
|
||||
@Override public abstract HostnameVerifier getHostnameVerifier();
|
||||
|
||||
@Override public abstract void setSSLSocketFactory(SSLSocketFactory sslSocketFactory);
|
||||
|
||||
@Override public abstract SSLSocketFactory getSSLSocketFactory();
|
||||
|
||||
@Override public String getCipherSuite() {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.cipherSuite() : null;
|
||||
}
|
||||
|
||||
@Override public Certificate[] getLocalCertificates() {
|
||||
Handshake handshake = handshake();
|
||||
if (handshake == null) return null;
|
||||
List<Certificate> result = handshake.localCertificates();
|
||||
return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
|
||||
}
|
||||
|
||||
@Override public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
|
||||
Handshake handshake = handshake();
|
||||
if (handshake == null) return null;
|
||||
List<Certificate> result = handshake.peerCertificates();
|
||||
return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
|
||||
}
|
||||
|
||||
@Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.peerPrincipal() : null;
|
||||
}
|
||||
|
||||
@Override public Principal getLocalPrincipal() {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.localPrincipal() : null;
|
||||
}
|
||||
|
||||
@Override public void connect() throws IOException {
|
||||
connected = true;
|
||||
delegate.connect();
|
||||
}
|
||||
|
||||
@Override public void disconnect() {
|
||||
delegate.disconnect();
|
||||
}
|
||||
|
||||
@Override public InputStream getErrorStream() {
|
||||
return delegate.getErrorStream();
|
||||
}
|
||||
|
||||
@Override public String getRequestMethod() {
|
||||
return delegate.getRequestMethod();
|
||||
}
|
||||
|
||||
@Override public int getResponseCode() throws IOException {
|
||||
return delegate.getResponseCode();
|
||||
}
|
||||
|
||||
@Override public String getResponseMessage() throws IOException {
|
||||
return delegate.getResponseMessage();
|
||||
}
|
||||
|
||||
@Override public void setRequestMethod(String method) throws ProtocolException {
|
||||
delegate.setRequestMethod(method);
|
||||
}
|
||||
|
||||
@Override public boolean usingProxy() {
|
||||
return delegate.usingProxy();
|
||||
}
|
||||
|
||||
@Override public boolean getInstanceFollowRedirects() {
|
||||
return delegate.getInstanceFollowRedirects();
|
||||
}
|
||||
|
||||
@Override public void setInstanceFollowRedirects(boolean followRedirects) {
|
||||
delegate.setInstanceFollowRedirects(followRedirects);
|
||||
}
|
||||
|
||||
@Override public boolean getAllowUserInteraction() {
|
||||
return delegate.getAllowUserInteraction();
|
||||
}
|
||||
|
||||
@Override public Object getContent() throws IOException {
|
||||
return delegate.getContent();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // Spec does not generify
|
||||
@Override public Object getContent(Class[] types) throws IOException {
|
||||
return delegate.getContent(types);
|
||||
}
|
||||
|
||||
@Override public String getContentEncoding() {
|
||||
return delegate.getContentEncoding();
|
||||
}
|
||||
|
||||
@Override public int getContentLength() {
|
||||
return delegate.getContentLength();
|
||||
}
|
||||
|
||||
@Override public String getContentType() {
|
||||
return delegate.getContentType();
|
||||
}
|
||||
|
||||
@Override public long getDate() {
|
||||
return delegate.getDate();
|
||||
}
|
||||
|
||||
@Override public boolean getDefaultUseCaches() {
|
||||
return delegate.getDefaultUseCaches();
|
||||
}
|
||||
|
||||
@Override public boolean getDoInput() {
|
||||
return delegate.getDoInput();
|
||||
}
|
||||
|
||||
@Override public boolean getDoOutput() {
|
||||
return delegate.getDoOutput();
|
||||
}
|
||||
|
||||
@Override public long getExpiration() {
|
||||
return delegate.getExpiration();
|
||||
}
|
||||
|
||||
@Override public String getHeaderField(int pos) {
|
||||
return delegate.getHeaderField(pos);
|
||||
}
|
||||
|
||||
@Override public Map<String, List<String>> getHeaderFields() {
|
||||
return delegate.getHeaderFields();
|
||||
}
|
||||
|
||||
@Override public Map<String, List<String>> getRequestProperties() {
|
||||
return delegate.getRequestProperties();
|
||||
}
|
||||
|
||||
@Override public void addRequestProperty(String field, String newValue) {
|
||||
delegate.addRequestProperty(field, newValue);
|
||||
}
|
||||
|
||||
@Override public String getHeaderField(String key) {
|
||||
return delegate.getHeaderField(key);
|
||||
}
|
||||
|
||||
@Override public long getHeaderFieldDate(String field, long defaultValue) {
|
||||
return delegate.getHeaderFieldDate(field, defaultValue);
|
||||
}
|
||||
|
||||
@Override public int getHeaderFieldInt(String field, int defaultValue) {
|
||||
return delegate.getHeaderFieldInt(field, defaultValue);
|
||||
}
|
||||
|
||||
@Override public String getHeaderFieldKey(int position) {
|
||||
return delegate.getHeaderFieldKey(position);
|
||||
}
|
||||
|
||||
@Override public long getIfModifiedSince() {
|
||||
return delegate.getIfModifiedSince();
|
||||
}
|
||||
|
||||
@Override public InputStream getInputStream() throws IOException {
|
||||
return delegate.getInputStream();
|
||||
}
|
||||
|
||||
@Override public long getLastModified() {
|
||||
return delegate.getLastModified();
|
||||
}
|
||||
|
||||
@Override public OutputStream getOutputStream() throws IOException {
|
||||
return delegate.getOutputStream();
|
||||
}
|
||||
|
||||
@Override public Permission getPermission() throws IOException {
|
||||
return delegate.getPermission();
|
||||
}
|
||||
|
||||
@Override public String getRequestProperty(String field) {
|
||||
return delegate.getRequestProperty(field);
|
||||
}
|
||||
|
||||
@Override public URL getURL() {
|
||||
return delegate.getURL();
|
||||
}
|
||||
|
||||
@Override public boolean getUseCaches() {
|
||||
return delegate.getUseCaches();
|
||||
}
|
||||
|
||||
@Override public void setAllowUserInteraction(boolean newValue) {
|
||||
delegate.setAllowUserInteraction(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDefaultUseCaches(boolean newValue) {
|
||||
delegate.setDefaultUseCaches(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDoInput(boolean newValue) {
|
||||
delegate.setDoInput(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDoOutput(boolean newValue) {
|
||||
delegate.setDoOutput(newValue);
|
||||
}
|
||||
|
||||
@Override public void setIfModifiedSince(long newValue) {
|
||||
delegate.setIfModifiedSince(newValue);
|
||||
}
|
||||
|
||||
@Override public void setRequestProperty(String field, String newValue) {
|
||||
delegate.setRequestProperty(field, newValue);
|
||||
}
|
||||
|
||||
@Override public void setUseCaches(boolean newValue) {
|
||||
delegate.setUseCaches(newValue);
|
||||
}
|
||||
|
||||
@Override public void setConnectTimeout(int timeoutMillis) {
|
||||
delegate.setConnectTimeout(timeoutMillis);
|
||||
}
|
||||
|
||||
@Override public int getConnectTimeout() {
|
||||
return delegate.getConnectTimeout();
|
||||
}
|
||||
|
||||
@Override public void setReadTimeout(int timeoutMillis) {
|
||||
delegate.setReadTimeout(timeoutMillis);
|
||||
}
|
||||
|
||||
@Override public int getReadTimeout() {
|
||||
return delegate.getReadTimeout();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return delegate.toString();
|
||||
}
|
||||
|
||||
@Override public void setFixedLengthStreamingMode(int contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override public void setChunkedStreamingMode(int chunkLength) {
|
||||
delegate.setChunkedStreamingMode(chunkLength);
|
||||
}
|
||||
}
|
||||
@@ -18,61 +18,23 @@ package com.squareup.okhttp.internal.http;
|
||||
|
||||
import com.squareup.okhttp.Handshake;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.security.Permission;
|
||||
import java.security.Principal;
|
||||
import java.security.cert.Certificate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
public final class HttpsURLConnectionImpl extends HttpsURLConnection {
|
||||
|
||||
/** Reuse HttpURLConnectionImpl. */
|
||||
public final class HttpsURLConnectionImpl extends DelegatingHttpsURLConnection {
|
||||
private final HttpURLConnectionImpl delegate;
|
||||
|
||||
public HttpsURLConnectionImpl(URL url, OkHttpClient client) {
|
||||
super(url);
|
||||
delegate = new HttpURLConnectionImpl(url, client);
|
||||
this(new HttpURLConnectionImpl(url, client));
|
||||
}
|
||||
|
||||
@Override public String getCipherSuite() {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.cipherSuite() : null;
|
||||
public HttpsURLConnectionImpl(HttpURLConnectionImpl delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override public Certificate[] getLocalCertificates() {
|
||||
Handshake handshake = handshake();
|
||||
if (handshake == null) return null;
|
||||
List<Certificate> result = handshake.localCertificates();
|
||||
return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
|
||||
}
|
||||
|
||||
@Override public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
|
||||
Handshake handshake = handshake();
|
||||
if (handshake == null) return null;
|
||||
List<Certificate> result = handshake.peerCertificates();
|
||||
return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null;
|
||||
}
|
||||
|
||||
@Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.peerPrincipal() : null;
|
||||
}
|
||||
|
||||
@Override public Principal getLocalPrincipal() {
|
||||
Handshake handshake = handshake();
|
||||
return handshake != null ? handshake.localPrincipal() : null;
|
||||
}
|
||||
|
||||
private Handshake handshake() {
|
||||
@Override protected Handshake handshake() {
|
||||
if (delegate.httpEngine == null) {
|
||||
throw new IllegalStateException("Connection has not yet been established");
|
||||
}
|
||||
@@ -85,212 +47,6 @@ public final class HttpsURLConnectionImpl extends HttpsURLConnection {
|
||||
: delegate.handshake;
|
||||
}
|
||||
|
||||
@Override public void disconnect() {
|
||||
delegate.disconnect();
|
||||
}
|
||||
|
||||
@Override public InputStream getErrorStream() {
|
||||
return delegate.getErrorStream();
|
||||
}
|
||||
|
||||
@Override public String getRequestMethod() {
|
||||
return delegate.getRequestMethod();
|
||||
}
|
||||
|
||||
@Override public int getResponseCode() throws IOException {
|
||||
return delegate.getResponseCode();
|
||||
}
|
||||
|
||||
@Override public String getResponseMessage() throws IOException {
|
||||
return delegate.getResponseMessage();
|
||||
}
|
||||
|
||||
@Override public void setRequestMethod(String method) throws ProtocolException {
|
||||
delegate.setRequestMethod(method);
|
||||
}
|
||||
|
||||
@Override public boolean usingProxy() {
|
||||
return delegate.usingProxy();
|
||||
}
|
||||
|
||||
@Override public boolean getInstanceFollowRedirects() {
|
||||
return delegate.getInstanceFollowRedirects();
|
||||
}
|
||||
|
||||
@Override public void setInstanceFollowRedirects(boolean followRedirects) {
|
||||
delegate.setInstanceFollowRedirects(followRedirects);
|
||||
}
|
||||
|
||||
@Override public void connect() throws IOException {
|
||||
connected = true;
|
||||
delegate.connect();
|
||||
}
|
||||
|
||||
@Override public boolean getAllowUserInteraction() {
|
||||
return delegate.getAllowUserInteraction();
|
||||
}
|
||||
|
||||
@Override public Object getContent() throws IOException {
|
||||
return delegate.getContent();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // Spec does not generify
|
||||
@Override public Object getContent(Class[] types) throws IOException {
|
||||
return delegate.getContent(types);
|
||||
}
|
||||
|
||||
@Override public String getContentEncoding() {
|
||||
return delegate.getContentEncoding();
|
||||
}
|
||||
|
||||
@Override public int getContentLength() {
|
||||
return delegate.getContentLength();
|
||||
}
|
||||
|
||||
@Override public String getContentType() {
|
||||
return delegate.getContentType();
|
||||
}
|
||||
|
||||
@Override public long getDate() {
|
||||
return delegate.getDate();
|
||||
}
|
||||
|
||||
@Override public boolean getDefaultUseCaches() {
|
||||
return delegate.getDefaultUseCaches();
|
||||
}
|
||||
|
||||
@Override public boolean getDoInput() {
|
||||
return delegate.getDoInput();
|
||||
}
|
||||
|
||||
@Override public boolean getDoOutput() {
|
||||
return delegate.getDoOutput();
|
||||
}
|
||||
|
||||
@Override public long getExpiration() {
|
||||
return delegate.getExpiration();
|
||||
}
|
||||
|
||||
@Override public String getHeaderField(int pos) {
|
||||
return delegate.getHeaderField(pos);
|
||||
}
|
||||
|
||||
@Override public Map<String, List<String>> getHeaderFields() {
|
||||
return delegate.getHeaderFields();
|
||||
}
|
||||
|
||||
@Override public Map<String, List<String>> getRequestProperties() {
|
||||
return delegate.getRequestProperties();
|
||||
}
|
||||
|
||||
@Override public void addRequestProperty(String field, String newValue) {
|
||||
delegate.addRequestProperty(field, newValue);
|
||||
}
|
||||
|
||||
@Override public String getHeaderField(String key) {
|
||||
return delegate.getHeaderField(key);
|
||||
}
|
||||
|
||||
@Override public long getHeaderFieldDate(String field, long defaultValue) {
|
||||
return delegate.getHeaderFieldDate(field, defaultValue);
|
||||
}
|
||||
|
||||
@Override public int getHeaderFieldInt(String field, int defaultValue) {
|
||||
return delegate.getHeaderFieldInt(field, defaultValue);
|
||||
}
|
||||
|
||||
@Override public String getHeaderFieldKey(int position) {
|
||||
return delegate.getHeaderFieldKey(position);
|
||||
}
|
||||
|
||||
@Override public long getIfModifiedSince() {
|
||||
return delegate.getIfModifiedSince();
|
||||
}
|
||||
|
||||
@Override public InputStream getInputStream() throws IOException {
|
||||
return delegate.getInputStream();
|
||||
}
|
||||
|
||||
@Override public long getLastModified() {
|
||||
return delegate.getLastModified();
|
||||
}
|
||||
|
||||
@Override public OutputStream getOutputStream() throws IOException {
|
||||
return delegate.getOutputStream();
|
||||
}
|
||||
|
||||
@Override public Permission getPermission() throws IOException {
|
||||
return delegate.getPermission();
|
||||
}
|
||||
|
||||
@Override public String getRequestProperty(String field) {
|
||||
return delegate.getRequestProperty(field);
|
||||
}
|
||||
|
||||
@Override public URL getURL() {
|
||||
return delegate.getURL();
|
||||
}
|
||||
|
||||
@Override public boolean getUseCaches() {
|
||||
return delegate.getUseCaches();
|
||||
}
|
||||
|
||||
@Override public void setAllowUserInteraction(boolean newValue) {
|
||||
delegate.setAllowUserInteraction(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDefaultUseCaches(boolean newValue) {
|
||||
delegate.setDefaultUseCaches(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDoInput(boolean newValue) {
|
||||
delegate.setDoInput(newValue);
|
||||
}
|
||||
|
||||
@Override public void setDoOutput(boolean newValue) {
|
||||
delegate.setDoOutput(newValue);
|
||||
}
|
||||
|
||||
@Override public void setIfModifiedSince(long newValue) {
|
||||
delegate.setIfModifiedSince(newValue);
|
||||
}
|
||||
|
||||
@Override public void setRequestProperty(String field, String newValue) {
|
||||
delegate.setRequestProperty(field, newValue);
|
||||
}
|
||||
|
||||
@Override public void setUseCaches(boolean newValue) {
|
||||
delegate.setUseCaches(newValue);
|
||||
}
|
||||
|
||||
@Override public void setConnectTimeout(int timeoutMillis) {
|
||||
delegate.setConnectTimeout(timeoutMillis);
|
||||
}
|
||||
|
||||
@Override public int getConnectTimeout() {
|
||||
return delegate.getConnectTimeout();
|
||||
}
|
||||
|
||||
@Override public void setReadTimeout(int timeoutMillis) {
|
||||
delegate.setReadTimeout(timeoutMillis);
|
||||
}
|
||||
|
||||
@Override public int getReadTimeout() {
|
||||
return delegate.getReadTimeout();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return delegate.toString();
|
||||
}
|
||||
|
||||
@Override public void setFixedLengthStreamingMode(int contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override public void setChunkedStreamingMode(int chunkLength) {
|
||||
delegate.setChunkedStreamingMode(chunkLength);
|
||||
}
|
||||
|
||||
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||
delegate.client.setHostnameVerifier(hostnameVerifier);
|
||||
}
|
||||
@@ -307,14 +63,14 @@ public final class HttpsURLConnectionImpl extends HttpsURLConnection {
|
||||
return delegate.client.getSslSocketFactory();
|
||||
}
|
||||
|
||||
@Override public void setFixedLengthStreamingMode(long contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override public long getContentLengthLong() {
|
||||
return delegate.getContentLengthLong();
|
||||
}
|
||||
|
||||
@Override public void setFixedLengthStreamingMode(long contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override public long getHeaderFieldLong(String field, long defaultValue) {
|
||||
return delegate.getHeaderFieldLong(field, defaultValue);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import com.squareup.okhttp.OkResponseCache;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import com.squareup.okhttp.ResponseSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -33,16 +32,11 @@ import java.net.ProtocolException;
|
||||
import java.net.ResponseCache;
|
||||
import java.net.SecureCacheResponse;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.security.Permission;
|
||||
import java.security.Principal;
|
||||
import java.security.cert.Certificate;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
@@ -159,7 +153,7 @@ public class ResponseCacheAdapter implements OkResponseCache {
|
||||
Request request = okResponse.request();
|
||||
// Create an object of the correct class in case the ResponseCache uses instanceof.
|
||||
if (request.isHttps()) {
|
||||
return new CacheHttpsURLConnection(okResponse);
|
||||
return new CacheHttpsURLConnection(new CacheHttpURLConnection(okResponse));
|
||||
} else {
|
||||
return new CacheHttpURLConnection(okResponse);
|
||||
}
|
||||
@@ -497,368 +491,48 @@ public class ResponseCacheAdapter implements OkResponseCache {
|
||||
public void setDefaultUseCaches(boolean defaultUseCaches) {
|
||||
super.setDefaultUseCaches(defaultUseCaches);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* An HttpsURLConnection to offer to the cache. HttpsURLConnection is concrete; rather than
|
||||
* completely duplicate CacheHttpURLConnection all methods that can be are delegated to a
|
||||
* CacheHttpURLConnection instead. The intent is that all real logic (besides HTTPS-specific
|
||||
* calls) exists in CacheHttpURLConnection.
|
||||
*/
|
||||
private static final class CacheHttpsURLConnection extends HttpsURLConnection {
|
||||
|
||||
/** An HttpsURLConnection to offer to the cache. */
|
||||
private static final class CacheHttpsURLConnection extends DelegatingHttpsURLConnection {
|
||||
private final CacheHttpURLConnection delegate;
|
||||
private final Response response;
|
||||
|
||||
public CacheHttpsURLConnection(Response response) {
|
||||
super(response.request().url());
|
||||
this.response = response;
|
||||
this.delegate = new CacheHttpURLConnection(response);
|
||||
public CacheHttpsURLConnection(CacheHttpURLConnection delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
// HttpsURLConnection methods.
|
||||
|
||||
@Override
|
||||
public String getCipherSuite() {
|
||||
if (response == null || response.handshake() == null) {
|
||||
return null;
|
||||
}
|
||||
return response.handshake().cipherSuite();
|
||||
@Override protected Handshake handshake() {
|
||||
return delegate.response.handshake();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate[] getLocalCertificates() {
|
||||
if (response == null || response.handshake() == null) {
|
||||
return null;
|
||||
}
|
||||
List<Certificate> localCertificates = response.handshake().localCertificates();
|
||||
if (localCertificates == null || localCertificates.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return localCertificates.toArray(new Certificate[localCertificates.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
|
||||
if (response == null || response.handshake() == null) {
|
||||
return null;
|
||||
}
|
||||
List<Certificate> peerCertificates = response.handshake().peerCertificates();
|
||||
if (peerCertificates == null || peerCertificates.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return peerCertificates.toArray(new Certificate[peerCertificates.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
|
||||
if (response == null || response.handshake() == null) {
|
||||
return null;
|
||||
}
|
||||
return response.handshake().peerPrincipal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Principal getLocalPrincipal() {
|
||||
if (response == null || response.handshake() == null) {
|
||||
return null;
|
||||
}
|
||||
return response.handshake().localPrincipal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||
throw throwRequestModificationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HostnameVerifier getHostnameVerifier() {
|
||||
@Override public HostnameVerifier getHostnameVerifier() {
|
||||
throw throwRequestSslAccessException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSSLSocketFactory(SSLSocketFactory socketFactory) {
|
||||
@Override public void setSSLSocketFactory(SSLSocketFactory socketFactory) {
|
||||
throw throwRequestModificationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SSLSocketFactory getSSLSocketFactory() {
|
||||
@Override public SSLSocketFactory getSSLSocketFactory() {
|
||||
throw throwRequestSslAccessException();
|
||||
}
|
||||
|
||||
// Delegated methods.
|
||||
|
||||
@Override
|
||||
public void connect() throws IOException {
|
||||
delegate.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
delegate.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequestProperty(String key, String value) {
|
||||
delegate.setRequestProperty(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRequestProperty(String key, String value) {
|
||||
delegate.addRequestProperty(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestProperty(String key) {
|
||||
return delegate.getRequestProperty(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getRequestProperties() {
|
||||
return delegate.getRequestProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFixedLengthStreamingMode(int contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFixedLengthStreamingMode(long contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChunkedStreamingMode(int chunkLength) {
|
||||
delegate.setChunkedStreamingMode(chunkLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInstanceFollowRedirects(boolean followRedirects) {
|
||||
delegate.setInstanceFollowRedirects(followRedirects);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getInstanceFollowRedirects() {
|
||||
return delegate.getInstanceFollowRedirects();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequestMethod(String method) throws ProtocolException {
|
||||
delegate.setRequestMethod(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestMethod() {
|
||||
return delegate.getRequestMethod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeaderFieldKey(int position) {
|
||||
return delegate.getHeaderFieldKey(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeaderField(int position) {
|
||||
return delegate.getHeaderField(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeaderField(String fieldName) {
|
||||
return delegate.getHeaderField(fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getResponseCode() throws IOException {
|
||||
return delegate.getResponseCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResponseMessage() throws IOException {
|
||||
return delegate.getResponseMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getErrorStream() {
|
||||
return delegate.getErrorStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean usingProxy() {
|
||||
return delegate.usingProxy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setConnectTimeout(int timeout) {
|
||||
delegate.setConnectTimeout(timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConnectTimeout() {
|
||||
return delegate.getConnectTimeout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadTimeout(int timeout) {
|
||||
delegate.setReadTimeout(timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReadTimeout() {
|
||||
return delegate.getReadTimeout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHeaderFields() {
|
||||
return delegate.getHeaderFields();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getContent() throws IOException {
|
||||
return delegate.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getContent(Class[] classes) throws IOException {
|
||||
return delegate.getContent(classes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return delegate.getInputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return delegate.getOutputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDoInput(boolean doInput) {
|
||||
delegate.setDoInput(doInput);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDoInput() {
|
||||
return delegate.getDoInput();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDoOutput(boolean doOutput) {
|
||||
delegate.setDoOutput(doOutput);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDoOutput() {
|
||||
return delegate.getDoOutput();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAllowUserInteraction(boolean allowUserInteraction) {
|
||||
delegate.setAllowUserInteraction(allowUserInteraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAllowUserInteraction() {
|
||||
return delegate.getAllowUserInteraction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUseCaches(boolean useCaches) {
|
||||
delegate.setUseCaches(useCaches);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getUseCaches() {
|
||||
return delegate.getUseCaches();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIfModifiedSince(long ifModifiedSince) {
|
||||
delegate.setIfModifiedSince(ifModifiedSince);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getIfModifiedSince() {
|
||||
return delegate.getIfModifiedSince();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDefaultUseCaches() {
|
||||
return delegate.getDefaultUseCaches();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDefaultUseCaches(boolean defaultUseCaches) {
|
||||
delegate.setDefaultUseCaches(defaultUseCaches);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getHeaderFieldDate(String name, long defaultValue) {
|
||||
return delegate.getHeaderFieldDate(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Permission getPermission() throws IOException {
|
||||
return delegate.getPermission();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() {
|
||||
return delegate.getURL();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getContentLength() {
|
||||
return delegate.getContentLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLengthLong() {
|
||||
@Override public long getContentLengthLong() {
|
||||
return delegate.getContentLengthLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return delegate.getContentType();
|
||||
@Override public void setFixedLengthStreamingMode(long contentLength) {
|
||||
delegate.setFixedLengthStreamingMode(contentLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentEncoding() {
|
||||
return delegate.getContentEncoding();
|
||||
@Override public long getHeaderFieldLong(String field, long defaultValue) {
|
||||
return delegate.getHeaderFieldLong(field, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getExpiration() {
|
||||
return delegate.getExpiration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getDate() {
|
||||
return delegate.getDate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastModified() {
|
||||
return delegate.getLastModified();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeaderFieldInt(String name, int defaultValue) {
|
||||
return delegate.getHeaderFieldInt(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getHeaderFieldLong(String name, long defaultValue) {
|
||||
return delegate.getHeaderFieldLong(name, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return delegate.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static RuntimeException throwRequestModificationException() {
|
||||
|
||||
Reference in New Issue
Block a user