зеркало из https://github.com/mozilla/gecko-dev.git
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:
Родитель
4ed35fcf54
Коммит
e553e05838
|
@ -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"));
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче