Ben Copsey

Added support for 307 redirects

Fix a potential lock issue with redirected requests
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 #import "ASIInputStream.h" 21 #import "ASIInputStream.h"
22 22
23 // Automatically set on build 23 // Automatically set on build
24 -NSString *ASIHTTPRequestVersion = @"v1.5-41 2010-02-03"; 24 +NSString *ASIHTTPRequestVersion = @"v1.5-42 2010-02-03";
25 25
26 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 26 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
27 27
@@ -1069,11 +1069,13 @@ static BOOL isiPhoneOS2; @@ -1069,11 +1069,13 @@ static BOOL isiPhoneOS2;
1069 // Some naughty / badly coded website is trying to force us into a redirection loop. This is not cool. 1069 // Some naughty / badly coded website is trying to force us into a redirection loop. This is not cool.
1070 [self failWithError:ASITooMuchRedirectionError]; 1070 [self failWithError:ASITooMuchRedirectionError];
1071 [self setComplete:YES]; 1071 [self setComplete:YES];
  1072 + [[self cancelledLock] unlock];
1072 } else { 1073 } else {
  1074 + [[self cancelledLock] unlock];
  1075 +
1073 // Go all the way back to the beginning and build the request again, so that we can apply any new cookies 1076 // Go all the way back to the beginning and build the request again, so that we can apply any new cookies
1074 [self main]; 1077 [self main];
1075 } 1078 }
1076 - [[self cancelledLock] unlock];  
1077 } 1079 }
1078 1080
1079 // This gets fired every 1/4 of a second to update the progress and work out if we need to timeout 1081 // This gets fired every 1/4 of a second to update the progress and work out if we need to timeout
@@ -1724,16 +1726,16 @@ static BOOL isiPhoneOS2; @@ -1724,16 +1726,16 @@ static BOOL isiPhoneOS2;
1724 } 1726 }
1725 } 1727 }
1726 // Do we need to redirect? 1728 // Do we need to redirect?
1727 - // Note that ASIHTTPRequest does not currently support 305 Use Proxy or 307 Temporary Redirect 1729 + // Note that ASIHTTPRequest does not currently support 305 Use Proxy
1728 if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) { 1730 if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
1729 - if ([self responseStatusCode] > 300 && [self responseStatusCode] < 304) { 1731 + if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) {
1730 1732
1731 // By default, we redirect 301 and 302 response codes as GET requests 1733 // By default, we redirect 301 and 302 response codes as GET requests
1732 // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen 1734 // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen
1733 // See also: 1735 // See also:
1734 // http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue 1736 // http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue
1735 1737
1736 - if (![self shouldUseRFC2616RedirectBehaviour] || [self responseStatusCode] == 303) { 1738 + if ([self responseStatusCode] != 307 && (![self shouldUseRFC2616RedirectBehaviour] || [self responseStatusCode] == 303)) {
1737 [self setRequestMethod:@"GET"]; 1739 [self setRequestMethod:@"GET"];
1738 [self setPostBody:nil]; 1740 [self setPostBody:nil];
1739 [self setPostLength:0]; 1741 [self setPostLength:0];
@@ -323,7 +323,11 @@ @@ -323,7 +323,11 @@
323 ASIFormDataRequest *request2; 323 ASIFormDataRequest *request2;
324 BOOL success; 324 BOOL success;
325 unsigned int i; 325 unsigned int i;
326 - for (i=301; i<305; i++) { 326 + for (i=301; i<308; i++) {
  327 +
  328 + if (i > 304 && i < 307) {
  329 + continue;
  330 + }
327 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/%hi",i]]; 331 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/%hi",i]];
328 request = [ASIHTTPRequest requestWithURL:url]; 332 request = [ASIHTTPRequest requestWithURL:url];
329 [request setShouldRedirect:NO]; 333 [request setShouldRedirect:NO];
@@ -340,10 +344,14 @@ @@ -340,10 +344,14 @@
340 344
341 NSString *method = @"GET"; 345 NSString *method = @"GET";
342 if (i>304) { 346 if (i>304) {
343 - method = @"POST"; 347 + method = @"POST";
344 } 348 }
345 - 349 + NSString *expectedString = [NSString stringWithFormat:@"Redirected as %@ after a %hi status code",method,i];
346 - success = [[request2 responseString] isEqualToString:[NSString stringWithFormat:@"Redirected as %@ after a %hi status code",method,i]]; 350 + if (i>304) {
  351 + expectedString = [NSString stringWithFormat:@"%@\r\nWatch out for the Giant Monkey!",expectedString];
  352 + }
  353 +
  354 + success = [[request2 responseString] isEqualToString:expectedString];
347 GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong content when redirecting after a %hi",i]); 355 GHAssertTrue(success,[NSString stringWithFormat:@"Got the wrong content when redirecting after a %hi",i]);
348 356
349 success = ([request2 responseStatusCode] == 200); 357 success = ([request2 responseStatusCode] == 200);
@@ -353,6 +361,7 @@ @@ -353,6 +361,7 @@
353 361
354 // Test RFC 2616 behaviour 362 // Test RFC 2616 behaviour
355 for (i=301; i<303; i++) { 363 for (i=301; i<303; i++) {
  364 +
356 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/%hi",i]]; 365 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/%hi",i]];
357 request2 = [ASIFormDataRequest requestWithURL:url]; 366 request2 = [ASIFormDataRequest requestWithURL:url];
358 [request2 setPostValue:@"Giant Monkey" forKey:@"lookbehindyou"]; 367 [request2 setPostValue:@"Giant Monkey" forKey:@"lookbehindyou"];