Ben Copsey

Fix cache-control max-age header parsing

Closes gh-109
@@ -303,6 +303,7 @@ static NSString *permanentCacheFolder = @"PermanentStore"; @@ -303,6 +303,7 @@ static NSString *permanentCacheFolder = @"PermanentStore";
303 NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString]; 303 NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString];
304 if (cacheControl) { 304 if (cacheControl) {
305 NSScanner *scanner = [NSScanner scannerWithString:cacheControl]; 305 NSScanner *scanner = [NSScanner scannerWithString:cacheControl];
  306 + [scanner scanUpToString:@"max-age" intoString:NULL];
306 if ([scanner scanString:@"max-age" intoString:NULL]) { 307 if ([scanner scanString:@"max-age" intoString:NULL]) {
307 [scanner scanString:@"=" intoString:NULL]; 308 [scanner scanString:@"=" intoString:NULL];
308 NSTimeInterval maxAge = 0; 309 NSTimeInterval maxAge = 0;
@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 #import "ASIDataCompressor.h" 24 #import "ASIDataCompressor.h"
25 25
26 // Automatically set on build 26 // Automatically set on build
27 -NSString *ASIHTTPRequestVersion = @"v1.8-14 2010-12-02"; 27 +NSString *ASIHTTPRequestVersion = @"v1.8-15 2010-12-04";
28 28
29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
30 30
@@ -237,6 +237,21 @@ @@ -237,6 +237,21 @@
237 } 237 }
238 } 238 }
239 239
  240 +- (void)testMaxAgeParsing
  241 +{
  242 + [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
  243 + [[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIUseDefaultCachePolicy];
  244 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-control-max-age-parsing"]];
  245 + [request setDownloadCache:[ASIDownloadCache sharedCache]];
  246 + [request startSynchronous];
  247 +
  248 + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-control-max-age-parsing"]];
  249 + [request setDownloadCache:[ASIDownloadCache sharedCache]];
  250 + [request startSynchronous];
  251 + BOOL success = [request didUseCachedResponse];
  252 + GHAssertTrue(success,@"Failed to use cached response");
  253 +}
  254 +
240 - (void)testCustomExpiry 255 - (void)testCustomExpiry
241 { 256 {
242 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; 257 [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];