public static RouteSelector get(Request request, OkHttpClient client, Dns dns) throws IOException { String uriHost = request.url().getHost(); if (uriHost == null || uriHost.length() == 0) { throw new UnknownHostException(request.url().toString()); } SSLSocketFactory sslSocketFactory = null; HostnameVerifier hostnameVerifier = null; if (request.isHttps()) { sslSocketFactory = client.getSslSocketFactory(); hostnameVerifier = client.getHostnameVerifier(); } Address address = new Address( uriHost, getEffectivePort(request.url()), client.getSocketFactory(), sslSocketFactory, hostnameVerifier, client.getAuthenticator(), client.getProxy(), client.getProtocols()); return new RouteSelector(address, request.uri(), dns, client, request); }
/** Prepares the connection specs to attempt. */ private void resetConnectionSpecs() { connectionSpecs = new ArrayList<ConnectionSpec>(); List<ConnectionSpec> specs = address.getConnectionSpecs(); for (int i = 0, size = specs.size(); i < size; i++) { ConnectionSpec spec = specs.get(i); if (request.isHttps() == spec.isTls()) { connectionSpecs.add(spec); } } nextSpecIndex = 0; }
private static Address createAddress(OkHttpClient okhttpclient, Request request) { com.squareup.okhttp.CertificatePinner certificatepinner = null; javax.net.ssl.SSLSocketFactory sslsocketfactory; javax.net.ssl.HostnameVerifier hostnameverifier; if (request.isHttps()) { sslsocketfactory = okhttpclient.getSslSocketFactory(); hostnameverifier = okhttpclient.getHostnameVerifier(); certificatepinner = okhttpclient.getCertificatePinner(); } else { hostnameverifier = null; sslsocketfactory = null; } return new Address(request.httpUrl().host(), request.httpUrl().port(), okhttpclient.getSocketFactory(), sslsocketfactory, hostnameverifier, certificatepinner, okhttpclient.getAuthenticator(), okhttpclient.getProxy(), okhttpclient.getProtocols(), okhttpclient.getConnectionSpecs(), okhttpclient.getProxySelector()); }
private static boolean includeAuthorityInRequestLine(Request request, java.net.Proxy.Type type) { return !request.isHttps() && type == java.net.Proxy.Type.HTTP; }
/** Returns a strategy to use assuming the request can use the network. */ private CacheStrategy getCandidate() { // No cached response. if (cacheResponse == null) { return new CacheStrategy(request, null); } // Drop the cached response if it's missing a required handshake. if (request.isHttps() && cacheResponse.handshake() == null) { return new CacheStrategy(request, null); } // If this response shouldn't have been stored, it should never be used // as a response source. This check should be redundant as long as the // persistence store is well-behaved and the rules are constant. if (!isCacheable(cacheResponse, request)) { return new CacheStrategy(request, null); } CacheControl requestCaching = request.cacheControl(); if (requestCaching.noCache() || hasConditions(request)) { return new CacheStrategy(request, null); } long ageMillis = cacheResponseAge(); long freshMillis = computeFreshnessLifetime(); if (requestCaching.maxAgeSeconds() != -1) { freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds())); } long minFreshMillis = 0; if (requestCaching.minFreshSeconds() != -1) { minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds()); } long maxStaleMillis = 0; CacheControl responseCaching = cacheResponse.cacheControl(); if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) { maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds()); } if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) { Response.Builder builder = cacheResponse.newBuilder(); if (ageMillis + minFreshMillis >= freshMillis) { builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\""); } long oneDayMillis = 24 * 60 * 60 * 1000L; if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) { builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\""); } return new CacheStrategy(null, builder.build()); } Request.Builder conditionalRequestBuilder = request.newBuilder(); if (etag != null) { conditionalRequestBuilder.header("If-None-Match", etag); } else if (lastModified != null) { conditionalRequestBuilder.header("If-Modified-Since", lastModifiedString); } else if (servedDate != null) { conditionalRequestBuilder.header("If-Modified-Since", servedDateString); } Request conditionalRequest = conditionalRequestBuilder.build(); return hasConditions(conditionalRequest) ? new CacheStrategy(conditionalRequest, cacheResponse) : new CacheStrategy(conditionalRequest, null); }