Ben Copsey

Set encoding and cookies on cached response

Add tests for same
... ... @@ -255,11 +255,7 @@ static NSString *permanentCacheFolder = @"PermanentStore";
[scanner scanDouble:&maxAge];
NSDate *fetchDate = [ASIHTTPRequest dateFromRFC1123String:[cachedHeaders objectForKey:@"X-ASIHTTPRequest-Fetch-date"]];
#if (TARGET_OS_IPHONE && (!defined(__IPHONE_4_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0)) || !defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
NSDate *expiryDate = [fetchDate addTimeInterval:maxAge];
#else
NSDate *expiryDate = [fetchDate dateByAddingTimeInterval:maxAge];
#endif
NSDate *expiryDate = [[[NSDate alloc] initWithTimeInterval:maxAge sinceDate:fetchDate] autorelease];
if ([expiryDate timeIntervalSinceNow] < 0) {
[[self accessLock] unlock];
... ...
... ... @@ -539,6 +539,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
// And works out if HTTP auth is required
- (void)readResponseHeaders;
// Attempts to set the correct encoding by looking at the Content-Type header, if this is one
- (void)parseStringEncodingFromHeaders;
#pragma mark http authentication stuff
// Apply credentials to this request
... ...
... ... @@ -24,7 +24,7 @@
// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-11 2010-07-02";
NSString *ASIHTTPRequestVersion = @"v1.7-12 2010-07-02";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
... ... @@ -1719,29 +1719,7 @@ static NSOperationQueue *sharedQueue = nil;
}
// Handle response text encoding
// If the Content-Type header specified an encoding, we'll use that, otherwise we use defaultStringEncoding (which defaults to NSISOLatin1StringEncoding)
NSString *contentType = [[self responseHeaders] objectForKey:@"Content-Type"];
NSStringEncoding encoding = [self defaultResponseEncoding];
if (contentType) {
NSString *charsetSeparator = @"charset=";
NSScanner *charsetScanner = [NSScanner scannerWithString: contentType];
NSString *IANAEncoding = nil;
if ([charsetScanner scanUpToString: charsetSeparator intoString: NULL] && [charsetScanner scanLocation] < [contentType length])
{
[charsetScanner setScanLocation: [charsetScanner scanLocation] + [charsetSeparator length]];
[charsetScanner scanUpToString: @";" intoString: &IANAEncoding];
}
if (IANAEncoding) {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)IANAEncoding);
if (cfEncoding != kCFStringEncodingInvalidId) {
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
}
}
[self setResponseEncoding:encoding];
[self parseStringEncodingFromHeaders];
// Handle cookies
NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[self responseHeaders] forURL:[self url]];
... ... @@ -1882,6 +1860,33 @@ static NSOperationQueue *sharedQueue = nil;
[self requestReceivedResponseHeaders];
}
// Handle response text encoding
// If the Content-Type header specified an encoding, we'll use that, otherwise we use defaultStringEncoding (which defaults to NSISOLatin1StringEncoding)
- (void)parseStringEncodingFromHeaders
{
NSString *contentType = [[self responseHeaders] objectForKey:@"Content-Type"];
NSStringEncoding encoding = [self defaultResponseEncoding];
if (contentType) {
NSString *charsetSeparator = @"charset=";
NSScanner *charsetScanner = [NSScanner scannerWithString: contentType];
NSString *IANAEncoding = nil;
if ([charsetScanner scanUpToString: charsetSeparator intoString: NULL] && [charsetScanner scanLocation] < [contentType length]) {
[charsetScanner setScanLocation: [charsetScanner scanLocation] + [charsetSeparator length]];
[charsetScanner scanUpToString: @";" intoString: &IANAEncoding];
}
if (IANAEncoding) {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)IANAEncoding);
if (cfEncoding != kCFStringEncodingInvalidId) {
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
}
}
[self setResponseEncoding:encoding];
}
#pragma mark http authentication
- (void)saveProxyCredentialsToKeychain:(NSDictionary *)newCredentials
... ... @@ -2775,8 +2780,11 @@ static NSOperationQueue *sharedQueue = nil;
}
[theRequest setContentLength:[[[self responseHeaders] objectForKey:@"Content-Length"] longLongValue]];
[theRequest setTotalBytesRead:[self contentLength]];
[theRequest parseStringEncodingFromHeaders];
[theRequest setResponseCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]];
[theRequest setComplete:YES];
[theRequest setDownloadComplete:YES];
... ...
... ... @@ -192,6 +192,7 @@
{
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIReloadIfDifferentCachePolicy];
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:YES];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/cache-away"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
... ... @@ -240,4 +241,54 @@
GHAssertTrue(success,@"Response was empty");
}
- (void)testStringEncoding
{
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/Character-Encoding/UTF-16"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request startSynchronous];
BOOL success = ([request responseEncoding] == NSUnicodeStringEncoding);
GHAssertTrue(success,@"Got the wrong encoding back, cannot proceed with test");
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request startSynchronous];
success = [request responseEncoding] == NSUnicodeStringEncoding;
GHAssertTrue(success,@"Failed to set the correct encoding on the cached response");
[ASIHTTPRequest setDefaultCache:nil];
}
- (void)testCookies
{
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[[ASIDownloadCache sharedCache] setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/set_cookie"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request startSynchronous];
NSArray *cookies = [request responseCookies];
BOOL success = ([cookies count]);
GHAssertTrue(success,@"Got no cookies back, cannot proceed with test");
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request startSynchronous];
NSUInteger i;
for (i=0; i<[cookies count]; i++) {
if (![[[cookies objectAtIndex:i] name] isEqualToString:[[[request responseCookies] objectAtIndex:i] name]]) {
GHAssertTrue(success,@"Failed to set response cookies correctly");
return;
}
}
[ASIHTTPRequest setDefaultCache:nil];
}
@end
... ...
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.