Permalink
Browse files

Added automatic redirection for 30x headers (on by default)

  • Loading branch information...
1 parent 951834f commit 44e5aee307d9bead97a4eec35fae9f6aad6efdd4 @pokeb committed May 31, 2009
Showing with 41 additions and 3 deletions.
  1. +4 −1 Classes/ASIHTTPRequest.h
  2. +7 −1 Classes/ASIHTTPRequest.m
  3. +30 −1 Classes/Tests/ASIHTTPRequestTests.m
View
5 Classes/ASIHTTPRequest.h
@@ -214,6 +214,9 @@ extern NSString* const NetworkRequestErrorDomain;
// Use HTTP 1.0 rather than 1.1 (defaults to false)
BOOL useHTTPVersionOne;
+
+ // When YES, requests will automatically redirect when they get a HTTP 30x header (defaults to YES)
+ BOOL shouldRedirect;
}
@@ -397,5 +400,5 @@ extern NSString* const NetworkRequestErrorDomain;
@property (assign) BOOL didCreateTemporaryPostDataFile;
@property (assign) BOOL useHTTPVersionOne;
@property (assign, readonly) unsigned long long partialDownloadSize;
-
+@property (assign) BOOL shouldRedirect;
@end
View
8 Classes/ASIHTTPRequest.m
@@ -88,6 +88,7 @@ - (id)initWithURL:(NSURL *)newURL
self = [super init];
[self setRequestMethod:@"GET"];
+ [self setShouldRedirect:YES];
[self setShowAccurateProgress:YES];
[self setShouldResetProgressIndicators:YES];
[self setAllowCompressedResponse:YES];
@@ -305,12 +306,13 @@ - (void)main
}
// Create a new HTTP request.
- request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)requestMethod, (CFURLRef)url, self.useHTTPVersionOne ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1);
+ request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)requestMethod, (CFURLRef)url, [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1);
if (!request) {
[self failWithError:ASIUnableToCreateRequestError];
return;
}
+
// If we've already talked to this server and have valid credentials, let's apply them to the request
if (useSessionPersistance && sessionCredentials && sessionAuthentication) {
if (!CFHTTPMessageApplyCredentialDictionary(request, sessionAuthentication, (CFMutableDictionaryRef)sessionCredentials, NULL)) {
@@ -428,6 +430,9 @@ - (void)startRequest
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create read stream",NSLocalizedDescriptionKey,nil]]];
return;
}
+
+ // Tell CFNetwork to automatically redirect for 30x status codes
+ CFReadStreamSetProperty(readStream, kCFStreamPropertyHTTPShouldAutoredirect, [self shouldRedirect] ? kCFBooleanTrue : kCFBooleanFalse);
// Set the client
CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL};
@@ -1634,4 +1639,5 @@ + (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest
@synthesize fileDownloadOutputStream;
@synthesize authenticationRetryCount;
@synthesize updatedProgress;
+@synthesize shouldRedirect;
@end
View
31 Classes/Tests/ASIHTTPRequestTests.m
@@ -126,10 +126,39 @@ - (void)testHTTPVersion
GHAssertTrue(success,@"Wrong HTTP version used");
}
+- (void)testAutomaticRedirection
+{
+ ASIHTTPRequest *request;
+ BOOL success;
+ int i;
+ for (i=301; i<308; i++) {
+ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/%hi",i]];
+ request = [ASIHTTPRequest requestWithURL:url];
+ [request setShouldRedirect:NO];
+ [request start];
+ NSLog([request responseString]);
+ if (i == 304) { // 304s will not contain a body, as per rfc2616. Will test 304 handling in a future test when we have etag support
+ continue;
+ }
+ success = [[request responseString] isEqualToString:[NSString stringWithFormat:@"Non-redirected content with %hi status code",i]];
+ GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong content when not redirecting after a %hi",i]);
+
+ request = [ASIHTTPRequest requestWithURL:url];
+ [request start];
+ NSLog([request responseString]);
+ success = [[request responseString] isEqualToString:[NSString stringWithFormat:@"Redirected content after a %hi status code",i]];
+ GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong content when redirecting after a %hi",i]);
+
+ success = ([request responseStatusCode] == 200);
+ GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong status code (expected %hi)",i]);
+
+ }
+}
+
- (void)testUploadContentLength
{
//This url will return the contents of the Content-Length request header
- NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-length"] autorelease];
+ NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/content-length"];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setPostBody:[NSMutableData dataWithLength:1024*32]];
[request start];

0 comments on commit 44e5aee

Please sign in to comment.