Revision cc176feb asi-http-request-with-pithos/Classes/ASIDownloadCache.m
b/asi-http-request-with-pithos/Classes/ASIDownloadCache.m | ||
---|---|---|
14 | 14 |
|
15 | 15 |
static NSString *sessionCacheFolder = @"SessionStore"; |
16 | 16 |
static NSString *permanentCacheFolder = @"PermanentStore"; |
17 |
static NSArray *fileExtensionsToHandleAsHTML = nil; |
|
17 | 18 |
|
18 | 19 |
@interface ASIDownloadCache () |
19 | 20 |
+ (NSString *)keyForURL:(NSURL *)url; |
... | ... | |
22 | 23 |
|
23 | 24 |
@implementation ASIDownloadCache |
24 | 25 |
|
26 |
+ (void)initialize |
|
27 |
{ |
|
28 |
if (self == [ASIDownloadCache class]) { |
|
29 |
// Obviously this is not an exhaustive list, but hopefully these are the most commonly used and this will 'just work' for the widest range of people |
|
30 |
// I imagine many web developers probably use url rewriting anyway |
|
31 |
fileExtensionsToHandleAsHTML = [[NSArray alloc] initWithObjects:@"asp",@"aspx",@"jsp",@"php",@"rb",@"py",@"pl",@"cgi", nil]; |
|
32 |
} |
|
33 |
} |
|
34 |
|
|
25 | 35 |
- (id)init |
26 | 36 |
{ |
27 | 37 |
self = [super init]; |
... | ... | |
105 | 115 |
|
106 | 116 |
- (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge |
107 | 117 |
{ |
108 |
NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]]; |
|
109 |
|
|
110 |
// If we weren't given a custom max-age, lets look for one in the response headers |
|
111 |
if (!maxAge) { |
|
112 |
NSString *cacheControl = [[responseHeaders objectForKey:@"Cache-Control"] lowercaseString]; |
|
113 |
if (cacheControl) { |
|
114 |
NSScanner *scanner = [NSScanner scannerWithString:cacheControl]; |
|
115 |
[scanner scanUpToString:@"max-age" intoString:NULL]; |
|
116 |
if ([scanner scanString:@"max-age" intoString:NULL]) { |
|
117 |
[scanner scanString:@"=" intoString:NULL]; |
|
118 |
[scanner scanDouble:&maxAge]; |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
// RFC 2612 says max-age must override any Expires header |
|
124 |
if (maxAge) { |
|
125 |
return [[NSDate date] addTimeInterval:maxAge]; |
|
126 |
} else { |
|
127 |
NSString *expires = [responseHeaders objectForKey:@"Expires"]; |
|
128 |
if (expires) { |
|
129 |
return [ASIHTTPRequest dateFromRFC1123String:expires]; |
|
130 |
} |
|
131 |
} |
|
132 |
return nil; |
|
118 |
return [ASIHTTPRequest expiryDateForRequest:request maxAge:maxAge]; |
|
133 | 119 |
} |
134 | 120 |
|
135 | 121 |
- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge |
... | ... | |
183 | 169 |
|
184 | 170 |
if ([request responseData]) { |
185 | 171 |
[[request responseData] writeToFile:dataPath atomically:NO]; |
186 |
} else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) { |
|
172 |
} else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) {
|
|
187 | 173 |
NSError *error = nil; |
188 |
[[[[NSFileManager alloc] init] autorelease] copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error]; |
|
174 |
NSFileManager* manager = [[NSFileManager alloc] init]; |
|
175 |
if ([manager fileExistsAtPath:dataPath]) { |
|
176 |
[manager removeItemAtPath:dataPath error:&error]; |
|
177 |
} |
|
178 |
[manager copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error]; |
|
179 |
[manager release]; |
|
189 | 180 |
} |
190 | 181 |
[[self accessLock] unlock]; |
191 | 182 |
} |
... | ... | |
212 | 203 |
{ |
213 | 204 |
// Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view |
214 | 205 |
NSString *extension = [[url path] pathExtension]; |
215 |
if (![extension length]) { |
|
206 |
|
|
207 |
// If the url doesn't have an extension, we'll add one so a webview can read it when locally cached |
|
208 |
// If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason |
|
209 |
if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) { |
|
216 | 210 |
extension = @"html"; |
217 | 211 |
} |
218 | 212 |
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:extension]]; |
219 | 213 |
} |
220 | 214 |
|
215 |
+ (NSArray *)fileExtensionsToHandleAsHTML |
|
216 |
{ |
|
217 |
return fileExtensionsToHandleAsHTML; |
|
218 |
} |
|
219 |
|
|
220 |
|
|
221 | 221 |
- (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url |
222 | 222 |
{ |
223 | 223 |
return [self pathToFile:[[[self class] keyForURL:url] stringByAppendingPathExtension:@"cachedheaders"]]; |
... | ... | |
262 | 262 |
|
263 | 263 |
// Grab the file extension, if there is one. We do this so we can save the cached response with the same file extension - this is important if you want to display locally cached data in a web view |
264 | 264 |
NSString *extension = [[[request url] path] pathExtension]; |
265 |
if (![extension length]) { |
|
265 |
|
|
266 |
// If the url doesn't have an extension, we'll add one so a webview can read it when locally cached |
|
267 |
// If the url has the extension of a common web scripting language, we'll change the extension on the cached path to html for the same reason |
|
268 |
if (![extension length] || [[[self class] fileExtensionsToHandleAsHTML] containsObject:[extension lowercaseString]]) { |
|
266 | 269 |
extension = @"html"; |
267 | 270 |
} |
268 | 271 |
path = [path stringByAppendingPathComponent:[[[self class] keyForURL:[request url]] stringByAppendingPathExtension:extension]]; |
Also available in: Unified diff