Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS URLCache caching when it shouldn't (IMHO)

Does anyone know why this request is being cached?

  • I'm using an unmodified .default URLSessionConfiguration.

  • The response headers are:

(from Charles, confirmed from debugging the response in the data tasks's completion block)

{
    "Accept-Ranges" = bytes;
    "Content-Length" = 1480;
    "Content-Type" = "application/json";
    Date = "Mon, 22 May 2017 19:14:13 GMT";
    Etag = "\"42bebc5fb88323b8cd145ed85ea7a018\"";
    "Last-Modified" = "Mon, 22 May 2017 14:54:38 GMT";
    Server = AmazonS3;
    "x-amz-id-2" = "abcdefghijklmn";
    "x-amz-request-id" = 1A2B3C4D5E;
}
  • I'm verifying that the request is cached using Charles proxy - the first request appears, but subsequent requests don't.

  • Using .ephemeral session configuration, or setting a custom url cache with 0 for memory and disk size shows all the requests in Charles, so I know Charles is a valid test.

I've always assumed that without cache headers a response won't be cached :|

Any ideas anyone?


EDIT: Here's the request I'm making

po task.originalRequest
▿ Optional<URLRequest>
  ▿ some : http://s3-eu-west-1.amazonaws.com/path/path/configuration.json
    ▿ url : Optional<URL>
      ▿ some : http://s3-eu-west-1.amazonaws.com/path/path/configuration.json
    - cachePolicy : 0
    - timeoutInterval : 60.0
    - mainDocumentURL : nil
    - networkServiceType : __ObjC.NSURLRequest.NetworkServiceType
    - allowsCellularAccess : true
    ▿ httpMethod : Optional<String>
      - some : "GET"
    - allHTTPHeaderFields : nil
    - httpBody : nil
    - httpBodyStream : nil
    - httpShouldHandleCookies : true
    - httpShouldUsePipelining : false
like image 449
deanWombourne Avatar asked May 22 '17 21:05

deanWombourne


1 Answers

I've always assumed that without cache headers a response won't be cached :|

That's not how it works, it WILL cache responses even without Cache-Control or Expires headers, if all other criteria met. However, it will use heuristics for determining cached response freshness, as exact expiration time was not provided in response headers. NSURLCache is implemented according to section 13 of RFC 2616 and this behavior is stated there.

For more information you can check the following articles:

  • A Primer In Http–caching And Its–native Support–by–ios
  • Image Caching
like image 187
Maxim Pavlov Avatar answered Nov 17 '22 17:11

Maxim Pavlov