Search in sources :

Example 1 with CacheResponse

use of java.net.CacheResponse in project android_frameworks_base by ParanoidAndroid.

the class HttpResponseCacheTest method testGetInstalledWithWrongTypeInstalled.

public void testGetInstalledWithWrongTypeInstalled() {
    ResponseCache.setDefault(new ResponseCache() {

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
            return null;
        }

        @Override
        public CacheRequest put(URI uri, URLConnection connection) {
            return null;
        }
    });
    assertNull(HttpResponseCache.getInstalled());
}
Also used : CacheResponse(java.net.CacheResponse) CacheRequest(java.net.CacheRequest) List(java.util.List) ResponseCache(java.net.ResponseCache) URI(java.net.URI) URLConnection(java.net.URLConnection)

Example 2 with CacheResponse

use of java.net.CacheResponse in project phonegap-facebook-plugin by Wizcorp.

the class HttpEngine method initResponseSource.

/**
   * Initialize the source for this response. It may be corrected later if the
   * request headers forbids network use.
   */
private void initResponseSource() throws IOException {
    responseSource = ResponseSource.NETWORK;
    if (!policy.getUseCaches() || policy.responseCache == null) {
        return;
    }
    CacheResponse candidate = policy.responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap(false));
    if (candidate == null) {
        return;
    }
    Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
    cachedResponseBody = candidate.getBody();
    if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
        Util.closeQuietly(cachedResponseBody);
        return;
    }
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
    cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
    long now = System.currentTimeMillis();
    this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
    if (responseSource == ResponseSource.CACHE) {
        this.cacheResponse = candidate;
        setResponse(cachedResponseHeaders, cachedResponseBody);
    } else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
        this.cacheResponse = candidate;
    } else if (responseSource == ResponseSource.NETWORK) {
        Util.closeQuietly(cachedResponseBody);
    } else {
        throw new AssertionError();
    }
}
Also used : CacheResponse(java.net.CacheResponse) List(java.util.List)

Example 3 with CacheResponse

use of java.net.CacheResponse in project robovm by robovm.

the class URLConnectionTest method backdoorUrlToUri.

/**
     * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI,
     * HttpURLConnection recovers from URLs with unescaped but unsupported URI
     * characters like '{' and '|' by escaping these characters.
     */
private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<URI>();
    ResponseCache.setDefault(new ResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            return null;
        }

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
            uriReference.set(uri);
            throw new UnsupportedOperationException();
        }
    });
    try {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.getResponseCode();
    } catch (Exception expected) {
    }
    return uriReference.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpRetryException(java.net.HttpRetryException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) List(java.util.List) ArrayList(java.util.ArrayList) HttpResponseCache(com.android.okhttp.HttpResponseCache) ResponseCache(java.net.ResponseCache)

Example 4 with CacheResponse

use of java.net.CacheResponse in project robovm by robovm.

the class HttpEngine method initResponseSource.

/**
   * Initialize the source for this response. It may be corrected later if the
   * request headers forbids network use.
   */
private void initResponseSource() throws IOException {
    responseSource = ResponseSource.NETWORK;
    if (!policy.getUseCaches())
        return;
    OkResponseCache responseCache = client.getOkResponseCache();
    if (responseCache == null)
        return;
    CacheResponse candidate = responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap(false));
    if (candidate == null)
        return;
    Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
    cachedResponseBody = candidate.getBody();
    if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
        Util.closeQuietly(cachedResponseBody);
        return;
    }
    RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
    cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
    long now = System.currentTimeMillis();
    this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
    if (responseSource == ResponseSource.CACHE) {
        this.cacheResponse = candidate;
        setResponse(cachedResponseHeaders, cachedResponseBody);
    } else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
        this.cacheResponse = candidate;
    } else if (responseSource == ResponseSource.NETWORK) {
        Util.closeQuietly(cachedResponseBody);
    } else {
        throw new AssertionError();
    }
}
Also used : CacheResponse(java.net.CacheResponse) OkResponseCache(com.squareup.okhttp.OkResponseCache) List(java.util.List)

Example 5 with CacheResponse

use of java.net.CacheResponse in project okhttp by square.

the class ResponseCacheTest method assertCached.

private void assertCached(boolean shouldPut, int responseCode) throws Exception {
    int expectedResponseCode = responseCode;
    server = new MockWebServer();
    MockResponse mockResponse = new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(responseCode).setBody("ABCDE").addHeader("WWW-Authenticate: challenge");
    if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
        mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpURLConnection.HTTP_RESET) {
        // We forbid bodies for 204 and 205.
        mockResponse.setBody("");
    }
    server.enqueue(mockResponse);
    if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
        // 408's are a bit of an outlier because we may repeat the request if we encounter this
        // response code. In this scenario, there are 2 responses: the initial 408 and then the 200
        // because of the retry. We just want to ensure the initial 408 isn't cached.
        expectedResponseCode = 200;
        server.enqueue(new MockResponse().setHeader("Cache-Control", "no-store").setBody("FGHIJ"));
    }
    server.start();
    URL url = server.url("/").url();
    HttpURLConnection connection = openConnection(url);
    assertEquals(expectedResponseCode, connection.getResponseCode());
    // Exhaust the content stream.
    readAscii(connection);
    CacheResponse cached = cache.get(url.toURI(), "GET", null);
    if (shouldPut) {
        assertNotNull(Integer.toString(responseCode), cached);
    } else {
        assertNull(Integer.toString(responseCode), cached);
    }
    // tearDown() isn't sufficient; this test starts multiple servers
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) HttpURLConnection(java.net.HttpURLConnection) MockWebServer(okhttp3.mockwebserver.MockWebServer) URL(java.net.URL)

Aggregations

CacheResponse (java.net.CacheResponse)20 List (java.util.List)17 URI (java.net.URI)10 SecureCacheResponse (java.net.SecureCacheResponse)9 CacheRequest (java.net.CacheRequest)7 Test (org.junit.Test)7 HttpURLConnection (java.net.HttpURLConnection)6 ResponseCache (java.net.ResponseCache)6 URLConnection (java.net.URLConnection)6 Request (okhttp3.Request)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Response (okhttp3.Response)5 LinkedHashMap (java.util.LinkedHashMap)4 Headers (okhttp3.Headers)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ProtocolException (java.net.ProtocolException)3 Certificate (java.security.cert.Certificate)3 Handshake (okhttp3.Handshake)3 ResponseBody (okhttp3.ResponseBody)3