protected OkHttpClient newOkHttpClient(Proxy proxy) { OkHttpClient client = new OkHttpClient(); client.setFollowProtocolRedirects(false); if (proxy != null) { client.setProxy(proxy); } return client; } }
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); }
@Override public int getConnectTimeout() { return client.getConnectTimeout(); }
HostnameVerifier hostnameVerifier = null; if (uri.getScheme().equalsIgnoreCase("https")) { sslSocketFactory = client.getSslSocketFactory(); hostnameVerifier = client.getHostnameVerifier(); hostnameVerifier, client.getAuthenticator(), client.getProxy(), client.getTransports()); routeSelector = new RouteSelector(address, uri, client.getProxySelector(), client.getConnectionPool(), Dns.DEFAULT, client.getRoutesDatabase()); connection.connect(client.getConnectTimeout(), client.getReadTimeout(), getTunnelConfig()); client.getConnectionPool().maybeShare(connection); client.getRoutesDatabase().connected(connection.getRoute()); } else { connection.updateReadTimeout(client.getReadTimeout()); if (connection.getRoute().getProxy() != client.getProxy()) {
Proxy selectedProxy = httpEngine.connection != null ? httpEngine.connection.getRoute().getProxy() : client.getProxy(); final int responseCode = getResponseCode(); switch (responseCode) { boolean credentialsFound = HttpAuthenticator.processAuthHeader(client.getAuthenticator(), getResponseCode(), httpEngine.getResponseHeaders().getHeaders(), rawRequestHeaders, selectedProxy, url); if (!sameProtocol && !client.getFollowProtocolRedirects()) { return Retry.NONE; // This client doesn't follow redirects across protocols.
OkResponseCache responseCache = client.getOkResponseCache(); if (responseCache != null) { responseCache.trackResponse(responseSource); sendSocketRequest(); } else if (connection != null) { client.getConnectionPool().recycle(connection); connection = null;
private void maybeCache() throws IOException { // Are we caching at all? if (!policy.getUseCaches()) return; OkResponseCache responseCache = client.getOkResponseCache(); if (responseCache == null) return; HttpURLConnection connectionToCache = policy.getHttpConnectionToCache(); // Should we cache this response for this request? if (!responseHeaders.isCacheable(requestHeaders)) { responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri); return; } // Offer this request to the cache. cacheRequest = responseCache.put(uri, connectionToCache); }
@Override public final boolean usingProxy() { if (selectedProxy != null) { return isValidNonDirectProxy(selectedProxy); } // This behavior is a bit odd (but is probably justified by the // oddness of the APIs involved). Before a connection is established, // this method will return true only if this connection was explicitly // opened with a Proxy. We don't attempt to query the ProxySelector // at all. return isValidNonDirectProxy(client.getProxy()); }
public void receiveHeaders(RawHeaders headers) throws IOException { CookieHandler cookieHandler = client.getCookieHandler(); if (cookieHandler != null) { cookieHandler.put(uri, headers.toMultimap(true)); } } }
/** * Cause the socket connection to be released to the connection pool when * it is no longer needed. If it is already unneeded, it will be pooled * immediately. Otherwise the connection is held so that redirects can be * handled by the same connection. */ public final void automaticallyReleaseConnectionToPool() { automaticallyReleaseConnectionToPool = true; if (connection != null && connectionReleased) { client.getConnectionPool().recycle(connection); connection = null; } }
@Override public HostnameVerifier getHostnameVerifier() { return delegate.client.getHostnameVerifier(); }
/** * Returns a shallow copy of this OkHttpClient that uses the system-wide default for * each field that hasn't been explicitly configured. */ private OkHttpClient copyWithDefaults() { OkHttpClient result = new OkHttpClient(this); result.proxy = proxy; result.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault(); result.cookieHandler = cookieHandler != null ? cookieHandler : CookieHandler.getDefault(); result.responseCache = responseCache != null ? responseCache : ResponseCache.getDefault(); result.sslSocketFactory = sslSocketFactory != null ? sslSocketFactory : HttpsURLConnection.getDefaultSSLSocketFactory(); result.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier : OkHostnameVerifier.INSTANCE; result.authenticator = authenticator != null ? authenticator : HttpAuthenticator.SYSTEM_DEFAULT; result.connectionPool = connectionPool != null ? connectionPool : ConnectionPool.getDefault(); result.followProtocolRedirects = followProtocolRedirects; result.transports = transports != null ? transports : DEFAULT_TRANSPORTS; result.connectTimeout = connectTimeout; result.readTimeout = readTimeout; return result; }
HostnameVerifier hostnameVerifier = null; if (uri.getScheme().equalsIgnoreCase("https")) { sslSocketFactory = client.getSslSocketFactory(); hostnameVerifier = client.getHostnameVerifier(); hostnameVerifier, client.getAuthenticator(), client.getProxy(), client.getTransports()); routeSelector = new RouteSelector(address, uri, client.getProxySelector(), client.getConnectionPool(), Dns.DEFAULT, client.getRoutesDatabase()); connection.connect(client.getConnectTimeout(), client.getReadTimeout(), getTunnelConfig()); client.getConnectionPool().maybeShare(connection); client.getRoutesDatabase().connected(connection.getRoute()); } else { connection.updateReadTimeout(client.getReadTimeout()); if (connection.getRoute().getProxy() != client.getProxy()) {
Proxy selectedProxy = httpEngine.connection != null ? httpEngine.connection.getRoute().getProxy() : client.getProxy(); final int responseCode = getResponseCode(); switch (responseCode) { boolean credentialsFound = HttpAuthenticator.processAuthHeader(client.getAuthenticator(), getResponseCode(), httpEngine.getResponseHeaders().getHeaders(), rawRequestHeaders, selectedProxy, url); if (!sameProtocol && !client.getFollowProtocolRedirects()) { return Retry.NONE; // This client doesn't follow redirects across protocols.
OkResponseCache responseCache = client.getOkResponseCache(); if (responseCache != null) { responseCache.trackResponse(responseSource); sendSocketRequest(); } else if (connection != null) { client.getConnectionPool().recycle(connection); connection = null;
private void maybeCache() throws IOException { // Are we caching at all? if (!policy.getUseCaches()) return; OkResponseCache responseCache = client.getOkResponseCache(); if (responseCache == null) return; HttpURLConnection connectionToCache = policy.getHttpConnectionToCache(); // Should we cache this response for this request? if (!responseHeaders.isCacheable(requestHeaders)) { responseCache.maybeRemove(connectionToCache.getRequestMethod(), uri); return; } // Offer this request to the cache. cacheRequest = responseCache.put(uri, connectionToCache); }
@Override public final boolean usingProxy() { if (selectedProxy != null) { return isValidNonDirectProxy(selectedProxy); } // This behavior is a bit odd (but is probably justified by the // oddness of the APIs involved). Before a connection is established, // this method will return true only if this connection was explicitly // opened with a Proxy. We don't attempt to query the ProxySelector // at all. return isValidNonDirectProxy(client.getProxy()); }
public void receiveHeaders(RawHeaders headers) throws IOException { CookieHandler cookieHandler = client.getCookieHandler(); if (cookieHandler != null) { cookieHandler.put(uri, headers.toMultimap(true)); } } }
/** * Cause the socket connection to be released to the connection pool when * it is no longer needed. If it is already unneeded, it will be pooled * immediately. Otherwise the connection is held so that redirects can be * handled by the same connection. */ public final void automaticallyReleaseConnectionToPool() { automaticallyReleaseConnectionToPool = true; if (connection != null && connectionReleased) { client.getConnectionPool().recycle(connection); connection = null; } }
@Override public HostnameVerifier getHostnameVerifier() { return delegate.client.getHostnameVerifier(); }