SDURLCache icon indicating copy to clipboard operation
SDURLCache copied to clipboard

SDURLCache.m line 189 issue

Open steveriggins opened this issue 11 years ago • 2 comments

Forgive me for my lack of cache knowledge. I was code reviewing SDURLCache.m the other day and was puzzled by line 189 in the "Expires" logic block. In this method, the code calculates "now" based on the Date in the response (or now if none) If a cached response header has a date of yesterday morning, and an expires of this morning, this method appears to calculate an interval of 24 hours, and then returns an expiration date based on 24 hours from now

Am I reading that entirely incorrectly? Or is it some case where if there is an Expires header, there won't be a Date header?

Thanks!

steveriggins avatar Oct 27 '13 18:10 steveriggins

This complies to the HTTP spec. To prevent from clock skewing, HTTP client must not use their own clock to compute expiration but server's by using the Date response header. If a server doesn't provide a Date response header, response should be considered as uncacheable.

By the way, SDURLCache is no longer necessary as iOS 5+ implemented disk cacking.

rs avatar Oct 28 '13 16:10 rs

Agreed, but you still need some code if you want to do cache introspection. "HTTP client must not use their own clock to compute expiration but server's by using the Date response header" I agree, but isn't the code in question doing what you said it should not?

// Date is read from cached response as yesterday at 8am

  NSTimeInterval expirationInterval = 0;
    NSDate *expirationDate = [NSURLCache dateFromHttpDateString:expires];

// expires is today at 8am if (expirationDate) { expirationInterval = [expirationDate timeIntervalSinceDate:date]; }

// time interval is 24 hours if (expirationInterval > 0) { // Convert remote expiration date to local expiration date return [NSDate dateWithTimeIntervalSinceNow:expirationInterval];

// Return now + 24 hours, calculated using client's clock } else

steveriggins avatar Oct 29 '13 05:10 steveriggins