use of okhttp3.internal.http.CacheStrategy in project okhttp by square.
the class URLEncodingTest method backdoorUrlToUri.
private URI backdoorUrlToUri(URL url) throws Exception {
final AtomicReference<URI> uriReference = new AtomicReference<>();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Internal.instance.setCache(builder, new InternalCache() {
@Override
public Response get(Request request) throws IOException {
uriReference.set(request.url().uri());
throw new UnsupportedOperationException();
}
@Override
public CacheRequest put(Response response) throws IOException {
return null;
}
@Override
public void remove(Request request) throws IOException {
}
@Override
public void update(Response cached, Response network) {
}
@Override
public void trackConditionalCacheHit() {
}
@Override
public void trackResponse(CacheStrategy cacheStrategy) {
}
});
try {
HttpURLConnection connection = new OkUrlFactory(builder.build()).open(url);
connection.getResponseCode();
} catch (Exception expected) {
if (expected.getCause() instanceof URISyntaxException) {
expected.printStackTrace();
}
}
return uriReference.get();
}
use of okhttp3.internal.http.CacheStrategy in project okhttp by square.
the class CacheInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Response cacheCandidate = cache != null ? cache.get(chain.request()) : null;
long now = System.currentTimeMillis();
CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
Request networkRequest = strategy.networkRequest;
Response cacheResponse = strategy.cacheResponse;
if (cache != null) {
cache.trackResponse(strategy);
}
if (cacheCandidate != null && cacheResponse == null) {
// The cache candidate wasn't applicable. Close it.
closeQuietly(cacheCandidate.body());
}
// If we're forbidden from using the network and the cache is insufficient, fail.
if (networkRequest == null && cacheResponse == null) {
return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(504).message("Unsatisfiable Request (only-if-cached)").body(Util.EMPTY_RESPONSE).sentRequestAtMillis(-1L).receivedResponseAtMillis(System.currentTimeMillis()).build();
}
// If we don't need the network, we're done.
if (networkRequest == null) {
return cacheResponse.newBuilder().cacheResponse(stripBody(cacheResponse)).build();
}
Response networkResponse = null;
try {
networkResponse = chain.proceed(networkRequest);
} finally {
// If we're crashing on I/O or otherwise, don't leak the cache body.
if (networkResponse == null && cacheCandidate != null) {
closeQuietly(cacheCandidate.body());
}
}
// If we have a cache response too, then we're doing a conditional get.
if (cacheResponse != null) {
if (networkResponse.code() == HTTP_NOT_MODIFIED) {
Response response = cacheResponse.newBuilder().headers(combine(cacheResponse.headers(), networkResponse.headers())).sentRequestAtMillis(networkResponse.sentRequestAtMillis()).receivedResponseAtMillis(networkResponse.receivedResponseAtMillis()).cacheResponse(stripBody(cacheResponse)).networkResponse(stripBody(networkResponse)).build();
networkResponse.body().close();
// Update the cache after combining headers but before stripping the
// Content-Encoding header (as performed by initContentStream()).
cache.trackConditionalCacheHit();
cache.update(cacheResponse, response);
return response;
} else {
closeQuietly(cacheResponse.body());
}
}
Response response = networkResponse.newBuilder().cacheResponse(stripBody(cacheResponse)).networkResponse(stripBody(networkResponse)).build();
if (HttpHeaders.hasBody(response)) {
CacheRequest cacheRequest = maybeCache(response, networkResponse.request(), cache);
response = cacheWritingResponse(cacheRequest, response);
}
return response;
}
use of okhttp3.internal.http.CacheStrategy in project edx-app-android by edx.
the class CustomCacheQueryInterceptor method intercept.
@Override
public Response intercept(@NonNull final Chain chain) throws IOException {
final Request request = chain.request();
Response response = chain.proceed(request);
final String urlString = request.url().toString();
// If the OkHttp client
if (response.cacheResponse() != null) {
cacheManager.remove(urlString);
} else {
final String cachedBody = cacheManager.get(urlString);
if (cachedBody != null) {
/* Since we don't cache the metadata, the cached entries should always be assumed to
* have a 200 response code. The body is not provided in this response, because it
* would only be needed if we don't have a network response.
*/
Response cacheResponse = response.newBuilder().code(HttpStatus.OK).message("OK").handshake(request.isHttps() ? DUMMY_HANDSHAKE : null).priorResponse(null).networkResponse(null).cacheResponse(null).body(null).build();
final CacheStrategy cacheStrategy = new CacheStrategy.Factory(System.currentTimeMillis(), request, cacheResponse).get();
cacheResponse = cacheStrategy.cacheResponse;
if (cacheResponse != null) {
/* Either querying the server is forbidden by the Cache-Control headers (if
* there is no network response), or they require a conditional query
* (otherwise). In the latter case, either the server has validated the cached
* response or not. Only in the last case would the network response be
* delivered; in the first two cases the cached response would be delivered
* instead.
*/
final Response networkResponse = response.networkResponse();
if (networkResponse == null || shouldUseCachedResponse(cacheResponse, networkResponse)) {
response = response.newBuilder().code(HttpStatus.OK).cacheResponse(cacheResponse).body(ResponseBody.create(MediaType.parse("application/json"), cachedBody)).build();
} else {
response = response.newBuilder().cacheResponse(cacheResponse).build();
if (HttpEngine.hasBody(response) && HttpMethod.invalidatesCache(request.method())) {
cacheManager.remove(urlString);
}
}
}
}
}
return response;
}
Aggregations