HttpURLConnection open(URL url, Proxy proxy) { String protocol = url.getProtocol(); OkHttpClient copy = copyWithDefaults(); copy.proxy = proxy; if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy); if (protocol.equals("https")) return new HttpsURLConnectionImpl(url, copy); throw new IllegalArgumentException("Unexpected protocol: " + protocol); }
private HttpEngine getHttpEngine(URLConnection httpConnection) { if (httpConnection instanceof HttpURLConnectionImpl) { return ((HttpURLConnectionImpl) httpConnection).getHttpEngine(); } else if (httpConnection instanceof HttpsURLConnectionImpl) { return ((HttpsURLConnectionImpl) httpConnection).getHttpEngine(); } else { return null; } }
/** * Returns the SSL socket used by {@code httpConnection} for HTTPS, nor null * if the connection isn't using HTTPS. Since we permit redirects across * protocols (HTTP to HTTPS or vice versa), the implementation type of the * connection doesn't necessarily match the implementation type of its HTTP * engine. */ private SSLSocket getSslSocket(HttpURLConnection httpConnection) { HttpEngine engine = httpConnection instanceof HttpsURLConnectionImpl ? ((HttpsURLConnectionImpl) httpConnection).getHttpEngine() : ((HttpURLConnectionImpl) httpConnection).getHttpEngine(); return engine instanceof HttpsEngine ? ((HttpsEngine) engine).getSslSocket() : null; }