Ben Copsey

Preserve user agent on redirect

Closes gh-46
@@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
23 23
24 24
25 // Automatically set on build 25 // Automatically set on build
26 -NSString *ASIHTTPRequestVersion = @"v1.6.2-20 2010-06-17"; 26 +NSString *ASIHTTPRequestVersion = @"v1.6.2-21 2010-06-17";
27 27
28 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 28 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
29 29
@@ -1736,7 +1736,14 @@ static BOOL isiPhoneOS2; @@ -1736,7 +1736,14 @@ static BOOL isiPhoneOS2;
1736 [self setRequestMethod:@"GET"]; 1736 [self setRequestMethod:@"GET"];
1737 [self setPostBody:nil]; 1737 [self setPostBody:nil];
1738 [self setPostLength:0]; 1738 [self setPostLength:0];
1739 - [self setRequestHeaders:nil]; 1739 +
  1740 + // Perhaps there are other headers we should be preserving, but it's hard to know what we need to keep and what to throw away.
  1741 + NSString *userAgent = [[self requestHeaders] objectForKey:@"User-Agent"];
  1742 + if (userAgent) {
  1743 + [self setRequestHeaders:[NSMutableDictionary dictionaryWithObject:userAgent forKey:@"User-Agent"]];
  1744 + } else {
  1745 + [self setRequestHeaders:nil];
  1746 + }
1740 [self setHaveBuiltRequestHeaders:NO]; 1747 [self setHaveBuiltRequestHeaders:NO];
1741 } else { 1748 } else {
1742 1749
@@ -447,6 +447,13 @@ @@ -447,6 +447,13 @@
447 447
448 success = ([[[request originalURL] absoluteString] isEqualToString:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/301"]); 448 success = ([[[request originalURL] absoluteString] isEqualToString:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/301"]);
449 GHAssertTrue(success,@"Failed to preserve original url"); 449 GHAssertTrue(success,@"Failed to preserve original url");
  450 +
  451 + // Ensure user agent is preserved
  452 + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/redirect/301"]];
  453 + [request addRequestHeader:@"User-Agent" value:@"test"];
  454 + [request startSynchronous];
  455 + success = ([[[request requestHeaders] objectForKey:@"User-Agent"] isEqualToString:@"test"]);
  456 + GHAssertTrue(success,@"Failed to preserve original user agent on redirect");
450 } 457 }
451 458
452 // Using a persistent connection for HTTP 305-307 would cause crashes on the redirect, not really sure why 459 // Using a persistent connection for HTTP 305-307 would cause crashes on the redirect, not really sure why
@@ -1604,5 +1611,6 @@ @@ -1604,5 +1611,6 @@
1604 } 1611 }
1605 1612
1606 1613
  1614 +
1607 @synthesize responseData; 1615 @synthesize responseData;
1608 @end 1616 @end