ASIDownloadCache.m
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// ASIDownloadCache.m
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
//
// Created by Ben Copsey on 01/05/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//
#import "ASIDownloadCache.h"
#import "ASIHTTPRequest.h"
#import <CommonCrypto/CommonHMAC.h>
static ASIDownloadCache *sharedCache = nil;
static NSString *sessionCacheFolder = @"SessionStore";
static NSString *permanentCacheFolder = @"PermanentStore";
@interface ASIDownloadCache ()
+ (NSString *)keyForRequest:(ASIHTTPRequest *)request;
+ (NSString *)responseHeader:(NSString *)header fromHeaders:(NSDictionary *)headers;
@end
@implementation ASIDownloadCache
- (id)init
{
self = [super init];
[self setDefaultCachePolicy:ASIReloadIfDifferentCachePolicy];
[self setDefaultCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[self setAccessLock:[[[NSLock alloc] init] autorelease]];
return self;
}
+ (id)sharedCache
{
if (!sharedCache) {
sharedCache = [[self alloc] init];
[sharedCache setStoragePath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"ASIHTTPRequestCache"]];
}
return sharedCache;
}
- (void)dealloc
{
[storagePath release];
[accessLock release];
[super dealloc];
}
- (void)setStoragePath:(NSString *)path
{
[[self accessLock] lock];
[storagePath release];
storagePath = [path retain];
BOOL isDirectory = NO;
NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil];
for (NSString *directory in directories) {
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
if (exists && !isDirectory) {
[NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",path];
} else if (!exists) {
[[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",path];
}
}
}
[[self accessLock] unlock];
}
- (void)storeResponseForRequest:(ASIHTTPRequest *)request
{
[[self accessLock] lock];
if ([request error] || ![request responseHeaders] || ([request responseStatusCode] != 200)) {
[[self accessLock] unlock];
return;
}
if ([self shouldRespectCacheHeaders] && ![[self class] serverAllowsResponseCachingForRequest:request]) {
[[self accessLock] unlock];
return;
}
// If the request is set to use the default policy, use this cache's default policy
ASICachePolicy policy = [request cachePolicy];
if (policy == ASIDefaultCachePolicy) {
policy = [self defaultCachePolicy];
}
if (policy == ASIIgnoreCachePolicy) {
[[self accessLock] unlock];
return;
}
NSString *path = nil;
if ([request cacheStoragePolicy] == ASICacheForSessionDurationCacheStoragePolicy) {
path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
} else {
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
}
path = [path stringByAppendingPathComponent:[[self class] keyForRequest:request]];
NSString *metadataPath = [path stringByAppendingPathExtension:@"cachedheaders"];
NSString *dataPath = [path stringByAppendingPathExtension:@"cacheddata"];
[[request responseHeaders] writeToFile:metadataPath atomically:NO];
if ([request responseData]) {
[[request responseData] writeToFile:path atomically:NO];
} else if ([request downloadDestinationPath]) {
[[NSFileManager defaultManager] copyPath:[request downloadDestinationPath] toPath:dataPath handler:nil];
}
[[self accessLock] unlock];
}
- (NSDictionary *)cachedHeadersForRequest:(ASIHTTPRequest *)request
{
if (![self storagePath]) {
return nil;
}
// Look in the session store
NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
return [NSDictionary dictionaryWithContentsOfFile:dataPath];
}
// Look in the permanent store
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
return [NSDictionary dictionaryWithContentsOfFile:dataPath];
}
return nil;
}
- (NSData *)cachedResponseDataForRequest:(ASIHTTPRequest *)request
{
NSString *path = [self pathToCachedResponseDataForRequest:request];
if (path) {
return [NSData dataWithContentsOfFile:path];
}
return nil;
}
- (NSString *)pathToCachedResponseDataForRequest:(ASIHTTPRequest *)request
{
if (![self storagePath]) {
return nil;
}
// Look in the session store
NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
return dataPath;
}
// Look in the permanent store
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
return dataPath;
}
return nil;
}
- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request
{
if (![self storagePath]) {
return NO;
}
NSDictionary *cachedHeaders = [self cachedHeadersForRequest:request];
if (!cachedHeaders) {
return NO;
}
NSString *dataPath = [self pathToCachedResponseDataForRequest:request];
if (!dataPath) {
return NO;
}
NSArray *headersToCompare = [NSArray arrayWithObjects:@"etag",@"last-modified",nil];
for (NSString *header in headersToCompare) {
if (![[[self class] responseHeader:header fromHeaders:[request responseHeaders]] isEqualToString:[[self class] responseHeader:header fromHeaders:cachedHeaders]]) {
return NO;
}
}
return YES;
}
- (BOOL)useDataFromCacheForRequest:(ASIHTTPRequest *)request
{
if (![self storagePath]) {
return NO;
}
NSDictionary *headers = [self cachedHeadersForRequest:request];
if (!headers) {
return NO;
}
NSString *dataPath = [self pathToCachedResponseDataForRequest:request];
if (!dataPath) {
return NO;
}
[request setResponseHeaders:headers];
if ([request downloadDestinationPath]) {
[request setDownloadDestinationPath:dataPath];
} else {
[request setRawResponseData:[NSMutableData dataWithData:[self cachedResponseDataForRequest:request]]];
}
return YES;
}
- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy
{
[[self accessLock] lock];
if (cachePolicy == ASIDefaultCachePolicy) {
defaultCachePolicy = ASIReloadIfDifferentCachePolicy;
} else {
defaultCachePolicy = cachePolicy;
}
[[self accessLock] unlock];
}
- (void)setDefaultCacheStoragePolicy:(ASICacheStoragePolicy)storagePolicy
{
[[self accessLock] lock];
defaultCacheStoragePolicy = storagePolicy;
[[self accessLock] unlock];
}
- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)storagePolicy
{
[[self accessLock] lock];
if (![self storagePath]) {
[[self accessLock] unlock];
return;
}
NSString *path;
if (storagePolicy == ASICacheForSessionDurationCacheStoragePolicy) {
path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder];
} else if (storagePolicy == ASICachePermanentlyCacheStoragePolicy) {
path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder];
}
BOOL isDirectory = NO;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
if (exists && !isDirectory || !exists) {
return;
}
NSError *error = nil;
NSArray *cacheFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
if (error) {
[NSException raise:@"FailedToTraverseCacheDirectory" format:@"Listing cache directory failed at path '%@'",path];
}
for (NSString *file in cacheFiles) {
NSString *extension = [file pathExtension];
if ([extension isEqualToString:@"cacheddata"] || [extension isEqualToString:@"cachedheaders"]) {
[[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error];
if (error) {
[NSException raise:@"FailedToRemoveCacheFile" format:@"Failed to remove cached data at path '%@'",path];
}
}
}
[[self accessLock] unlock];
}
+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request
{
NSString *cacheControl = [[[self class] responseHeader:@"cache-control" fromHeaders:[request responseHeaders]] lowercaseString];
if (cacheControl) {
if ([cacheControl isEqualToString:@"no-cache"] || [cacheControl isEqualToString:@"no-store"]) {
return NO;
}
}
NSString *pragma = [[[self class] responseHeader:@"pragma" fromHeaders:[request responseHeaders]] lowercaseString];
if (pragma) {
if ([pragma isEqualToString:@"no-cache"]) {
return NO;
}
}
return YES;
}
// Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa
+ (NSString *)keyForRequest:(ASIHTTPRequest *)request
{
const char *cStr = [[[request url] absoluteString] UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]];
}
+ (NSString *)responseHeader:(NSString *)header fromHeaders:(NSDictionary *)headers
{
for (NSString *responseHeader in headers) {
if ([[responseHeader lowercaseString] isEqualToString:header]) {
return [headers objectForKey:responseHeader];
}
}
return nil;
}
@synthesize storagePath;
@synthesize defaultCachePolicy;
@synthesize defaultCacheStoragePolicy;
@synthesize accessLock;
@synthesize shouldRespectCacheHeaders;
@end