Ben Copsey

ASIDownloadCache: If we get a max-age, ignore any Expires header

closes gh-108
@@ -299,14 +299,6 @@ static NSString *permanentCacheFolder = @"PermanentStore"; @@ -299,14 +299,6 @@ static NSString *permanentCacheFolder = @"PermanentStore";
299 299
300 if ([self shouldRespectCacheControlHeaders]) { 300 if ([self shouldRespectCacheControlHeaders]) {
301 301
302 - // Look for an Expires header to see if the content is out of date  
303 - NSString *expires = [cachedHeaders objectForKey:@"Expires"];  
304 - if (expires) {  
305 - if ([[ASIHTTPRequest dateFromRFC1123String:expires] timeIntervalSinceNow] >= 0) {  
306 - [[self accessLock] unlock];  
307 - return YES;  
308 - }  
309 - }  
310 // Look for a max-age header 302 // Look for a max-age header
311 NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString]; 303 NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString];
312 if (cacheControl) { 304 if (cacheControl) {
@@ -315,17 +307,29 @@ static NSString *permanentCacheFolder = @"PermanentStore"; @@ -315,17 +307,29 @@ static NSString *permanentCacheFolder = @"PermanentStore";
315 [scanner scanString:@"=" intoString:NULL]; 307 [scanner scanString:@"=" intoString:NULL];
316 NSTimeInterval maxAge = 0; 308 NSTimeInterval maxAge = 0;
317 [scanner scanDouble:&maxAge]; 309 [scanner scanDouble:&maxAge];
318 - NSDate *fetchDate = [ASIHTTPRequest dateFromRFC1123String:[cachedHeaders objectForKey:@"X-ASIHTTPRequest-Fetch-date"]];  
319 310
  311 + NSDate *fetchDate = [ASIHTTPRequest dateFromRFC1123String:[cachedHeaders objectForKey:@"X-ASIHTTPRequest-Fetch-date"]];
320 NSDate *expiryDate = [[[NSDate alloc] initWithTimeInterval:maxAge sinceDate:fetchDate] autorelease]; 312 NSDate *expiryDate = [[[NSDate alloc] initWithTimeInterval:maxAge sinceDate:fetchDate] autorelease];
321 313
322 if ([expiryDate timeIntervalSinceNow] >= 0) { 314 if ([expiryDate timeIntervalSinceNow] >= 0) {
323 [[self accessLock] unlock]; 315 [[self accessLock] unlock];
324 return YES; 316 return YES;
325 } 317 }
  318 + // RFC 2612 says max-age must override any Expires header
  319 + [[self accessLock] unlock];
  320 + return NO;
326 } 321 }
327 } 322 }
328 - 323 +
  324 + // Look for an Expires header to see if the content is out of date
  325 + NSString *expires = [cachedHeaders objectForKey:@"Expires"];
  326 + if (expires) {
  327 + if ([[ASIHTTPRequest dateFromRFC1123String:expires] timeIntervalSinceNow] >= 0) {
  328 + [[self accessLock] unlock];
  329 + return YES;
  330 + }
  331 + }
  332 +
329 // No explicit expiration time sent by the server 333 // No explicit expiration time sent by the server
330 [[self accessLock] unlock]; 334 [[self accessLock] unlock];
331 return NO; 335 return NO;