Bug 94514 - POST result page should not appear in global history or history autocomplete results. r=bz, sr=bz
authorsdwilsh@shawnwilsher.com
Wed, 06 Feb 2008 10:18:07 -0800
changeset 11255 b697005df2bee746c9279334bbdfdf78b5d93089
parent 11254 296ac8e52d8416ceba4515b531d0373e165f6398
child 11256 d7c487aae0147e5065014eec22c2622a99403239
push idunknown
push userunknown
push dateunknown
reviewersbz, bz
bugs94514
milestone1.9b4pre
Bug 94514 - POST result page should not appear in global history or history autocomplete results. r=bz, sr=bz
docshell/base/nsDocShell.cpp
docshell/test/Makefile.in
docshell/test/bug94514-postpage.html
docshell/test/test_bug94514.html
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -8607,16 +8607,26 @@ NS_IMETHODIMP nsDocShell::MakeEditable(P
 
 nsresult
 nsDocShell::AddToGlobalHistory(nsIURI * aURI, PRBool aRedirect,
                                nsIChannel * aChannel)
 {
     if (mItemType != typeContent || !mGlobalHistory)
         return NS_OK;
 
+    // If this is a POST request, we do not want to include this in global
+    // history, so return early.
+    nsCOMPtr<nsIHttpChannel> hchan(do_QueryInterface(aChannel));
+    if (hchan) {
+        nsCAutoString type;
+        nsresult rv = hchan->GetRequestMethod(type);
+        if (NS_SUCCEEDED(rv) && type.EqualsLiteral("POST"))
+            return NS_OK;
+    }
+
     PRBool visited;
     nsresult rv = mGlobalHistory->IsVisited(aURI, &visited);
     if (NS_FAILED(rv))
         return rv;
 
     nsCOMPtr<nsIURI> referrer;
     if (aChannel)
         NS_GetReferrerFromChannel(aChannel, getter_AddRefs(referrer));
--- a/docshell/test/Makefile.in
+++ b/docshell/test/Makefile.in
@@ -49,16 +49,18 @@ DIRS += chrome \
 	$(NULL)
 
 XPCSHELL_TESTS = unit
 
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES = \
+		test_bug94514.html \
+		bug94514-postpage.html \
 		test_bug344861.html \
 		test_bug369814.html \
 		bug369814.zip       \
 		test_bug384014.html \
 		test_bug387979.html \
 		test_bug404548.html \
 		bug404548-subframe.html \
 		$(NULL)
new file mode 100644
--- /dev/null
+++ b/docshell/test/bug94514-postpage.html
@@ -0,0 +1,30 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=94514
+Specifically, this is a test page that actually submits a form.
+-->
+<head>
+  <title>Test Page for Bug 94515</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+</head>
+<body>
+<form id="testForm" method="POST">
+  <input type="submit" id="send"/>
+</form>
+
+<script class="testbody" type="text/javascript">
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+if (!window.location.href.match("posted=1")) {
+  // Here we just submit the form
+  var form = $("testForm");
+  form.action = window.location.href + "?posted=1";
+  form.submit();
+} else {
+  window.opener.finishTest();
+}
+
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/docshell/test/test_bug94514.html
@@ -0,0 +1,64 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=94514
+Specifically, this tests that a page that is obtained via a post request does
+not get added to global history.
+-->
+<head>
+  <title>Test for Bug 94515</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=94514">Mozilla Bug 94514</a>
+
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+var startURI = "http://localhost:8888/tests/docshell/test/bug94514-postpage.html";
+var postedURI = startURI + "?posted=1";
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+var ios = Cc["@mozilla.org/network/io-service;1"].
+          getService(Ci.nsIIOService);
+var startPage = ios.newURI(startURI, null, null);
+var postedPage = ios.newURI(postedURI, null, null);
+
+var gh = Cc["@mozilla.org/browser/global-history;2"].
+         getService(Ci.nsIGlobalHistory2);
+SimpleTest.ok(!gh.isVisited(startPage),
+              "Initial page does not start in global history.  Note: " +
+              "this will also fail if you run the test twice.");
+SimpleTest.ok(!gh.isVisited(postedPage),
+              "Posted page does not start in global history.");
+
+var w = window.open(startURI, "", "width=10,height=10");
+
+function finishTest()
+{
+  // We need to check that this was not added to global history.
+  SimpleTest.ok(gh.isVisited(startPage),
+                "Initial page was added to global history.");
+  SimpleTest.ok(!gh.isVisited(postedPage),
+                "Posted page was not added to global history.");
+
+  w.close();
+  SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>