Skip to content

Commit

Permalink
Make ASIDownloadCache's expiryDateForRequest a static method on ASIHT…
Browse files Browse the repository at this point in the history
…TPRequest so that the code can be reused.

The functionality is common enough that it may be advantageous for
a third party looking at the request headers to use this method to
calculate the request's expiry date.

Unit tests:

  No new tests.

Documentation:

  Documented the new method +expiryDateForRequest:maxAge:
  • Loading branch information
jverkoey committed Jul 3, 2011
1 parent 1710da9 commit 7e70179
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
26 changes: 1 addition & 25 deletions Classes/ASIDownloadCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,7 @@ - (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)

- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge
{
NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]];

// If we weren't given a custom max-age, lets look for one in the response headers
if (!maxAge) {
NSString *cacheControl = [[responseHeaders objectForKey:@"Cache-Control"] lowercaseString];
if (cacheControl) {
NSScanner *scanner = [NSScanner scannerWithString:cacheControl];
[scanner scanUpToString:@"max-age" intoString:NULL];
if ([scanner scanString:@"max-age" intoString:NULL]) {
[scanner scanString:@"=" intoString:NULL];
[scanner scanDouble:&maxAge];
}
}
}

// RFC 2612 says max-age must override any Expires header
if (maxAge) {
return [[NSDate date] addTimeInterval:maxAge];
} else {
NSString *expires = [responseHeaders objectForKey:@"Expires"];
if (expires) {
return [ASIHTTPRequest dateFromRFC1123String:expires];
}
}
return nil;
return [ASIHTTPRequest expiryDateForRequest:request maxAge:maxAge];
}

- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge
Expand Down
5 changes: 5 additions & 0 deletions Classes/ASIHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ typedef void (^ASIDataBlock)(NSData *data);
// And also by ASIS3Request
+ (NSString *)base64forData:(NSData *)theData;

// Returns the expiration date for the request.
// Calculated from the Expires response header property, unless maxAge is non-zero or
// there exists a non-zero max-age property in the Cache-Control response header.
+ (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;

// Returns a date from a string in RFC1123 format
+ (NSDate *)dateFromRFC1123String:(NSString *)string;

Expand Down
29 changes: 29 additions & 0 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -4824,6 +4824,35 @@ + (NSString*)base64forData:(NSData*)theData {
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

+ (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge
{
NSDictionary *responseHeaders = [request responseHeaders];

// If we weren't given a custom max-age, lets look for one in the response headers
if (!maxAge) {
NSString *cacheControl = [[responseHeaders objectForKey:@"Cache-Control"] lowercaseString];
if (cacheControl) {
NSScanner *scanner = [NSScanner scannerWithString:cacheControl];
[scanner scanUpToString:@"max-age" intoString:NULL];
if ([scanner scanString:@"max-age" intoString:NULL]) {
[scanner scanString:@"=" intoString:NULL];
[scanner scanDouble:&maxAge];
}
}
}

// RFC 2612 says max-age must override any Expires header
if (maxAge) {
return [[NSDate date] addTimeInterval:maxAge];
} else {
NSString *expires = [responseHeaders objectForKey:@"Expires"];
if (expires) {
return [ASIHTTPRequest dateFromRFC1123String:expires];
}
}
return nil;
}

// Based on hints from http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter
+ (NSDate *)dateFromRFC1123String:(NSString *)string
{
Expand Down

0 comments on commit 7e70179

Please sign in to comment.