artifacts / AFCache

AFCache is an HTTP cache for iOS and OSX seeking towards full RFC2616 compliance
Apache License 2.0
361 stars 43 forks source link

AFCacheableItemInfo returning MIME type of 'plain/text' after first use #3

Open tonyarnold opened 13 years ago

tonyarnold commented 13 years ago

I've got an odd problem where I'm getting a MIME type of 'plain/text' on items that were previously 'image/jpeg' or 'video/mp4'. It seems to happen only after I've quit and restarted my app, or after I've recalled the item from cache already. Here's how I'm retrieving items from cache:

Am I doing something wrong?

dqueffeulouatw commented 13 years ago

I have a similar problem. I've made a modification in AFURLCache that seems to fix the problem : I have to split the content-type and the charset :

-(NSCachedURLResponse)cachedResponseForRequest:(NSURLRequest)request { NSURL* url = request.URL; AFCacheableItem* item = [[AFCache sharedInstance] cacheableItemFromCacheStore:url]; if (item && item.cacheStatus == kCacheStatusFresh && item.data) { NSRange oRange = [item.info.mimeType rangeOfString:@"charset="]; NSString* oMimeType = item.info.mimeType; NSString* oCharset = nil; if (oRange.location != NSNotFound) { oMimeType = [[[item.info.mimeType substringToIndex:oRange.location] stringByReplacingOccurrencesOfString:@";" withString:@""] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; oCharset = [[item.info.mimeType substringFromIndex:(oRange.location + oRange.length)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; } NSURLResponse* response = [[NSURLResponse alloc] initWithURL:item.url MIMEType:oMimeType expectedContentLength:[item.data length] textEncodingName:oCharset]; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:item.data userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly]; [response release]; NSLog(@"Cache HIT for file: %@", [[AFCache sharedInstance] filenameForURL: url]); return [cachedResponse autorelease]; } else { NSLog(@"Cache miss for file: %@", [[AFCache sharedInstance] filenameForURL: url]); }

NSCachedURLResponse *response = [super cachedResponseForRequest:request];
return response;

}

Also I added a test on item.data because sometimes I got nil which ends with a blank page in UIWebView.