Skip to content

Commit

Permalink
Stop requests deciding to fallback to simple progress when they fail …
Browse files Browse the repository at this point in the history
…to get a content-length header during redirection

Add test for same
Tweaks
  • Loading branch information
pokeb committed Jun 30, 2010
1 parent 310ba30 commit 83dad43
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 40 deletions.
87 changes: 47 additions & 40 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -24,7 +24,7 @@

// Automatically set on build

NSString *ASIHTTPRequestVersion = @"v1.7-1 2010-06-30";
NSString *ASIHTTPRequestVersion = @"v1.7-2 2010-06-30";

NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";

Expand Down Expand Up @@ -1684,33 +1684,6 @@ - (void)readResponseHeaders
[[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials];
}
}

// See if we got a Content-length header
NSString *cLength = [responseHeaders valueForKey:@"Content-Length"];
ASIHTTPRequest *theRequest = self;
if ([self mainRequest]) {
theRequest = [self mainRequest];
}

if (cLength) {
SInt32 length = CFStringGetIntValue((CFStringRef)cLength);

// Workaround for Apache HEAD requests for dynamically generated content returning the wrong Content-Length when using gzip
if ([self mainRequest] && [self allowCompressedResponse] && length == 20 && [self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[[self mainRequest] setShowAccurateProgress:NO];
[[self mainRequest] incrementDownloadSizeBy:1];

} else {
[theRequest setContentLength:length];
if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[theRequest incrementDownloadSizeBy:[theRequest contentLength]+[theRequest partialDownloadSize]];
}
}

} else if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[theRequest setShowAccurateProgress:NO];
[theRequest incrementDownloadSizeBy:1];
}

// Handle response text encoding
// If the Content-Type header specified an encoding, we'll use that, otherwise we use defaultStringEncoding (which defaults to NSISOLatin1StringEncoding)
Expand Down Expand Up @@ -1738,13 +1711,13 @@ - (void)readResponseHeaders
[self setResponseEncoding:encoding];

// Handle cookies
NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:responseHeaders forURL:url];
NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[self responseHeaders] forURL:[self url]];
[self setResponseCookies:newCookies];

if ([self useCookiePersistence]) {

// Store cookies in global persistent store
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:url mainDocumentURL:nil];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:[self url] mainDocumentURL:nil];

// We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later
NSHTTPCookie *cookie;
Expand Down Expand Up @@ -1799,6 +1772,36 @@ - (void)readResponseHeaders

}
}

if (![self needsRedirect]) {
// See if we got a Content-length header
NSString *cLength = [responseHeaders valueForKey:@"Content-Length"];
ASIHTTPRequest *theRequest = self;
if ([self mainRequest]) {
theRequest = [self mainRequest];
}

if (cLength) {
SInt32 length = CFStringGetIntValue((CFStringRef)cLength);

// Workaround for Apache HEAD requests for dynamically generated content returning the wrong Content-Length when using gzip
if ([self mainRequest] && [self allowCompressedResponse] && length == 20 && [self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[[self mainRequest] setShowAccurateProgress:NO];
[[self mainRequest] incrementDownloadSizeBy:1];

} else {
[theRequest setContentLength:length];
if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[theRequest incrementDownloadSizeBy:[theRequest contentLength]+[theRequest partialDownloadSize]];
}
}

} else if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) {
[theRequest setShowAccurateProgress:NO];
[theRequest incrementDownloadSizeBy:1];
}
}

// Handle connection persistence
if ([self shouldAttemptPersistentConnection]) {

Expand Down Expand Up @@ -3783,6 +3786,20 @@ + (void)reachabilityChanged:(NSNotification *)note
}
#endif

#pragma mark cache

+ (void)setDefaultCache:(id <ASICacheDelegate>)cache
{
[defaultCache release];
defaultCache = [cache retain];
}

+ (id <ASICacheDelegate>)defaultCache
{
return defaultCache;
}


#pragma mark network activity

+ (BOOL)isNetworkInUse
Expand Down Expand Up @@ -3838,16 +3855,6 @@ + (void)runRequests
CFRelease(source);
}

+ (void)setDefaultCache:(id <ASICacheDelegate>)cache
{
[defaultCache release];
defaultCache = [cache retain];
}

+ (id <ASICacheDelegate>)defaultCache
{
return defaultCache;
}


#pragma mark miscellany
Expand Down
16 changes: 16 additions & 0 deletions Classes/Tests/ASIHTTPRequestTests.m
Expand Up @@ -1631,5 +1631,21 @@ - (void)testRFC1123DateParsing

}

- (void)testAccurateProgressFallback
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]];
[request setAllowCompressedResponse:NO]; // A bit hacky - my server will send a chunked response (without content length) when we don't specify that we accept gzip
[request startSynchronous];

BOOL success = ([request showAccurateProgress] == NO);
GHAssertTrue(success,@"Request failed to fall back to simple progress");

request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect_resume"]];
[request startSynchronous];

success = ([request showAccurateProgress] == YES);
GHAssertTrue(success,@"Request fell back to simple progress when redirecting");
}

@synthesize responseData;
@end

0 comments on commit 83dad43

Please sign in to comment.