1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package com.squareup.okhttp.internal.huc; 18 19 import com.squareup.okhttp.Handshake; 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.net.HttpURLConnection; 24 import java.net.ProtocolException; 25 import java.net.URL; 26 import java.security.Permission; 27 import java.security.Principal; 28 import java.security.cert.Certificate; 29 import java.util.List; 30 import java.util.Map; 31 import javax.net.ssl.HostnameVerifier; 32 import javax.net.ssl.HttpsURLConnection; 33 import javax.net.ssl.SSLPeerUnverifiedException; 34 import javax.net.ssl.SSLSocketFactory; 35 36 /** 37 * Implement an HTTPS connection by delegating to an HTTP connection for 38 * everything but the HTTPS-specific stuff. 39 */ 40 abstract class DelegatingHttpsURLConnection extends HttpsURLConnection { 41 private final HttpURLConnection delegate; 42 DelegatingHttpsURLConnection(HttpURLConnection delegate)43 public DelegatingHttpsURLConnection(HttpURLConnection delegate) { 44 super(delegate.getURL()); 45 this.delegate = delegate; 46 } 47 handshake()48 protected abstract Handshake handshake(); 49 setHostnameVerifier(HostnameVerifier hostnameVerifier)50 @Override public abstract void setHostnameVerifier(HostnameVerifier hostnameVerifier); 51 getHostnameVerifier()52 @Override public abstract HostnameVerifier getHostnameVerifier(); 53 setSSLSocketFactory(SSLSocketFactory sslSocketFactory)54 @Override public abstract void setSSLSocketFactory(SSLSocketFactory sslSocketFactory); 55 getSSLSocketFactory()56 @Override public abstract SSLSocketFactory getSSLSocketFactory(); 57 getCipherSuite()58 @Override public String getCipherSuite() { 59 Handshake handshake = handshake(); 60 return handshake != null ? handshake.cipherSuite() : null; 61 } 62 getLocalCertificates()63 @Override public Certificate[] getLocalCertificates() { 64 Handshake handshake = handshake(); 65 if (handshake == null) return null; 66 List<Certificate> result = handshake.localCertificates(); 67 return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null; 68 } 69 getServerCertificates()70 @Override public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException { 71 Handshake handshake = handshake(); 72 if (handshake == null) return null; 73 List<Certificate> result = handshake.peerCertificates(); 74 return !result.isEmpty() ? result.toArray(new Certificate[result.size()]) : null; 75 } 76 getPeerPrincipal()77 @Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { 78 Handshake handshake = handshake(); 79 return handshake != null ? handshake.peerPrincipal() : null; 80 } 81 getLocalPrincipal()82 @Override public Principal getLocalPrincipal() { 83 Handshake handshake = handshake(); 84 return handshake != null ? handshake.localPrincipal() : null; 85 } 86 connect()87 @Override public void connect() throws IOException { 88 connected = true; 89 delegate.connect(); 90 } 91 disconnect()92 @Override public void disconnect() { 93 delegate.disconnect(); 94 } 95 getErrorStream()96 @Override public InputStream getErrorStream() { 97 return delegate.getErrorStream(); 98 } 99 getRequestMethod()100 @Override public String getRequestMethod() { 101 return delegate.getRequestMethod(); 102 } 103 getResponseCode()104 @Override public int getResponseCode() throws IOException { 105 return delegate.getResponseCode(); 106 } 107 getResponseMessage()108 @Override public String getResponseMessage() throws IOException { 109 return delegate.getResponseMessage(); 110 } 111 setRequestMethod(String method)112 @Override public void setRequestMethod(String method) throws ProtocolException { 113 delegate.setRequestMethod(method); 114 } 115 usingProxy()116 @Override public boolean usingProxy() { 117 return delegate.usingProxy(); 118 } 119 getInstanceFollowRedirects()120 @Override public boolean getInstanceFollowRedirects() { 121 return delegate.getInstanceFollowRedirects(); 122 } 123 setInstanceFollowRedirects(boolean followRedirects)124 @Override public void setInstanceFollowRedirects(boolean followRedirects) { 125 delegate.setInstanceFollowRedirects(followRedirects); 126 } 127 getAllowUserInteraction()128 @Override public boolean getAllowUserInteraction() { 129 return delegate.getAllowUserInteraction(); 130 } 131 getContent()132 @Override public Object getContent() throws IOException { 133 return delegate.getContent(); 134 } 135 136 @SuppressWarnings("unchecked") // Spec does not generify getContent(Class[] types)137 @Override public Object getContent(Class[] types) throws IOException { 138 return delegate.getContent(types); 139 } 140 getContentEncoding()141 @Override public String getContentEncoding() { 142 return delegate.getContentEncoding(); 143 } 144 getContentLength()145 @Override public int getContentLength() { 146 return delegate.getContentLength(); 147 } 148 getContentType()149 @Override public String getContentType() { 150 return delegate.getContentType(); 151 } 152 getDate()153 @Override public long getDate() { 154 return delegate.getDate(); 155 } 156 getDefaultUseCaches()157 @Override public boolean getDefaultUseCaches() { 158 return delegate.getDefaultUseCaches(); 159 } 160 getDoInput()161 @Override public boolean getDoInput() { 162 return delegate.getDoInput(); 163 } 164 getDoOutput()165 @Override public boolean getDoOutput() { 166 return delegate.getDoOutput(); 167 } 168 getExpiration()169 @Override public long getExpiration() { 170 return delegate.getExpiration(); 171 } 172 getHeaderField(int pos)173 @Override public String getHeaderField(int pos) { 174 return delegate.getHeaderField(pos); 175 } 176 getHeaderFields()177 @Override public Map<String, List<String>> getHeaderFields() { 178 return delegate.getHeaderFields(); 179 } 180 getRequestProperties()181 @Override public Map<String, List<String>> getRequestProperties() { 182 return delegate.getRequestProperties(); 183 } 184 addRequestProperty(String field, String newValue)185 @Override public void addRequestProperty(String field, String newValue) { 186 delegate.addRequestProperty(field, newValue); 187 } 188 getHeaderField(String key)189 @Override public String getHeaderField(String key) { 190 return delegate.getHeaderField(key); 191 } 192 getHeaderFieldDate(String field, long defaultValue)193 @Override public long getHeaderFieldDate(String field, long defaultValue) { 194 return delegate.getHeaderFieldDate(field, defaultValue); 195 } 196 getHeaderFieldInt(String field, int defaultValue)197 @Override public int getHeaderFieldInt(String field, int defaultValue) { 198 return delegate.getHeaderFieldInt(field, defaultValue); 199 } 200 getHeaderFieldKey(int position)201 @Override public String getHeaderFieldKey(int position) { 202 return delegate.getHeaderFieldKey(position); 203 } 204 getIfModifiedSince()205 @Override public long getIfModifiedSince() { 206 return delegate.getIfModifiedSince(); 207 } 208 getInputStream()209 @Override public InputStream getInputStream() throws IOException { 210 return delegate.getInputStream(); 211 } 212 getLastModified()213 @Override public long getLastModified() { 214 return delegate.getLastModified(); 215 } 216 getOutputStream()217 @Override public OutputStream getOutputStream() throws IOException { 218 return delegate.getOutputStream(); 219 } 220 getPermission()221 @Override public Permission getPermission() throws IOException { 222 return delegate.getPermission(); 223 } 224 getRequestProperty(String field)225 @Override public String getRequestProperty(String field) { 226 return delegate.getRequestProperty(field); 227 } 228 getURL()229 @Override public URL getURL() { 230 return delegate.getURL(); 231 } 232 getUseCaches()233 @Override public boolean getUseCaches() { 234 return delegate.getUseCaches(); 235 } 236 setAllowUserInteraction(boolean newValue)237 @Override public void setAllowUserInteraction(boolean newValue) { 238 delegate.setAllowUserInteraction(newValue); 239 } 240 setDefaultUseCaches(boolean newValue)241 @Override public void setDefaultUseCaches(boolean newValue) { 242 delegate.setDefaultUseCaches(newValue); 243 } 244 setDoInput(boolean newValue)245 @Override public void setDoInput(boolean newValue) { 246 delegate.setDoInput(newValue); 247 } 248 setDoOutput(boolean newValue)249 @Override public void setDoOutput(boolean newValue) { 250 delegate.setDoOutput(newValue); 251 } 252 setIfModifiedSince(long newValue)253 @Override public void setIfModifiedSince(long newValue) { 254 delegate.setIfModifiedSince(newValue); 255 } 256 setRequestProperty(String field, String newValue)257 @Override public void setRequestProperty(String field, String newValue) { 258 delegate.setRequestProperty(field, newValue); 259 } 260 setUseCaches(boolean newValue)261 @Override public void setUseCaches(boolean newValue) { 262 delegate.setUseCaches(newValue); 263 } 264 setConnectTimeout(int timeoutMillis)265 @Override public void setConnectTimeout(int timeoutMillis) { 266 delegate.setConnectTimeout(timeoutMillis); 267 } 268 getConnectTimeout()269 @Override public int getConnectTimeout() { 270 return delegate.getConnectTimeout(); 271 } 272 setReadTimeout(int timeoutMillis)273 @Override public void setReadTimeout(int timeoutMillis) { 274 delegate.setReadTimeout(timeoutMillis); 275 } 276 getReadTimeout()277 @Override public int getReadTimeout() { 278 return delegate.getReadTimeout(); 279 } 280 toString()281 @Override public String toString() { 282 return delegate.toString(); 283 } 284 setFixedLengthStreamingMode(int contentLength)285 @Override public void setFixedLengthStreamingMode(int contentLength) { 286 delegate.setFixedLengthStreamingMode(contentLength); 287 } 288 setChunkedStreamingMode(int chunkLength)289 @Override public void setChunkedStreamingMode(int chunkLength) { 290 delegate.setChunkedStreamingMode(chunkLength); 291 } 292 } 293