// dont' save data if the operation was created to download directly to a stream. if([self.downloadStreams count] == 0) self.mutableData = [NSMutableData dataWithCapacity:size]; else self.mutableData = nil;
// if you attach a stream to the operation, MKNetworkKit will not cache the response. // Streams are usually "big data chunks" that doesn't need caching anyways.
// We have all this complicated cache handling since NSURLRequestReloadRevalidatingCacheData is not implemented // do cache processing only if the request is a "GET" method NSString *lastModified = [httpHeaders objectForKey:@"Last-Modified"]; NSString *eTag = [httpHeaders objectForKey:@"ETag"]; NSString *expiresOn = [httpHeaders objectForKey:@"Expires"];
NSString *contentType = [httpHeaders objectForKey:@"Content-Type"]; // if contentType is image,
// For images let's assume a expiry date of 7 days if there is no eTag or Last Modified. if(!eTag && !lastModified) expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultImageCacheDuration]; else expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultImageHeadRequestDuration]; }
// if there was a cacheControl entity, we would have a expiresOnDate that is not nil. // "Cache-Control" headers take precedence over "Expires" headers
expiresOn = [expiresOnDate rfc1123String];
// now remember lastModified, eTag and expires for this request in cache if(expiresOn) [self.cacheHeaders setObject:expiresOn forKey:@"Expires"]; if(lastModified) [self.cacheHeaders setObject:lastModified forKey:@"Last-Modified"]; if(eTag) [self.cacheHeaders setObject:eTag forKey:@"ETag"]; }
if ([self.mutableData length] == 0 || [self.downloadStreams count] > 0) { // This is the first batch of data // Check for a range header and make changes as neccesary NSString *rangeString = [[self request] valueForHTTPHeaderField:@"Range"]; if ([rangeString hasPrefix:@"bytes="] && [rangeString hasSuffix:@"-"]) { NSString *bytesText = [rangeString substringWithRange:NSMakeRange(6, [rangeString length] - 7)]; self.startPosition = [bytesText integerValue]; self.downloadedDataSize = self.startPosition; DLog(@"Resuming at %d bytes", self.startPosition); } } }