@Override public InputStream getTransferStream(CacheRequest cacheRequest) throws IOException { if (!httpEngine.hasResponseBody()) { return new FixedLengthInputStream(socketIn, cacheRequest, httpEngine, 0); } if (httpEngine.responseHeaders.isChunked()) { return new ChunkedInputStream(socketIn, cacheRequest, this); } if (httpEngine.responseHeaders.getContentLength() != -1) { return new FixedLengthInputStream(socketIn, cacheRequest, httpEngine, httpEngine.responseHeaders.getContentLength()); } // Wrap the input stream from the connection (rather than just returning // "socketIn" directly here), so that we can control its use after the // reference escapes. return new UnknownLengthHttpInputStream(socketIn, cacheRequest, httpEngine); }
@Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { Response okResponse = JavaApiConverter.createOkResponseForCachePut(uri, urlConnection); if (okResponse == null) { // The URLConnection is not cacheable or could not be converted. Stop. return null; } okhttp3.internal.cache.CacheRequest okCacheRequest = delegate.internalCache.put(okResponse); if (okCacheRequest == null) { return null; } return JavaApiConverter.createJavaCacheRequest(okCacheRequest); }
@Test public void put_httpsGet() throws Exception { final URL serverUrl = configureHttpsServer(new MockResponse()); assertEquals("https", serverUrl.getProtocol()); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { try { assertTrue(connection instanceof HttpsURLConnection); assertEquals(toUri(serverUrl), uri); assertEquals(serverUrl, connection.getURL()); HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection; HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection; assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite()); assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal()); assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates()); assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates()); assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal()); return null; } catch (Throwable t) { throw new IOException("unexpected cache failure", t); } } }; setInternalCache(new CacheAdapter(responseCache)); client = client.newBuilder() .sslSocketFactory(sslClient.socketFactory, sslClient.trustManager) .hostnameVerifier(hostnameVerifier) .build(); connection = new OkUrlFactory(client).open(serverUrl); executeGet(connection); }
public HttpInputStream (InputStream is, CacheRequest cacheRequest) { super (is); this.cacheRequest = cacheRequest; try { this.outputStream = cacheRequest.getBody(); } catch (IOException ioex) { this.cacheRequest.abort(); this.cacheRequest = null; this.outputStream = null; } }
AbstractHttpInputStream(InputStream in, HttpEngine httpEngine, CacheRequest cacheRequest) throws IOException { this.in = in; this.httpEngine = httpEngine; OutputStream cacheBody = cacheRequest != null ? cacheRequest.getBody() : null; // some apps return a null body; for compatibility we treat that like a null cache request if (cacheBody == null) { cacheRequest = null; } this.cacheBody = cacheBody; this.cacheRequest = cacheRequest; }
public FixedLengthInputStream(InputStream is, CacheRequest cacheRequest, HttpEngine httpEngine, long length) throws IOException { super(is, httpEngine, cacheRequest); bytesRemaining = length; if (bytesRemaining == 0) { endOfInput(); } }
public synchronized CacheRequest put(URI uri, URLConnection conn) throws IOException { System.out.println("put: " + uri); Thread.currentThread().dumpStack(); URL url = uri.toURL(); return new DeployCacheRequest(url, conn); }
@Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { Response okResponse = JavaApiConverter.createOkResponseForCachePut(uri, urlConnection); if (okResponse == null) { // The URLConnection is not cacheable or could not be converted. Stop. return null; } okhttp3.internal.http.CacheRequest okCacheRequest = delegate.internalCache.put(okResponse); if (okCacheRequest == null) { return null; } return JavaApiConverter.createJavaCacheRequest(okCacheRequest); }
@Test public void put_httpGet() throws Exception { final String statusLine = "HTTP/1.1 200 Fantastic"; final byte[] response = "ResponseString".getBytes(StandardCharsets.UTF_8); final URL serverUrl = configureServer( new MockResponse() .setStatus(statusLine) .addHeader("A", "c") .setBody(new Buffer().write(response))); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { assertTrue(connection instanceof HttpURLConnection); assertFalse(connection instanceof HttpsURLConnection); assertEquals(response.length, connection.getContentLength()); HttpURLConnection httpUrlConnection = (HttpURLConnection) connection; assertEquals("GET", httpUrlConnection.getRequestMethod()); assertTrue(httpUrlConnection.getDoInput()); assertFalse(httpUrlConnection.getDoOutput()); assertEquals("Fantastic", httpUrlConnection.getResponseMessage()); assertEquals(toUri(serverUrl), uri); assertEquals(serverUrl, connection.getURL()); assertEquals("value", connection.getRequestProperty("key")); // Check retrieval by string key. assertEquals(statusLine, httpUrlConnection.getHeaderField(null)); assertEquals("c", httpUrlConnection.getHeaderField("A")); // The RI and OkHttp supports case-insensitive matching for this method. assertEquals("c", httpUrlConnection.getHeaderField("a")); return null; } }; setInternalCache(new CacheAdapter(responseCache)); connection = new OkUrlFactory(client).open(serverUrl); connection.setRequestProperty("key", "value"); executeGet(connection); }
@Test public void put_httpPost() throws Exception { final String statusLine = "HTTP/1.1 200 Fantastic"; final URL serverUrl = configureServer( new MockResponse() .setStatus(statusLine) .addHeader("A", "c")); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { assertTrue(connection instanceof HttpURLConnection); assertFalse(connection instanceof HttpsURLConnection); assertEquals(0, connection.getContentLength()); HttpURLConnection httpUrlConnection = (HttpURLConnection) connection; assertEquals("POST", httpUrlConnection.getRequestMethod()); assertTrue(httpUrlConnection.getDoInput()); assertTrue(httpUrlConnection.getDoOutput()); assertEquals("Fantastic", httpUrlConnection.getResponseMessage()); assertEquals(toUri(serverUrl), uri); assertEquals(serverUrl, connection.getURL()); assertEquals("value", connection.getRequestProperty("key")); // Check retrieval by string key. assertEquals(statusLine, httpUrlConnection.getHeaderField(null)); assertEquals("c", httpUrlConnection.getHeaderField("A")); // The RI and OkHttp supports case-insensitive matching for this method. assertEquals("c", httpUrlConnection.getHeaderField("a")); return null; } }; setInternalCache(new CacheAdapter(responseCache)); connection = new OkUrlFactory(client).open(serverUrl); executePost(connection); }
@Test public void put_httpsGet() throws Exception { final URL serverUrl = configureHttpsServer(new MockResponse()); assertEquals("https", serverUrl.getProtocol()); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { assertTrue(connection instanceof HttpsURLConnection); assertEquals(toUri(serverUrl), uri); assertEquals(serverUrl, connection.getURL()); HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection; HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection; assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite()); assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal()); assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates()); assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates()); assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal()); return null; } }; setInternalCache(new CacheAdapter(responseCache)); client = client.newBuilder() .sslSocketFactory(sslContext.getSocketFactory()) .hostnameVerifier(hostnameVerifier) .build(); connection = new OkUrlFactory(client).open(serverUrl); executeGet(connection); }