if (cacheResponse != null) { // We have a valid cached response. Promote it to the user response immediately. this.userResponse = cacheResponse.newBuilder() .request(userRequest) .priorResponse(stripBody(priorResponse)) .cacheResponse(stripBody(cacheResponse)) .build(); } else { // We're forbidden from using the network, and the cache is insufficient. this.userResponse = new Response.Builder() .request(userRequest) .priorResponse(stripBody(priorResponse)) .protocol(Protocol.HTTP_1_1) .code(504) .message("Unsatisfiable Request (only-if-cached)") .body(EMPTY_BODY) .build(); }
/** Connect to the origin server either directly or via a proxy. */ privatevoidconnect()throws RequestException, RouteException { if (connection != null) thrownew IllegalStateException();
/** * Connects this connection if it isn't already. This creates tunnels, shares * the connection with the connection pool, and configures timeouts. */ voidconnectAndSetOwner(OkHttpClient client, Object owner)throws RouteException { setOwner(owner);
if (!isConnected()) { List<ConnectionSpec> connectionSpecs = route.address.getConnectionSpecs(); connect(client.getConnectTimeout(), client.getReadTimeout(), client.getWriteTimeout(), connectionSpecs, client.getRetryOnConnectionFailure()); if (isFramed()) { client.getConnectionPool().share(this); } client.routeDatabase().connected(getRoute()); }
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */ privatevoidconnectSocket(int connectTimeout, int readTimeout, int writeTimeout, ConnectionSpecSelector connectionSpecSelector)throws IOException { socket.setSoTimeout(readTimeout); Platform.get().connectSocket(socket, route.getSocketAddress(), connectTimeout);
// Configure the socket's ciphers, TLS versions, and extensions. ConnectionSpec connectionSpec = connectionSpecSelector.configureSecureSocket(sslSocket); if (connectionSpec.supportsTlsExtensions()) { Platform.get().configureTlsExtensions( sslSocket, address.getUriHost(), address.getProtocols()); }
// Force handshake. This can throw! sslSocket.startHandshake(); Handshake unverifiedHandshake = Handshake.get(sslSocket.getSession());
...
// Check that the certificate pinner is satisfied by the certificates presented. address.getCertificatePinner().check(address.getUriHost(), unverifiedHandshake.peerCertificates());