SDURLCache
SDURLCache copied to clipboard
SDURLCache.m line 189 issue
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!
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.
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