2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-07-01 11:52:52 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2013-11-21 02:55:44 +04:00
|
|
|
#undef LOG
|
2021-06-09 07:56:48 +03:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-11 15:35:10 +04:00
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
#include "nsSimpleURI.h"
|
|
|
|
#include "nscore.h"
|
2021-05-05 21:09:28 +03:00
|
|
|
#include "nsCRT.h"
|
1999-07-01 11:52:52 +04:00
|
|
|
#include "nsString.h"
|
2000-02-03 06:44:22 +03:00
|
|
|
#include "nsURLHelper.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsNetCID.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsEscape.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2019-09-18 11:26:34 +03:00
|
|
|
#include "mozilla/TextUtils.h"
|
2012-08-23 23:33:46 +04:00
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2020-09-29 14:41:26 +03:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2017-11-20 19:11:30 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2019-05-28 16:50:00 +03:00
|
|
|
#include "mozilla/net/MozURL.h"
|
2023-02-28 13:20:16 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2012-08-23 23:33:46 +04:00
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
1999-07-01 11:52:52 +04:00
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
static NS_DEFINE_CID(kThisSimpleURIImplementationCID,
|
|
|
|
NS_THIS_SIMPLEURI_IMPLEMENTATION_CID);
|
|
|
|
|
2019-02-26 01:11:11 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<nsSimpleURI> nsSimpleURI::From(nsIURI* aURI) {
|
2017-07-07 12:29:44 +03:00
|
|
|
RefPtr<nsSimpleURI> uri;
|
|
|
|
nsresult rv = aURI->QueryInterface(kThisSimpleURIImplementationCID,
|
|
|
|
getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri.forget();
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:41:26 +03:00
|
|
|
NS_IMPL_CLASSINFO(nsSimpleURI, nullptr, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_SIMPLEURI_CID)
|
|
|
|
// Empty CI getter. We only need nsIClassInfo for Serialization
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER0(nsSimpleURI)
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsSimpleURI methods:
|
|
|
|
|
2010-06-13 00:40:05 +04:00
|
|
|
NS_IMPL_ADDREF(nsSimpleURI)
|
|
|
|
NS_IMPL_RELEASE(nsSimpleURI)
|
|
|
|
NS_INTERFACE_TABLE_HEAD(nsSimpleURI)
|
2020-09-29 14:41:26 +03:00
|
|
|
NS_INTERFACE_TABLE(nsSimpleURI, nsIURI, nsISerializable)
|
2010-06-13 00:40:05 +04:00
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE
|
2020-09-29 14:41:26 +03:00
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsSimpleURI)
|
2021-04-06 11:37:40 +03:00
|
|
|
if (aIID.Equals(kThisSimpleURIImplementationCID)) {
|
2010-06-13 00:40:05 +04:00
|
|
|
foundInterface = static_cast<nsIURI*>(this);
|
2021-04-06 11:37:40 +03:00
|
|
|
} else
|
2012-02-20 07:51:48 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISizeOf)
|
2010-06-13 00:40:05 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
1999-07-01 11:52:52 +04:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsISerializable methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-03-19 22:22:32 +03:00
|
|
|
nsSimpleURI::Read(nsIObjectInputStream* aStream) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
|
2018-03-19 22:22:32 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsSimpleURI::ReadPrivate(nsIObjectInputStream* aStream) {
|
2001-07-31 23:05:34 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2013-01-11 05:56:51 +04:00
|
|
|
bool isMutable;
|
2011-05-22 05:12:45 +04:00
|
|
|
rv = aStream->ReadBoolean(&isMutable);
|
2006-05-02 22:54:19 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-05-09 19:21:24 +03:00
|
|
|
Unused << isMutable;
|
2006-05-02 22:54:19 +04:00
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
rv = aStream->ReadCString(mScheme);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
rv = aStream->ReadCString(mPath);
|
2011-05-22 05:12:45 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isRefValid;
|
2011-05-22 05:12:45 +04:00
|
|
|
rv = aStream->ReadBoolean(&isRefValid);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2011-05-22 05:12:45 +04:00
|
|
|
mIsRefValid = isRefValid;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
if (isRefValid) {
|
2011-05-22 05:12:45 +04:00
|
|
|
rv = aStream->ReadCString(mRef);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2011-05-22 05:12:45 +04:00
|
|
|
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-05-22 05:12:45 +04:00
|
|
|
|
|
|
|
bool isQueryValid;
|
2011-05-24 19:40:10 +04:00
|
|
|
rv = aStream->ReadBoolean(&isQueryValid);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mIsQueryValid = isQueryValid;
|
2011-05-22 05:12:45 +04:00
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
if (isQueryValid) {
|
|
|
|
rv = aStream->ReadCString(mQuery);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2016-11-14 15:04:33 +03:00
|
|
|
mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Write(nsIObjectOutputStream* aStream) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2018-05-09 19:21:24 +03:00
|
|
|
rv = aStream->WriteBoolean(false); // former mMutable
|
2006-05-02 22:54:19 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = aStream->WriteStringZ(mScheme.get());
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = aStream->WriteStringZ(mPath.get());
|
2016-11-14 15:04:33 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
rv = aStream->WriteBoolean(mIsRefValid);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
if (mIsRefValid) {
|
|
|
|
rv = aStream->WriteStringZ(mRef.get());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-05-22 05:12:45 +04:00
|
|
|
|
|
|
|
rv = aStream->WriteBoolean(mIsQueryValid);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
if (mIsQueryValid) {
|
|
|
|
rv = aStream->WriteStringZ(mQuery.get());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
void nsSimpleURI::Serialize(URIParams& aParams) {
|
|
|
|
SimpleURIParams params;
|
2011-05-22 05:12:45 +04:00
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
params.scheme() = mScheme;
|
|
|
|
params.path() = mPath;
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
if (mIsRefValid) {
|
2012-08-23 23:33:46 +04:00
|
|
|
params.ref() = mRef;
|
2016-11-14 15:04:33 +03:00
|
|
|
} else {
|
2012-08-23 23:33:46 +04:00
|
|
|
params.ref().SetIsVoid(true);
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
|
|
|
if (mIsQueryValid) {
|
|
|
|
params.query() = mQuery;
|
|
|
|
} else {
|
|
|
|
params.query().SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
aParams = params;
|
2010-10-11 15:35:10 +04:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
bool nsSimpleURI::Deserialize(const URIParams& aParams) {
|
|
|
|
if (aParams.type() != URIParams::TSimpleURIParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
2012-08-23 23:33:46 +04:00
|
|
|
|
|
|
|
const SimpleURIParams& params = aParams.get_SimpleURIParams();
|
|
|
|
|
|
|
|
mScheme = params.scheme();
|
|
|
|
mPath = params.path();
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
if (params.ref().IsVoid()) {
|
|
|
|
mRef.Truncate();
|
|
|
|
mIsRefValid = false;
|
2016-11-14 15:04:33 +03:00
|
|
|
} else {
|
2012-08-23 23:33:46 +04:00
|
|
|
mRef = params.ref();
|
|
|
|
mIsRefValid = true;
|
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
|
|
|
if (params.query().IsVoid()) {
|
|
|
|
mQuery.Truncate();
|
|
|
|
mIsQueryValid = false;
|
|
|
|
} else {
|
|
|
|
mQuery = params.query();
|
|
|
|
mIsQueryValid = true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
return true;
|
2010-10-11 15:35:10 +04:00
|
|
|
}
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIURI methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetSpec(nsACString& result) {
|
2016-08-01 02:14:33 +03:00
|
|
|
if (!result.Assign(mScheme, fallible) || !result.Append(":"_ns, fallible) ||
|
|
|
|
!result.Append(mPath, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
if (mIsQueryValid) {
|
|
|
|
if (!result.Append("?"_ns, fallible) || !result.Append(mQuery, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2016-11-14 15:04:33 +03:00
|
|
|
MOZ_ASSERT(mQuery.IsEmpty(), "mIsQueryValid/mQuery invariant broken");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
if (mIsRefValid) {
|
2016-08-01 02:14:33 +03:00
|
|
|
if (!result.Append("#"_ns, fallible) || !result.Append(mRef, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-16 01:18:15 +04:00
|
|
|
// result may contain unescaped UTF-8 characters
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetSpecIgnoringRef(nsACString& result) {
|
|
|
|
result = mScheme + ":"_ns + mPath;
|
2016-11-14 15:04:33 +03:00
|
|
|
if (mIsQueryValid) {
|
|
|
|
result += "?"_ns + mQuery;
|
|
|
|
}
|
2011-08-16 01:18:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-11 20:09:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetDisplaySpec(nsACString& aUnicodeSpec) {
|
|
|
|
return GetSpec(aUnicodeSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetDisplayHostPort(nsACString& aUnicodeHostPort) {
|
|
|
|
return GetHostPort(aUnicodeHostPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetDisplayHost(nsACString& aUnicodeHost) {
|
|
|
|
return GetHost(aUnicodeHost);
|
|
|
|
}
|
|
|
|
|
2017-08-09 18:43:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetDisplayPrePath(nsACString& aPrePath) {
|
|
|
|
return GetPrePath(aPrePath);
|
|
|
|
}
|
|
|
|
|
2011-08-16 01:18:15 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSimpleURI::GetHasRef(bool* result) {
|
2011-08-16 01:18:15 +04:00
|
|
|
*result = mIsRefValid;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1864985: Add hasUserPass attribute to nsIURI r=necko-reviewers,valentin
We will remove the user and pass from the URL for Histories and Favicons using
nsIOService::CreateExposableURI()[1], then store them to the database. And, for
Favicons. And, for Favicons, we will also read them. Therefore, as the
frequency calling CreateExposableURI() will be higher, we want to optimize it.
Currently, CreateExposableURI() gets/copies the String of UserPass to check whether there is a user pass info in the URL[2]. Its copying will be waste. To
prevent it, implement hasUserPass in nsIURI, and refer to it instead.
[1] https://searchfox.org/mozilla-central/rev/90dce6b0223b4dc17bb10f1125b44f70951585f9/netwerk/base/nsIOService.cpp#1031
[2] https://searchfox.org/mozilla-central/rev/90dce6b0223b4dc17bb10f1125b44f70951585f9/netwerk/base/nsIOService.cpp#1017
Differential Revision: https://phabricator.services.mozilla.com/D193623
2023-11-17 23:12:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetHasUserPass(bool* result) {
|
|
|
|
*result = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:37:40 +03:00
|
|
|
nsresult nsSimpleURI::SetSpecInternal(const nsACString& aSpec,
|
|
|
|
bool aStripWhitespace) {
|
2021-10-11 15:38:29 +03:00
|
|
|
if (StaticPrefs::network_url_max_length() &&
|
|
|
|
aSpec.Length() > StaticPrefs::network_url_max_length()) {
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
|
2017-08-13 11:03:31 +03:00
|
|
|
nsresult rv = net_ExtractURLScheme(aSpec, mScheme);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2021-04-06 11:37:40 +03:00
|
|
|
rv = net_FilterAndEscapeURI(
|
|
|
|
aSpec, esc_OnlyNonASCII,
|
|
|
|
aStripWhitespace ? ASCIIMask::MaskWhitespace() : ASCIIMask::MaskCRLFTab(),
|
|
|
|
spec);
|
2016-06-27 21:10:47 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-08-13 11:03:34 +03:00
|
|
|
return rv;
|
2016-06-27 21:10:47 +03:00
|
|
|
}
|
2000-04-02 02:31:00 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t colonPos = spec.FindChar(':');
|
2017-08-13 11:03:31 +03:00
|
|
|
MOZ_ASSERT(colonPos != kNotFound, "A colon should be in this string");
|
2016-11-14 15:04:33 +03:00
|
|
|
// This sets mPath, mQuery and mRef.
|
2021-04-06 11:37:40 +03:00
|
|
|
return SetPathQueryRefInternal(Substring(spec, colonPos + 1));
|
1999-07-01 11:52:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetScheme(nsACString& result) {
|
|
|
|
result = mScheme;
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:02:05 +03:00
|
|
|
nsresult nsSimpleURI::SetScheme(const nsACString& input) {
|
|
|
|
// Strip tabs, newlines, carriage returns from input
|
|
|
|
nsAutoCString scheme(input);
|
|
|
|
scheme.StripTaggedASCII(ASCIIMask::MaskCRLFTab());
|
|
|
|
|
|
|
|
if (!net_IsValidScheme(scheme)) {
|
2016-04-30 17:08:33 +03:00
|
|
|
NS_WARNING("the given url scheme contains invalid characters");
|
2007-02-16 04:10:39 +03:00
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
mScheme = scheme;
|
2001-03-13 05:01:07 +03:00
|
|
|
ToLowerCase(mScheme);
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-05-17 11:12:40 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetPrePath(nsACString& result) {
|
|
|
|
result = mScheme + ":"_ns;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetUserPass(nsACString& result) { return NS_ERROR_FAILURE; }
|
2000-05-17 11:12:40 +04:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult nsSimpleURI::SetUserPass(const nsACString& userPass) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-05-17 11:12:40 +04:00
|
|
|
}
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetUsername(nsACString& result) { return NS_ERROR_FAILURE; }
|
1999-07-01 11:52:52 +04:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult nsSimpleURI::SetUsername(const nsACString& userName) {
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-02-03 06:44:22 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetPassword(nsACString& result) { return NS_ERROR_FAILURE; }
|
2000-02-03 06:44:22 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult nsSimpleURI::SetPassword(const nsACString& password) {
|
2000-02-03 06:44:22 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetHostPort(nsACString& result) {
|
2006-02-14 22:45:23 +03:00
|
|
|
// Note: Audit all callers before changing this to return an empty
|
|
|
|
// string -- CAPS and UI code may depend on this throwing.
|
2015-08-17 22:34:17 +03:00
|
|
|
// Note: If this is changed, change GetAsciiHostPort as well.
|
2000-02-03 06:44:22 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-08-17 21:46:14 +03:00
|
|
|
nsresult nsSimpleURI::SetHostPort(const nsACString& aValue) {
|
2000-02-03 06:44:22 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsSimpleURI::GetHost(nsACString& result) {
|
2006-02-14 22:45:23 +03:00
|
|
|
// Note: Audit all callers before changing this to return an empty
|
|
|
|
// string -- CAPS and UI code depend on this throwing.
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult nsSimpleURI::SetHost(const nsACString& host) {
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSimpleURI::GetPort(int32_t* result) {
|
2006-02-14 22:45:23 +03:00
|
|
|
// Note: Audit all callers before changing this to return an empty
|
|
|
|
// string -- CAPS and UI code may depend on this throwing.
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsSimpleURI::SetPort(int32_t port) { return NS_ERROR_FAILURE; }
|
1999-07-01 11:52:52 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-07-29 14:50:21 +03:00
|
|
|
nsSimpleURI::GetPathQueryRef(nsACString& result) {
|
2002-03-06 10:48:55 +03:00
|
|
|
result = mPath;
|
2016-11-14 15:04:33 +03:00
|
|
|
if (mIsQueryValid) {
|
|
|
|
result += "?"_ns + mQuery;
|
|
|
|
}
|
2011-05-22 05:12:45 +04:00
|
|
|
if (mIsRefValid) {
|
|
|
|
result += "#"_ns + mRef;
|
|
|
|
}
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-29 14:50:21 +03:00
|
|
|
nsresult nsSimpleURI::SetPathQueryRef(const nsACString& aPath) {
|
2021-10-11 15:38:29 +03:00
|
|
|
if (StaticPrefs::network_url_max_length() &&
|
|
|
|
aPath.Length() > StaticPrefs::network_url_max_length()) {
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
|
2017-03-06 00:55:45 +03:00
|
|
|
nsAutoCString path;
|
2021-04-06 11:37:40 +03:00
|
|
|
nsresult rv = NS_EscapeURL(aPath, esc_OnlyNonASCII, path, fallible);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2021-04-06 11:37:40 +03:00
|
|
|
return SetPathQueryRefInternal(path);
|
|
|
|
}
|
2017-08-13 11:03:28 +03:00
|
|
|
|
2021-04-06 11:37:40 +03:00
|
|
|
nsresult nsSimpleURI::SetPathQueryRefInternal(const nsACString& aPath) {
|
|
|
|
nsresult rv;
|
|
|
|
const auto* start = aPath.BeginReading();
|
|
|
|
const auto* end = aPath.EndReading();
|
2016-11-14 15:04:33 +03:00
|
|
|
|
2021-04-06 11:37:40 +03:00
|
|
|
// Find the first instance of ? or # that marks the end of the path.
|
|
|
|
auto hashOrQueryFilter = [](char c) { return c == '?' || c == '#'; };
|
|
|
|
const auto* pathEnd = std::find_if(start, end, hashOrQueryFilter);
|
2011-05-22 05:12:45 +04:00
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
mIsQueryValid = false;
|
|
|
|
mQuery.Truncate();
|
|
|
|
|
|
|
|
mIsRefValid = false;
|
|
|
|
mRef.Truncate();
|
|
|
|
|
|
|
|
// The path
|
2021-04-06 11:37:40 +03:00
|
|
|
if (!mPath.Assign(Substring(start, pathEnd), fallible)) {
|
2016-11-14 15:04:33 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:37:40 +03:00
|
|
|
if (pathEnd == end) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* queryEnd =
|
|
|
|
std::find_if(pathEnd, end, [](char c) { return c == '#'; });
|
|
|
|
|
2024-06-10 22:56:56 +03:00
|
|
|
// Bug 1874118: If the URL does not contain a '?' or '?' appears after '#',
|
|
|
|
// SetQuery will not execute, preventing TrimTrailingCharactersFromPath
|
|
|
|
if (pathEnd != end && *pathEnd == '?') {
|
|
|
|
rv = SetQuery(Substring(pathEnd, queryEnd));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:37:40 +03:00
|
|
|
if (queryEnd == end) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetRef(Substring(queryEnd, end));
|
1999-07-01 11:52:52 +04:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetRef(nsACString& result) {
|
2011-05-22 05:12:45 +04:00
|
|
|
if (!mIsRefValid) {
|
2016-11-14 15:04:33 +03:00
|
|
|
MOZ_ASSERT(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
|
|
|
|
result.Truncate();
|
2011-05-22 05:12:45 +04:00
|
|
|
} else {
|
2016-11-14 15:04:33 +03:00
|
|
|
result = mRef;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
// NOTE: SetRef("") removes our ref, whereas SetRef("#") sets it to the empty
|
|
|
|
// string (and will result in .spec and .path having a terminal #).
|
|
|
|
nsresult nsSimpleURI::SetRef(const nsACString& aRef) {
|
2021-10-11 15:38:29 +03:00
|
|
|
if (StaticPrefs::network_url_max_length() &&
|
|
|
|
aRef.Length() > StaticPrefs::network_url_max_length()) {
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
nsAutoCString ref;
|
2018-06-18 16:06:25 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_EscapeURL(aRef, esc_OnlyNonASCII | esc_Spaces, ref, fallible);
|
2016-11-14 15:04:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
if (ref.IsEmpty()) {
|
|
|
|
// Empty string means to remove ref completely.
|
|
|
|
mIsRefValid = false;
|
|
|
|
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
|
2023-02-23 16:02:05 +03:00
|
|
|
|
|
|
|
// Trim trailing invalid chars when ref and query are removed
|
|
|
|
if (mScheme != "javascript" && mRef.IsEmpty() && mQuery.IsEmpty()) {
|
|
|
|
TrimTrailingCharactersFromPath();
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
return NS_OK;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsRefValid = true;
|
2011-05-22 05:12:45 +04:00
|
|
|
|
|
|
|
// Gracefully skip initial hash
|
2016-11-14 15:04:33 +03:00
|
|
|
if (ref[0] == '#') {
|
|
|
|
mRef = Substring(ref, 1);
|
2011-05-22 05:12:45 +04:00
|
|
|
} else {
|
2016-11-14 15:04:33 +03:00
|
|
|
mRef = ref;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
1999-07-01 11:52:52 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSimpleURI::Equals(nsIURI* other, bool* result) {
|
2011-05-22 05:12:45 +04:00
|
|
|
return EqualsInternal(other, eHonorRef, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSimpleURI::EqualsExceptRef(nsIURI* other, bool* result) {
|
2011-05-22 05:12:45 +04:00
|
|
|
return EqualsInternal(other, eIgnoreRef, result);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:11:11 +03:00
|
|
|
/* virtual */
|
|
|
|
nsresult nsSimpleURI::EqualsInternal(
|
2011-05-22 05:12:45 +04:00
|
|
|
nsIURI* other, nsSimpleURI::RefHandlingEnum refHandlingMode, bool* result) {
|
2011-05-22 05:12:45 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(other);
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(result, "null pointer");
|
2011-05-22 05:12:45 +04:00
|
|
|
|
2011-05-26 00:23:07 +04:00
|
|
|
RefPtr<nsSimpleURI> otherUri;
|
|
|
|
nsresult rv = other->QueryInterface(kThisSimpleURIImplementationCID,
|
|
|
|
getter_AddRefs(otherUri));
|
2011-05-22 05:12:45 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-26 00:23:07 +04:00
|
|
|
*result = false;
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-05-26 00:23:07 +04:00
|
|
|
*result = EqualsInternal(otherUri, refHandlingMode);
|
2011-05-22 05:12:45 +04:00
|
|
|
return NS_OK;
|
2011-05-26 00:23:07 +04:00
|
|
|
}
|
2011-05-22 05:12:45 +04:00
|
|
|
|
2011-05-26 00:23:07 +04:00
|
|
|
bool nsSimpleURI::EqualsInternal(nsSimpleURI* otherUri,
|
|
|
|
RefHandlingEnum refHandlingMode) {
|
|
|
|
bool result = (mScheme == otherUri->mScheme && mPath == otherUri->mPath);
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
if (result) {
|
|
|
|
result = (mIsQueryValid == otherUri->mIsQueryValid &&
|
|
|
|
(!mIsQueryValid || mQuery == otherUri->mQuery));
|
|
|
|
}
|
|
|
|
|
2011-05-26 00:23:07 +04:00
|
|
|
if (result && refHandlingMode == eHonorRef) {
|
|
|
|
result = (mIsRefValid == otherUri->mIsRefValid &&
|
|
|
|
(!mIsRefValid || mRef == otherUri->mRef));
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
2011-05-26 00:23:07 +04:00
|
|
|
return result;
|
1999-07-01 11:52:52 +04:00
|
|
|
}
|
|
|
|
|
2001-01-31 04:33:03 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsSimpleURI::SchemeIs(const char* i_Scheme, bool* o_Equals) {
|
2019-01-11 11:09:33 +03:00
|
|
|
MOZ_ASSERT(o_Equals, "null pointer");
|
2019-01-13 21:05:39 +03:00
|
|
|
if (!i_Scheme) {
|
|
|
|
*o_Equals = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-03-13 05:01:07 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
const char* this_scheme = mScheme.get();
|
|
|
|
|
2001-03-13 05:01:07 +03:00
|
|
|
// mScheme is guaranteed to be lower case.
|
2002-03-06 10:48:55 +03:00
|
|
|
if (*i_Scheme == *this_scheme || *i_Scheme == (*this_scheme - ('a' - 'A'))) {
|
2021-05-05 21:09:28 +03:00
|
|
|
*o_Equals = nsCRT::strcasecmp(this_scheme, i_Scheme) == 0;
|
2001-03-13 05:01:07 +03:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
*o_Equals = false;
|
2001-03-13 05:01:07 +03:00
|
|
|
}
|
|
|
|
|
2001-01-31 04:33:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-02 22:54:19 +04:00
|
|
|
/* virtual */ nsSimpleURI* nsSimpleURI::StartClone(
|
2016-07-27 01:38:46 +03:00
|
|
|
nsSimpleURI::RefHandlingEnum refHandlingMode, const nsACString& newRef) {
|
|
|
|
nsSimpleURI* url = new nsSimpleURI();
|
|
|
|
SetRefOnClone(url, refHandlingMode, newRef);
|
|
|
|
return url;
|
2006-05-02 22:54:19 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:11:11 +03:00
|
|
|
/* virtual */
|
|
|
|
void nsSimpleURI::SetRefOnClone(nsSimpleURI* url,
|
|
|
|
nsSimpleURI::RefHandlingEnum refHandlingMode,
|
|
|
|
const nsACString& newRef) {
|
2016-07-27 01:38:46 +03:00
|
|
|
if (refHandlingMode == eHonorRef) {
|
|
|
|
url->mRef = mRef;
|
|
|
|
url->mIsRefValid = mIsRefValid;
|
|
|
|
} else if (refHandlingMode == eReplaceRef) {
|
|
|
|
url->SetRef(newRef);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-05-22 05:12:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsSimpleURI::Clone(nsIURI** result) {
|
2020-09-23 18:17:15 +03:00
|
|
|
return CloneInternal(eHonorRef, ""_ns, result);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
nsresult nsSimpleURI::CloneInternal(
|
2016-07-27 01:38:46 +03:00
|
|
|
nsSimpleURI::RefHandlingEnum refHandlingMode, const nsACString& newRef,
|
2002-03-06 10:48:55 +03:00
|
|
|
nsIURI** result) {
|
|
|
|
RefPtr<nsSimpleURI> url = StartClone(refHandlingMode, newRef);
|
1999-07-01 11:52:52 +04:00
|
|
|
if (!url) return NS_ERROR_OUT_OF_MEMORY;
|
2002-03-06 10:48:55 +03:00
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
url->mScheme = mScheme;
|
|
|
|
url->mPath = mPath;
|
|
|
|
|
|
|
|
url->mIsQueryValid = mIsQueryValid;
|
|
|
|
if (url->mIsQueryValid) {
|
|
|
|
url->mQuery = mQuery;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:12:45 +04:00
|
|
|
url.forget(result);
|
1999-07-01 11:52:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-03 08:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
2016-11-14 15:04:33 +03:00
|
|
|
nsSimpleURI::Resolve(const nsACString& relativePath, nsACString& result) {
|
2019-05-28 16:50:00 +03:00
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = net_ExtractURLScheme(relativePath, scheme);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
result = relativePath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = GetAsciiSpec(spec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// If getting the spec fails for some reason, preserve behaviour and just
|
|
|
|
// return the relative path.
|
|
|
|
result = relativePath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MozURL> url;
|
|
|
|
rv = MozURL::Init(getter_AddRefs(url), spec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// If parsing the current url fails, we revert to the previous behaviour
|
|
|
|
// and just return the relative path.
|
|
|
|
result = relativePath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MozURL> url2;
|
|
|
|
rv = MozURL::Init(getter_AddRefs(url2), relativePath, url);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// If parsing the relative url fails, we revert to the previous behaviour
|
|
|
|
// and just return the relative path.
|
|
|
|
result = relativePath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = url2->Spec();
|
2002-03-06 10:48:55 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-08-13 11:03:10 +03:00
|
|
|
nsSimpleURI::GetAsciiSpec(nsACString& aResult) {
|
|
|
|
nsresult rv = GetSpec(aResult);
|
2002-03-06 10:48:55 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2019-09-18 11:26:34 +03:00
|
|
|
MOZ_ASSERT(IsAscii(aResult), "The spec should be ASCII");
|
2017-08-13 11:03:10 +03:00
|
|
|
return NS_OK;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
2015-08-17 22:34:17 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetAsciiHostPort(nsACString& result) {
|
|
|
|
// XXX This behavior mimics GetHostPort.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetAsciiHost(nsACString& result) {
|
|
|
|
result.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-20 07:51:48 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// nsSimpleURI::nsISizeOf
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
size_t nsSimpleURI::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
2012-02-20 07:51:48 +04:00
|
|
|
return mScheme.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
|
|
|
mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
2016-11-14 15:04:33 +03:00
|
|
|
mQuery.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
2012-02-20 07:51:48 +04:00
|
|
|
mRef.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
size_t nsSimpleURI::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
2012-02-20 07:51:48 +04:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetFilePath(nsACString& aFilePath) {
|
|
|
|
aFilePath = mPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsSimpleURI::SetFilePath(const nsACString& aFilePath) {
|
2018-12-12 01:01:58 +03:00
|
|
|
if (mPath.IsEmpty() || mPath.First() != '/') {
|
|
|
|
// cannot-be-a-base
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
const char* current = aFilePath.BeginReading();
|
|
|
|
const char* end = aFilePath.EndReading();
|
|
|
|
|
|
|
|
// Only go up to the first ? or # symbol
|
|
|
|
for (; current < end; ++current) {
|
|
|
|
if (*current == '?' || *current == '#') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SetPathQueryRef(
|
|
|
|
nsDependentCSubstring(aFilePath.BeginReading(), current));
|
2016-11-14 15:04:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetQuery(nsACString& aQuery) {
|
|
|
|
if (!mIsQueryValid) {
|
|
|
|
MOZ_ASSERT(mQuery.IsEmpty(), "mIsQueryValid/mQuery invariant broken");
|
|
|
|
aQuery.Truncate();
|
|
|
|
} else {
|
|
|
|
aQuery = mQuery;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2023-08-09 10:01:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::GetHasQuery(bool* result) {
|
|
|
|
*result = mIsQueryValid;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
nsresult nsSimpleURI::SetQuery(const nsACString& aQuery) {
|
2021-10-11 15:38:29 +03:00
|
|
|
if (StaticPrefs::network_url_max_length() &&
|
|
|
|
aQuery.Length() > StaticPrefs::network_url_max_length()) {
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
2016-11-14 15:04:33 +03:00
|
|
|
nsAutoCString query;
|
2017-01-06 14:56:14 +03:00
|
|
|
nsresult rv = NS_EscapeURL(aQuery, esc_OnlyNonASCII, query, fallible);
|
2016-11-14 15:04:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.IsEmpty()) {
|
2017-01-06 14:56:14 +03:00
|
|
|
// Empty string means to remove query completely.
|
2016-11-14 15:04:33 +03:00
|
|
|
mIsQueryValid = false;
|
|
|
|
mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
|
2023-02-23 16:02:05 +03:00
|
|
|
|
|
|
|
// Trim trailing invalid chars when ref and query are removed
|
|
|
|
if (mScheme != "javascript" && mRef.IsEmpty() && mQuery.IsEmpty()) {
|
|
|
|
TrimTrailingCharactersFromPath();
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:04:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsQueryValid = true;
|
|
|
|
|
2017-01-06 14:56:14 +03:00
|
|
|
// Gracefully skip initial question mark
|
2016-11-14 15:04:33 +03:00
|
|
|
if (query[0] == '?') {
|
|
|
|
mQuery = Substring(query, 1);
|
|
|
|
} else {
|
|
|
|
mQuery = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-02 14:43:30 +03:00
|
|
|
nsresult nsSimpleURI::SetQueryWithEncoding(const nsACString& aQuery,
|
|
|
|
const Encoding* aEncoding) {
|
|
|
|
return SetQuery(aQuery);
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:02:05 +03:00
|
|
|
void nsSimpleURI::TrimTrailingCharactersFromPath() {
|
|
|
|
const auto* start = mPath.BeginReading();
|
|
|
|
const auto* end = mPath.EndReading();
|
|
|
|
|
|
|
|
auto charFilter = [](char c) { return static_cast<uint8_t>(c) > 0x20; };
|
|
|
|
const auto* newEnd =
|
|
|
|
std::find_if(std::reverse_iterator<decltype(end)>(end),
|
|
|
|
std::reverse_iterator<decltype(start)>(start), charFilter)
|
|
|
|
.base();
|
|
|
|
|
|
|
|
auto trailCount = std::distance(newEnd, end);
|
|
|
|
if (trailCount) {
|
|
|
|
mPath.Truncate(mPath.Length() - trailCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:32 +03:00
|
|
|
// Queries this list of interfaces. If none match, it queries mURI.
|
|
|
|
NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleURI::Mutator, nsIURISetters,
|
2021-04-06 11:37:40 +03:00
|
|
|
nsIURIMutator, nsISerializable,
|
|
|
|
nsISimpleURIMutator)
|
2017-11-20 19:11:30 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimpleURI::Mutate(nsIURIMutator** aMutator) {
|
|
|
|
RefPtr<nsSimpleURI::Mutator> mutator = new nsSimpleURI::Mutator();
|
|
|
|
nsresult rv = mutator->InitFromURI(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mutator.forget(aMutator);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|