Search in sources :

Example 6 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine in project robovm by robovm.

the class HttpResponseCache method put.

@Override
public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException {
    if (!(urlConnection instanceof HttpURLConnection)) {
        return null;
    }
    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
    String requestMethod = httpConnection.getRequestMethod();
    if (maybeRemove(requestMethod, uri)) {
        return null;
    }
    if (!requestMethod.equals("GET")) {
        // so is high and the benefit is low.
        return null;
    }
    HttpEngine httpEngine = getHttpEngine(httpConnection);
    if (httpEngine == null) {
        // Don't cache unless the HTTP implementation is ours.
        return null;
    }
    ResponseHeaders response = httpEngine.getResponseHeaders();
    if (response.hasVaryAll()) {
        return null;
    }
    RawHeaders varyHeaders = httpEngine.getRequestHeaders().getHeaders().getAll(response.getVaryFields());
    Entry entry = new Entry(uri, varyHeaders, httpConnection);
    DiskLruCache.Editor editor = null;
    try {
        editor = cache.edit(uriToKey(uri));
        if (editor == null) {
            return null;
        }
        entry.writeTo(editor);
        return new CacheRequestImpl(editor);
    } catch (IOException e) {
        abortQuietly(editor);
        return null;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpEngine(com.squareup.okhttp.internal.http.HttpEngine) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) ResponseHeaders(com.squareup.okhttp.internal.http.ResponseHeaders) RawHeaders(com.squareup.okhttp.internal.http.RawHeaders)

Example 7 with HttpEngine

use of com.squareup.okhttp.internal.http.HttpEngine 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)

Aggregations

HttpEngine (com.squareup.okhttp.internal.http.HttpEngine)7 IOException (java.io.IOException)7 DiskLruCache (com.squareup.okhttp.internal.DiskLruCache)6 RawHeaders (com.squareup.okhttp.internal.http.RawHeaders)6 ResponseHeaders (com.squareup.okhttp.internal.http.ResponseHeaders)6 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 Connection (com.squareup.okhttp.Connection)1 MediaType (com.squareup.okhttp.MediaType)1 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 Response (com.squareup.okhttp.Response)1 RequestException (com.squareup.okhttp.internal.http.RequestException)1 RouteException (com.squareup.okhttp.internal.http.RouteException)1 ProtocolException (java.net.ProtocolException)1