Bug 1409405 - Implement MozURL::GetOrigin r=mayhemer draft
authorValentin Gosu <valentin.gosu@gmail.com>
Wed, 25 Oct 2017 18:03:05 +0200
changeset 686227 f47595f7a3beed55f68c6a6a27f5a78d1ab3da5b
parent 686221 bdadc78db1cb135a65cfae0ae39b6073c962b85e
child 737330 102ddee81d529d043a54e4299c25aa8b9ce18c82
push id86129
push uservalentin.gosu@gmail.com
push dateWed, 25 Oct 2017 16:03:44 +0000
reviewersmayhemer
bugs1409405
milestone58.0a1
Bug 1409405 - Implement MozURL::GetOrigin r=mayhemer MozReview-Commit-ID: H1X0dAOyij1
netwerk/base/MozURL.cpp
netwerk/base/MozURL.h
netwerk/base/rust-url-capi/src/lib.rs
netwerk/base/rust-url-capi/src/rust-url-capi.h
netwerk/test/gtest/TestMozURL.cpp
--- a/netwerk/base/MozURL.cpp
+++ b/netwerk/base/MozURL.cpp
@@ -94,16 +94,22 @@ MozURL::GetQuery(nsACString& aQuery)
 }
 
 nsresult
 MozURL::GetRef(nsACString& aRef)
 {
   return rusturl_get_fragment(mURL.get(), &aRef);
 }
 
+nsresult
+MozURL::GetOrigin(nsACString& aOrigin)
+{
+  return rusturl_get_origin(mURL.get(), &aOrigin);
+}
+
 // MozURL::Mutator
 
 // This macro ensures that the mutator is still valid, meaning it hasn't been
 // finalized, and none of the setters have returned an error code.
 #define ENSURE_VALID()                          \
   PR_BEGIN_MACRO                                \
     if (mFinalized) {                           \
       mStatus = NS_ERROR_NOT_AVAILABLE;         \
--- a/netwerk/base/MozURL.h
+++ b/netwerk/base/MozURL.h
@@ -47,16 +47,17 @@ public:
   // hostname, otherwise it will return a string of the form `{host}:{port}`
   // See: https://url.spec.whatwg.org/#default-port
   nsresult GetHostPort(nsACString& aHostPort);
   // Will return the port number, if specified, or -1
   nsresult GetPort(int32_t* aPort);
   nsresult GetFilePath(nsACString& aPath);
   nsresult GetQuery(nsACString& aQuery);
   nsresult GetRef(nsACString& aRef);
+  nsresult GetOrigin(nsACString& aOrigin);
 
 private:
   explicit MozURL(rusturl* rawPtr)
     : mURL(rawPtr)
   {
   }
   virtual ~MozURL() {}
   struct FreeRustURL
--- a/netwerk/base/rust-url-capi/src/lib.rs
+++ b/netwerk/base/rust-url-capi/src/lib.rs
@@ -226,16 +226,28 @@ pub extern "C" fn rusturl_has_fragment(u
     return NS_ERROR_INVALID_ARG;
   };
 
   *has_fragment = url.fragment().is_some();
   NS_OK
 }
 
 #[no_mangle]
+pub extern "C" fn rusturl_get_origin(urlptr: Option<&Url>, origin: &mut nsACString) -> nsresult {
+  let url = if let Some(url) = urlptr {
+    url
+  } else {
+    return NS_ERROR_INVALID_ARG;
+  };
+
+  origin.assign(&url.origin().ascii_serialization());
+  NS_OK
+}
+
+#[no_mangle]
 pub extern "C" fn rusturl_set_scheme(urlptr: Option<&mut Url>, scheme: &nsACString) -> nsresult {
   let url = if let Some(url) = urlptr {
     url
   } else {
     return NS_ERROR_INVALID_ARG;
   };
 
   let scheme_ = match str::from_utf8(scheme) {
--- a/netwerk/base/rust-url-capi/src/rust-url-capi.h
+++ b/netwerk/base/rust-url-capi/src/rust-url-capi.h
@@ -29,16 +29,17 @@ nsresult rusturl_get_username(const rust
 nsresult rusturl_get_password(const rusturl* url, nsACString* cont);
 nsresult rusturl_get_host(const rusturl* url, nsACString* cont);
 nsresult rusturl_get_port(const rusturl* url, int32_t* port);
 nsresult rusturl_get_filepath(const rusturl* url, nsACString* cont);
 nsresult rusturl_get_path(const rusturl* url, nsACString* cont);
 nsresult rusturl_get_query(const rusturl* url, nsACString* cont);
 nsresult rusturl_get_fragment(const rusturl* url, nsACString* cont);
 nsresult rusturl_has_fragment(const rusturl* url, bool* has_fragment);
+nsresult rusturl_get_origin(const rusturl* url, nsACString* origin);
 
 nsresult rusturl_set_scheme(rusturl* url, const nsACString* scheme);
 nsresult rusturl_set_username(rusturl* url, const nsACString* user);
 nsresult rusturl_set_password(rusturl* url, const nsACString* password);
 nsresult rusturl_set_host_port(rusturl* url, const nsACString* hostport);
 nsresult rusturl_set_host_and_port(rusturl* url, const nsACString* hostport);
 nsresult rusturl_set_host(rusturl* url, const nsACString* host);
 nsresult rusturl_set_port(rusturl* url, const nsACString* port);
--- a/netwerk/test/gtest/TestMozURL.cpp
+++ b/netwerk/test/gtest/TestMozURL.cpp
@@ -143,8 +143,24 @@ TEST(TestMozURL, HostPort)
   ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
   ASSERT_EQ(url->GetHostPort(out), NS_OK);
   ASSERT_TRUE(out.EqualsLiteral("example.net"));
   int32_t port;
   ASSERT_EQ(url->GetPort(&port), NS_OK);
   ASSERT_EQ(port, -1);
 }
 
+TEST(TestMozURL, Origin)
+{
+  nsAutoCString href("https://user:pass@example.net:1234/path?query#ref");
+  RefPtr<MozURL> url;
+  ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
+  nsAutoCString out;
+
+  ASSERT_EQ(url->GetOrigin(out), NS_OK);
+  ASSERT_TRUE(out.EqualsLiteral("https://example.net:1234"));
+
+  RefPtr<MozURL> url2;
+  ASSERT_EQ(MozURL::Init(getter_AddRefs(url2),
+                         NS_LITERAL_CSTRING("file:///tmp/foo")), NS_OK);
+  ASSERT_EQ(url2->GetOrigin(out), NS_OK);
+  ASSERT_TRUE(out.EqualsLiteral("null"));
+}