Search in sources :

Example 6 with Connection

use of com.squareup.okhttp.Connection in project cordova-android-chromeview by thedracle.

the class HttpTransport method discardStream.

/**
   * Discards the response body so that the connection can be reused. This
   * needs to be done judiciously, since it delays the current request in
   * order to speed up a potential future request that may never occur.
   */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
    Connection connection = httpEngine.connection;
    if (connection == null)
        return false;
    Socket socket = connection.getSocket();
    if (socket == null)
        return false;
    try {
        int socketTimeout = socket.getSoTimeout();
        socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
        try {
            Util.skipAll(responseBodyIn);
            return true;
        } finally {
            socket.setSoTimeout(socketTimeout);
        }
    } catch (IOException e) {
        return false;
    }
}
Also used : Connection(com.squareup.okhttp.Connection) IOException(java.io.IOException) Socket(java.net.Socket)

Example 7 with Connection

use of com.squareup.okhttp.Connection in project cordova-android-chromeview by thedracle.

the class RouteSelector method next.

/**
   * Returns the next route address to attempt.
   *
   * @throws NoSuchElementException if there are no more routes to attempt.
   */
public Connection next() throws IOException {
    // Always prefer pooled connections over new connections.
    Connection pooled = pool.get(address);
    if (pooled != null) {
        return pooled;
    }
    // Compute the next route to attempt.
    if (!hasNextTlsMode()) {
        if (!hasNextInetSocketAddress()) {
            if (!hasNextProxy()) {
                if (!hasNextPostponed()) {
                    throw new NoSuchElementException();
                }
                return new Connection(nextPostponed());
            }
            lastProxy = nextProxy();
            resetNextInetSocketAddress(lastProxy);
        }
        lastInetSocketAddress = nextInetSocketAddress();
        resetNextTlsMode();
    }
    boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
    Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
    if (failedRoutes.contains(route)) {
        postponedRoutes.add(route);
        // tried last.
        return next();
    }
    return new Connection(route);
}
Also used : Connection(com.squareup.okhttp.Connection) NoSuchElementException(java.util.NoSuchElementException) Route(com.squareup.okhttp.Route)

Example 8 with Connection

use of com.squareup.okhttp.Connection in project QuickAndroid by ImKarl.

the class OkHttpCacheHelper method getResponse.

private static Response getResponse(OkHttpClient client, Request request) throws IOException {
    // Copy body metadata to the appropriate request headers.
    RequestBody body = request.body();
    if (body != null) {
        Request.Builder requestBuilder = request.newBuilder();
        MediaType contentType = body.contentType();
        if (contentType != null) {
            requestBuilder.header("Content-Type", contentType.toString());
        }
        long contentLength = body.contentLength();
        if (contentLength != -1) {
            requestBuilder.header("Content-Length", Long.toString(contentLength));
            requestBuilder.removeHeader("Transfer-Encoding");
        } else {
            requestBuilder.header("Transfer-Encoding", "chunked");
            requestBuilder.removeHeader("Content-Length");
        }
        request = requestBuilder.build();
    }
    copyWithDefaults(client);
    // Create the initial HTTP engine. Retries and redirects need new engine
    // for each attempt.
    HttpEngine engine = new HttpEngine(client, request, false, false, false, null, null, null, null);
    int followUpCount = 0;
    while (true) {
        try {
            engine.sendRequest();
            engine.readResponse();
        } catch (RequestException e) {
            // The attempt to interpret the request failed. Give up.
            throw e.getCause();
        } catch (RouteException e) {
            // The attempt to connect via a route failed. The request will
            // not have been sent.
            HttpEngine retryEngine = engine.recover(e);
            if (retryEngine != null) {
                engine = retryEngine;
                continue;
            }
            // Give up; recovery is not possible.
            throw e.getLastConnectException();
        } catch (IOException e) {
            // An attempt to communicate with a server failed. The request
            // may have been sent.
            HttpEngine retryEngine = engine.recover(e, null);
            if (retryEngine != null) {
                engine = retryEngine;
                continue;
            }
            // Give up; recovery is not possible.
            throw e;
        }
        Response response = engine.getResponse();
        Request followUp = engine.followUpRequest();
        if (followUp == null) {
            return response;
        }
        if (++followUpCount > MAX_FOLLOW_UPS) {
            throw new ProtocolException("Too many follow-up requests: " + followUpCount);
        }
        if (!engine.sameConnection(followUp.httpUrl())) {
            engine.releaseConnection();
        }
        Connection connection = engine.close();
        request = followUp;
        engine = new HttpEngine(client, request, false, false, false, connection, null, null, response);
    }
}
Also used : RouteException(com.squareup.okhttp.internal.http.RouteException) ProtocolException(java.net.ProtocolException) HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) Request(com.squareup.okhttp.Request) Connection(com.squareup.okhttp.Connection) IOException(java.io.IOException) RequestException(com.squareup.okhttp.internal.http.RequestException) Response(com.squareup.okhttp.Response) MediaType(com.squareup.okhttp.MediaType) RequestBody(com.squareup.okhttp.RequestBody)

Example 9 with Connection

use of com.squareup.okhttp.Connection in project pinpoint by naver.

the class HttpEngineConnectMethodInterceptor method doInAfterTrace.

@Override
protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
    if (target instanceof ConnectionGetter) {
        Connection connection = ((ConnectionGetter) target)._$PINPOINT$_getConnection();
        if (connection != null) {
            final StringBuilder sb = new StringBuilder();
            sb.append(connection.getRoute().getAddress().getUriHost()).append(":");
            sb.append(connection.getRoute().getAddress().getUriPort());
            recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, sb.toString());
        }
    }
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(OkHttpConstants.OK_HTTP_CLIENT_INTERNAL);
    recorder.recordException(throwable);
}
Also used : Connection(com.squareup.okhttp.Connection) ConnectionGetter(com.navercorp.pinpoint.plugin.okhttp.ConnectionGetter)

Aggregations

Connection (com.squareup.okhttp.Connection)9 IOException (java.io.IOException)5 Route (com.squareup.okhttp.Route)3 Socket (java.net.Socket)3 NoSuchElementException (java.util.NoSuchElementException)3 MediaType (com.squareup.okhttp.MediaType)2 Request (com.squareup.okhttp.Request)2 Response (com.squareup.okhttp.Response)2 DefaultResponseHandler (com.facebook.stetho.inspector.network.DefaultResponseHandler)1 RequestBodyHelper (com.facebook.stetho.inspector.network.RequestBodyHelper)1 ConnectionGetter (com.navercorp.pinpoint.plugin.okhttp.ConnectionGetter)1 RequestBody (com.squareup.okhttp.RequestBody)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 HttpEngine (com.squareup.okhttp.internal.http.HttpEngine)1 RequestException (com.squareup.okhttp.internal.http.RequestException)1 RouteException (com.squareup.okhttp.internal.http.RouteException)1 InputStream (java.io.InputStream)1 ProtocolException (java.net.ProtocolException)1