/** * Report and attempt to recover from {@code e}. Returns true if the HTTP engine was replaced and * the request should be retried. Otherwise the failure is permanent. */ private boolean handleFailure(IOException e) throws IOException { RouteSelector routeSelector = httpEngine.routeSelector; if (routeSelector != null && httpEngine.connection != null) { routeSelector.connectFailed(httpEngine.connection, e); } OutputStream requestBody = httpEngine.getRequestBody(); boolean canRetryRequestBody = requestBody == null || requestBody instanceof RetryableOutputStream || (faultRecoveringRequestBody != null && faultRecoveringRequestBody.isRecoverable()); if (routeSelector == null && httpEngine.connection == null // No connection. || routeSelector != null && !routeSelector.hasNext() // No more routes to attempt. || !isRecoverable(e) || !canRetryRequestBody) { httpEngineFailure = e; return false; } httpEngine.release(true); RetryableOutputStream retryableOutputStream = requestBody instanceof RetryableOutputStream ? (RetryableOutputStream) requestBody : null; httpEngine = newHttpEngine(method, rawRequestHeaders, null, retryableOutputStream); httpEngine.routeSelector = routeSelector; // Keep the same routeSelector. if (faultRecoveringRequestBody != null && faultRecoveringRequestBody.isRecoverable()) { httpEngine.sendRequest(); faultRecoveringRequestBody.replaceStream(httpEngine.getRequestBody()); } return true; }
private Connection createNextConnection() { Object obj = client.getConnectionPool(); do { Connection connection1 = ((ConnectionPool) (obj)).get(address); if (connection1 == null) { break; } if (networkRequest.method().equals("GET") || Internal.instance.isReadable(connection1)) { return connection1; } Util.closeQuietly(connection1.getSocket()); } while (true); try { obj = new Connection(((ConnectionPool) (obj)), routeSelector.next()); } catch (IOException ioexception) { throw new RouteException(ioexception); } return ((Connection) (obj)); }
private void connectFailed(RouteSelector routeselector, IOException ioexception) { if (Internal.instance.recycleCount(connection) > 0) { return; } else { routeselector.connectFailed(connection.getRoute(), ioexception); return; } }
/** Connect to the origin server either directly or via a proxy. */ protected final void connect() throws IOException { if (connection != null) { return; } if (routeSelector == null) { String uriHost = uri.getHost(); if (uriHost == null) { throw new UnknownHostException(uri.toString()); } SSLSocketFactory sslSocketFactory = null; HostnameVerifier hostnameVerifier = null; if (uri.getScheme().equalsIgnoreCase("https")) { sslSocketFactory = client.getSslSocketFactory(); hostnameVerifier = client.getHostnameVerifier(); } Address address = new Address( uriHost, getEffectivePort(uri), sslSocketFactory, hostnameVerifier, client.getAuthenticator(), client.getProxy(), client.getTransports()); routeSelector = new RouteSelector( address, uri, client.getProxySelector(), client.getConnectionPool(), Dns.DEFAULT, client.getRoutesDatabase()); } connection = routeSelector.next(method); if (!connection.isConnected()) { connection.connect(client.getConnectTimeout(), client.getReadTimeout(), getTunnelConfig()); client.getConnectionPool().maybeShare(connection); client.getRoutesDatabase().connected(connection.getRoute()); } else if (!connection.isSpdy()) { connection.updateReadTimeout(client.getReadTimeout()); } connected(connection); if (connection.getRoute().getProxy() != client.getProxy()) { // Update the request line if the proxy changed; it may need a host name. requestHeaders.getHeaders().setRequestLine(getRequestLine()); } }
private void connect() { if (connection != null) { throw new IllegalStateException(); } if (routeSelector == null) { address = createAddress(client, networkRequest); try { routeSelector = RouteSelector.get(address, networkRequest, client); } catch (IOException ioexception) { throw new RequestException(ioexception); } } connection = createNextConnection(); Internal.instance.connectAndSetOwner(client, connection, this, networkRequest); route = connection.getRoute(); }