Bug 401564 - Test and fix for redirection losing Accept header. r=mcmanus
authorScott West <scott.west@inf.ethz.ch>
Wed, 26 Feb 2014 09:03:14 -0500
changeset 171023 3a737c64fa7433212f03ed69ff18d02f007d1a83
parent 171022 b90bf66240666f255fcca4064309942db531d60c
child 171024 2cb3948fb588ecb1a06aff98e8319d5f213797ae
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmcmanus
bugs401564
milestone30.0a1
Bug 401564 - Test and fix for redirection losing Accept header. r=mcmanus The test has a redirect where the initial GET request had Accept: application/json, and ensures that this is also the type of the redirected request. The fix is done in the setup of the replicated HttpBaseChannel, where the previous channel's Accept header is copied to the new one, if it exists.
netwerk/protocol/http/HttpBaseChannel.cpp
netwerk/test/unit/test_bug401564.js
netwerk/test/unit/xpcshell.ini
--- a/netwerk/protocol/http/HttpBaseChannel.cpp
+++ b/netwerk/protocol/http/HttpBaseChannel.cpp
@@ -1805,16 +1805,27 @@ HttpBaseChannel::SetupReplacementChannel
   // convey the referrer if one was used for this channel to the next one
   if (mReferrer)
     httpChannel->SetReferrer(mReferrer);
   // convey the mAllowPipelining flag
   httpChannel->SetAllowPipelining(mAllowPipelining);
   // convey the new redirection limit
   httpChannel->SetRedirectionLimit(mRedirectionLimit - 1);
 
+  // convey the Accept header value
+  {
+    nsAutoCString oldAcceptValue;
+    nsresult hasHeader = mRequestHead.GetHeader(nsHttp::Accept, oldAcceptValue);
+    if (NS_SUCCEEDED(hasHeader)) {
+      httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
+                                    oldAcceptValue,
+                                    false);
+    }
+  }
+
   nsCOMPtr<nsIHttpChannelInternal> httpInternal = do_QueryInterface(newChannel);
   if (httpInternal) {
     // convey the mForceAllowThirdPartyCookie flag
     httpInternal->SetForceAllowThirdPartyCookie(mForceAllowThirdPartyCookie);
     // convey the spdy flag
     httpInternal->SetAllowSpdy(mAllowSpdy);
 
     // update the DocumentURI indicator since we are being redirected.
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_bug401564.js
@@ -0,0 +1,49 @@
+/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+"use strict";
+Cu.import("resource://testing-common/httpd.js");
+
+var httpserver = null;
+const noRedirectURI = "/content";
+const pageValue = "Final page";
+const acceptType = "application/json";
+
+function redirectHandler(metadata, response)
+{
+  response.setStatusLine(metadata.httpVersion, 302, "Moved Temporarily");
+  response.setHeader("Location", noRedirectURI, false);
+}
+
+function contentHandler(metadata, response)
+{
+  do_check_eq(metadata.getHeader("Accept"), acceptType);
+  httpserver.stop(do_test_finished);
+}
+
+function dummyHandler(request, buffer)
+{
+}
+
+function run_test()
+{
+  httpserver = new HttpServer();
+  httpserver.registerPathHandler("/redirect", redirectHandler);
+  httpserver.registerPathHandler("/content", contentHandler);
+  httpserver.start(-1);
+
+  var prefs = Cc["@mozilla.org/preferences-service;1"]
+                .getService(Components.interfaces.nsIPrefBranch);
+  prefs.setBoolPref("network.http.prompt-temp-redirect", false);
+
+  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+  var chan = ios.newChannel("http://localhost:" +
+                            httpserver.identity.primaryPort + "/redirect",
+                            "",
+                            null);
+
+  chan.QueryInterface(Ci.nsIHttpChannel);
+  chan.setRequestHeader("Accept", acceptType, false);
+
+  chan.asyncOpen(new ChannelListener(dummyHandler, null), null);
+
+  do_test_pending();
+}
--- a/netwerk/test/unit/xpcshell.ini
+++ b/netwerk/test/unit/xpcshell.ini
@@ -91,16 +91,17 @@ skip-if = "FTP channel implementation ne
 [test_bug371473.js]
 [test_bug376660.js]
 [test_bug376844.js]
 [test_bug376865.js]
 [test_bug379034.js]
 [test_bug380994.js]
 [test_bug388281.js]
 [test_bug396389.js]
+[test_bug401564.js]
 [test_bug411952.js]
 [test_bug412945.js]
 [test_bug414122.js]
 [test_bug427957.js]
 [test_bug429347.js]
 [test_bug455311.js]
 [test_bug455598.js]
 [test_bug468426.js]