bug 967341 - uri management r=honzab
authorPatrick McManus <mcmanus@ducksong.com>
Tue, 04 Feb 2014 11:55:02 -0500
changeset 169583 71f4051ae6b392b59e005b77bf84bcb1f754a420
parent 169582 5c6b2dcd79eba94bab375aa5071671fded2e62e6
child 169584 a304d2065f65ee3bdbd9f428583cc28c48980ac7
push id26258
push usercbook@mozilla.com
push dateThu, 20 Feb 2014 11:53:37 +0000
treeherdermozilla-central@df6148983ef9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershonzab
bugs967341
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 967341 - uri management r=honzab
netwerk/base/src/nsStandardURL.cpp
netwerk/test/mochitests/mochitest.ini
netwerk/test/mochitests/test_uri_scheme.html
--- a/netwerk/base/src/nsStandardURL.cpp
+++ b/netwerk/base/src/nsStandardURL.cpp
@@ -348,16 +348,17 @@ nsStandardURL::ShutdownGlobalObjects()
 
 void
 nsStandardURL::Clear()
 {
     mSpec.Truncate();
 
     mPort = -1;
 
+    mScheme.Reset();
     mAuthority.Reset();
     mUsername.Reset();
     mPassword.Reset();
     mHost.Reset();
     mHostEncoding = eEncoding_ASCII;
 
     mPath.Reset();
     mFilepath.Reset();
@@ -1132,21 +1133,26 @@ nsStandardURL::SetSpec(const nsACString 
     nsAutoCString buf1;
     if (net_FilterURIString(spec, buf1)) {
         spec = buf1.get();
         specLength = buf1.Length();
     }
 
     // parse the given URL...
     nsresult rv = ParseURL(spec, specLength);
-    if (NS_FAILED(rv)) return rv;
-
-    // finally, use the URLSegment member variables to build a normalized
-    // copy of |spec|
-    rv = BuildNormalizedSpec(spec);
+    if (NS_SUCCEEDED(rv)) {
+        // finally, use the URLSegment member variables to build a normalized
+        // copy of |spec|
+        rv = BuildNormalizedSpec(spec);
+    }
+
+    if (NS_FAILED(rv)) {
+        Clear();
+        return rv;
+    }
 
 #if defined(PR_LOGGING)
     if (LOG_ENABLED()) {
         LOG((" spec      = %s\n", mSpec.get()));
         LOG((" port      = %d\n", mPort));
         LOG((" scheme    = (%u,%d)\n", mScheme.mPos,    mScheme.mLen));
         LOG((" authority = (%u,%d)\n", mAuthority.mPos, mAuthority.mLen));
         LOG((" username  = (%u,%d)\n", mUsername.mPos,  mUsername.mLen));
@@ -1169,26 +1175,26 @@ nsStandardURL::SetScheme(const nsACStrin
 {
     ENSURE_MUTABLE();
 
     const nsPromiseFlatCString &scheme = PromiseFlatCString(input);
 
     LOG(("nsStandardURL::SetScheme [scheme=%s]\n", scheme.get()));
 
     if (scheme.IsEmpty()) {
-        NS_ERROR("cannot remove the scheme from an url");
+        NS_WARNING("cannot remove the scheme from an url");
         return NS_ERROR_UNEXPECTED;
     }
     if (mScheme.mLen < 0) {
-        NS_ERROR("uninitialized");
+        NS_WARNING("uninitialized");
         return NS_ERROR_NOT_INITIALIZED;
     }
 
     if (!net_IsValidScheme(scheme)) {
-        NS_ERROR("the given url scheme contains invalid characters");
+        NS_WARNING("the given url scheme contains invalid characters");
         return NS_ERROR_UNEXPECTED;
     }
 
     InvalidateCache();
 
     int32_t shift = ReplaceSegment(mScheme.mPos, mScheme.mLen, scheme);
 
     if (shift) {
@@ -1211,21 +1217,21 @@ nsStandardURL::SetUserPass(const nsACStr
 
     const nsPromiseFlatCString &userpass = PromiseFlatCString(input);
 
     LOG(("nsStandardURL::SetUserPass [userpass=%s]\n", userpass.get()));
 
     if (mURLType == URLTYPE_NO_AUTHORITY) {
         if (userpass.IsEmpty())
             return NS_OK;
-        NS_ERROR("cannot set user:pass on no-auth url");
+        NS_WARNING("cannot set user:pass on no-auth url");
         return NS_ERROR_UNEXPECTED;
     }
     if (mAuthority.mLen < 0) {
-        NS_ERROR("uninitialized");
+        NS_WARNING("uninitialized");
         return NS_ERROR_NOT_INITIALIZED;
     }
 
     InvalidateCache();
 
     if (userpass.IsEmpty()) {
         // remove user:pass
         if (mUsername.mLen > 0) {
@@ -1312,17 +1318,17 @@ nsStandardURL::SetUsername(const nsACStr
 
     const nsPromiseFlatCString &username = PromiseFlatCString(input);
 
     LOG(("nsStandardURL::SetUsername [username=%s]\n", username.get()));
 
     if (mURLType == URLTYPE_NO_AUTHORITY) {
         if (username.IsEmpty())
             return NS_OK;
-        NS_ERROR("cannot set username on no-auth url");
+        NS_WARNING("cannot set username on no-auth url");
         return NS_ERROR_UNEXPECTED;
     }
 
     if (username.IsEmpty())
         return SetUserPass(username);
 
     InvalidateCache();
 
@@ -1357,21 +1363,21 @@ nsStandardURL::SetPassword(const nsACStr
 
     const nsPromiseFlatCString &password = PromiseFlatCString(input);
 
     LOG(("nsStandardURL::SetPassword [password=%s]\n", password.get()));
 
     if (mURLType == URLTYPE_NO_AUTHORITY) {
         if (password.IsEmpty())
             return NS_OK;
-        NS_ERROR("cannot set password on no-auth url");
+        NS_WARNING("cannot set password on no-auth url");
         return NS_ERROR_UNEXPECTED;
     }
     if (mUsername.mLen <= 0) {
-        NS_ERROR("cannot set password without existing username");
+        NS_WARNING("cannot set password without existing username");
         return NS_ERROR_FAILURE;
     }
 
     InvalidateCache();
 
     if (password.IsEmpty()) {
         if (mPassword.mLen >= 0) {
             // cut(":password")
@@ -1809,17 +1815,17 @@ nsStandardURL::Resolve(const nsACString 
 
     NS_ASSERTION(mParser, "no parser: unitialized");
 
     // NOTE: there is no need for this function to produce normalized
     // output.  normalization will occur when the result is used to 
     // initialize a nsStandardURL object.
 
     if (mScheme.mLen < 0) {
-        NS_ERROR("unable to Resolve URL: this URL not initialized");
+        NS_WARNING("unable to Resolve URL: this URL not initialized");
         return NS_ERROR_NOT_INITIALIZED;
     }
 
     nsresult rv;
     URLSegment scheme;
     char *resultPath = nullptr;
     bool relative = false;
     uint32_t offset = 0;
@@ -2509,22 +2515,22 @@ nsStandardURL::EnsureFile()
                     "EnsureFile() called on a URL that doesn't support files!");
     if (mFile) {
         // Nothing to do
         return NS_OK;
     }
 
     // Parse the spec if we don't have a cached result
     if (mSpec.IsEmpty()) {
-        NS_ERROR("url not initialized");
+        NS_WARNING("url not initialized");
         return NS_ERROR_NOT_INITIALIZED;
     }
 
     if (!SegmentIs(mScheme, "file")) {
-        NS_ERROR("not a file URL");
+        NS_WARNING("not a file URL");
         return NS_ERROR_FAILURE;
     }
 
     return net_GetFileFromURLSpec(mSpec, getter_AddRefs(mFile));
 }
 
 NS_IMETHODIMP
 nsStandardURL::GetFile(nsIFile **result)
--- a/netwerk/test/mochitests/mochitest.ini
+++ b/netwerk/test/mochitests/mochitest.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 support-files =
   partial_content.sjs
   user_agent.sjs
   user_agent_update.sjs
 
 [test_partially_cached_content.html]
+[test_uri_scheme.html]
 [test_user_agent_overrides.html]
 [test_user_agent_updates.html]
 [test_user_agent_updates_reset.html]
new file mode 100644
--- /dev/null
+++ b/netwerk/test/mochitests/test_uri_scheme.html
@@ -0,0 +1,45 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+-->
+<head>
+  <title>Test for URI Manipulation</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+
+<script type="text/javascript">
+function dotest1()
+{
+  SimpleTest.waitForExplicitFinish();
+  var o = new URL("http://localhost/");
+  try { o.href = "foopy:bar:baz"; } catch(e) { }
+  o.protocol = "http:";
+  o.hostname;
+  try { o.href = "http://localhost/"; } catch(e) { }
+  ok(o.protocol, "http:");
+  dotest2();
+}
+
+function dotest2()
+{
+  var o = new URL("http://www.mozilla.org/");
+  try {
+      o.href ="aaaaaaaaaaa:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
+  } catch(e) { }
+  o.hash = "#";
+  o.pathname = "/";
+  o.protocol = "http:";
+  try { o.href = "http://localhost/"; } catch(e) { }
+  ok(o.protocol, "http:");
+  SimpleTest.finish();
+}
+</script>
+</head>
+<body onload="dotest1();">
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+</pre>
+</body>
+</html>
+