Bug 1407540 - Allow MozURL to be constructed with a base URL r=mayhemer

MozReview-Commit-ID: Fg2aDJPhwQO

--HG--
extra : rebase_source : 47294abc8cd339143c1deaac0dc3449602fe53f1
This commit is contained in:
Valentin Gosu 2017-10-11 12:18:28 +02:00
Родитель 4ed35fcf54
Коммит e553e05838
6 изменённых файлов: 39 добавлений и 12 удалений

Просмотреть файл

@ -11,9 +11,10 @@ NS_IMPL_ADDREF(MozURL)
NS_IMPL_RELEASE(MozURL)
/* static */ nsresult
MozURL::Init(const nsACString& aSpec, MozURL** aURL)
MozURL::Init(MozURL** aURL, const nsACString& aSpec, const MozURL* aBaseURL)
{
rusturl* ptr = rusturl_new(&aSpec);
rusturl* base = aBaseURL ? aBaseURL->mURL.get() : nullptr;
rusturl* ptr = rusturl_new(&aSpec, base);
if (!ptr) {
return NS_ERROR_FAILURE;
}

Просмотреть файл

@ -18,7 +18,7 @@ namespace net {
//
// RefPtr<MozURL> url;
// nsAutoCString href("http://example.com/path?query#ref");
// nsresult rv = MozURL::Init(href, getter_AddRefs(url));
// nsresult rv = MozURL::Init(getter_AddRefs(url), href);
// if (NS_SUCCEEDED(rv)) { /* use url */ }
//
// When changing the URL is needed, you need to call the Mutate() method.
@ -33,7 +33,8 @@ namespace net {
class MozURL final
{
public:
static nsresult Init(const nsACString& aSpec, MozURL** aURL);
static nsresult Init(MozURL** aURL, const nsACString& aSpec,
const MozURL* aBaseURL = nullptr);
nsresult GetScheme(nsACString& aScheme);
nsresult GetSpec(nsACString& aSpec);

Просмотреть файл

@ -58,7 +58,7 @@ RustURL::SetSpec(const nsACString & aSpec)
{
ENSURE_MUTABLE();
rusturl* ptr = rusturl_new(&aSpec);
rusturl* ptr = rusturl_new(&aSpec, nullptr);
if (!ptr) {
return NS_ERROR_FAILURE;
}

Просмотреть файл

@ -40,12 +40,19 @@ fn default_port(scheme: &str) -> Option<u32> {
}
#[no_mangle]
pub extern "C" fn rusturl_new(spec: &nsACString) -> *mut Url {
pub extern "C" fn rusturl_new(spec: &nsACString, baseptr: Option<&Url>) -> *mut Url {
let url_spec = match str::from_utf8(spec) {
Ok(spec) => spec,
Err(_) => return ptr::null_mut(),
};
if let Some(base) = baseptr {
match base.join(url_spec) {
Ok(url) => return Box::into_raw(Box::new(url)),
Err(_) => return ptr::null_mut()
};
}
match parser().parse(url_spec) {
Ok(url) => Box::into_raw(Box::new(url)),
Err(_) => return ptr::null_mut(),

Просмотреть файл

@ -19,7 +19,7 @@ extern "C" {
// The `rusturl` opaque type is equivalent to the rust type `::url::Url`
struct rusturl;
rusturl* rusturl_new(const nsACString* spec);
rusturl* rusturl_new(const nsACString* spec, const rusturl* base);
rusturl* rusturl_clone(const rusturl* url);
/* unsafe */ void rusturl_free(rusturl* url);

Просмотреть файл

@ -10,7 +10,7 @@ TEST(TestMozURL, Getters)
{
nsAutoCString href("http://user:pass@example.com/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(href, getter_AddRefs(url)), NS_OK);
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
@ -39,7 +39,7 @@ TEST(TestMozURL, Getters)
ASSERT_TRUE(out.EqualsLiteral("ref"));
url = nullptr;
ASSERT_EQ(MozURL::Init(NS_LITERAL_CSTRING(""), getter_AddRefs(url)),
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), NS_LITERAL_CSTRING("")),
NS_ERROR_FAILURE);
ASSERT_EQ(url, nullptr);
}
@ -48,7 +48,7 @@ TEST(TestMozURL, MutatorChain)
{
nsAutoCString href("http://user:pass@example.com/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(href, getter_AddRefs(url)), NS_OK);
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
RefPtr<MozURL> url2;
@ -69,7 +69,7 @@ TEST(TestMozURL, MutatorFinalizeTwice)
{
nsAutoCString href("http://user:pass@example.com/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(href, getter_AddRefs(url)), NS_OK);
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
RefPtr<MozURL> url2;
@ -89,7 +89,7 @@ TEST(TestMozURL, MutatorErrorStatus)
{
nsAutoCString href("http://user:pass@example.com/path?query#ref");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(href, getter_AddRefs(url)), NS_OK);
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
// Test that trying to set the scheme to a bad value will get you an error
@ -101,3 +101,21 @@ TEST(TestMozURL, MutatorErrorStatus)
mut.SetScheme(NS_LITERAL_CSTRING("test"));
ASSERT_EQ(mut.GetStatus(), NS_ERROR_MALFORMED_URI);
}
TEST(TestMozURL, InitWithBase)
{
nsAutoCString href("https://example.net/a/b.html");
RefPtr<MozURL> url;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url), href), NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://example.net/a/b.html"));
RefPtr<MozURL> url2;
ASSERT_EQ(MozURL::Init(getter_AddRefs(url2), NS_LITERAL_CSTRING("c.png"),
url), NS_OK);
ASSERT_EQ(url2->GetSpec(out), NS_OK);
ASSERT_TRUE(out.EqualsLiteral("https://example.net/a/c.png"));
}