Ben Copsey

Change redirection behaviour for 301, 302 http status codes to use GET as per th…

…e browser convention (rather than the rfc)
... ... @@ -21,7 +21,7 @@
#import "ASIInputStream.h"
// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.2-22 2009-12-18";
NSString *ASIHTTPRequestVersion = @"v1.2-23 2009-12-18";
// We use our own custom run loop mode as CoreAnimation seems to want to hijack our threads otherwise
static CFStringRef ASIHTTPRequestRunMode = CFSTR("ASIHTTPRequest");
... ... @@ -1449,7 +1449,13 @@ static BOOL isiPhoneOS2;
// Do we need to redirect?
if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
if ([self responseStatusCode] > 300 && [self responseStatusCode] < 308 && [self responseStatusCode] != 304) {
if ([self responseStatusCode] == 303) {
// We redirect 301, 302 and 303 response codes as GET requests
// According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen
// See also:
// http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue
if ([self responseStatusCode] < 304) {
[self setRequestMethod:@"GET"];
[self setPostBody:nil];
[self setPostLength:0];
... ...
... ... @@ -303,6 +303,7 @@
- (void)testAutomaticRedirection
{
ASIHTTPRequest *request;
ASIFormDataRequest *request2;
BOOL success;
int i;
for (i=301; i<308; i++) {
... ... @@ -316,12 +317,19 @@
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];
success = [[request responseString] isEqualToString:[NSString stringWithFormat:@"Redirected content after a %hi status code",i]];
request2 = [ASIFormDataRequest requestWithURL:url];
[request2 setPostValue:@"foo" forKey:@"eep"];
[request2 start];
NSString *method = @"GET";
if (i>304) {
method = @"POST";
}
success = [[request2 responseString] isEqualToString:[NSString stringWithFormat:@"Redirected as %@ after a %hi status code",method,i]];
GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong content when redirecting after a %hi",i]);
success = ([request responseStatusCode] == 200);
success = ([request2 responseStatusCode] == 200);
GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong status code (expected %hi)",i]);
}
... ...