/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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/. */
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)
if (isRefValid) {
rv = aStream->ReadCString(mRef); if (NS_FAILED(rv)) return rv;
} else {
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
}
if (isQueryValid) {
rv = aStream->ReadCString(mQuery); if (NS_FAILED(rv)) return rv;
} else {
mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
}
bool nsSimpleURI::Deserialize(const URIParams& aParams) { if (aParams.type() != URIParams::TSimpleURIParams) {
NS_ERROR("Received unknown parameters from the other process!"); returnfalse;
}
int32_t colonPos = spec.FindChar(':');
MOZ_ASSERT(colonPos != kNotFound, "A colon should be in this string"); // This sets mPath, mQuery and mRef. return SetPathQueryRefInternal(Substring(spec, colonPos + 1));
}
NS_IMETHODIMP
nsSimpleURI::GetScheme(nsACString& result) {
result = mScheme; return NS_OK;
}
NS_IMETHODIMP
nsSimpleURI::GetHostPort(nsACString& result) { // Note: Audit all callers before changing this to return an empty // string -- CAPS and UI code may depend on this throwing. // Note: If this is changed, change GetAsciiHostPort as well. return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSimpleURI::GetHost(nsACString& result) { // Note: Audit all callers before changing this to return an empty // string -- CAPS and UI code depend on this throwing. return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSimpleURI::GetPort(int32_t* result) { // Note: Audit all callers before changing this to return an empty // string -- CAPS and UI code may depend on this throwing. return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSimpleURI::GetPathQueryRef(nsACString& result) {
result = mPath; if (mIsQueryValid) {
result += "?"_ns + mQuery;
} if (mIsRefValid) {
result += "#"_ns + mRef;
}
// Find the first instance of ? or # that marks the end of the path. auto hashOrQueryFilter = [](char c) { return c == '?' || c == '#'; }; constauto* pathEnd = std::find_if(start, end, hashOrQueryFilter);
mIsQueryValid = false;
mQuery.Truncate();
mIsRefValid = false;
mRef.Truncate();
// The path if (!mPath.Assign(Substring(start, pathEnd), fallible)) { return NS_ERROR_OUT_OF_MEMORY;
}
if (pathEnd == end) { return NS_OK;
}
constauto* queryEnd =
std::find_if(pathEnd, end, [](char c) { return c == '#'; });
// 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;
}
}
if (queryEnd == end) { return NS_OK;
}
return SetRef(Substring(queryEnd, end));
}
NS_IMETHODIMP
nsSimpleURI::GetRef(nsACString& result) { if (!mIsRefValid) {
MOZ_ASSERT(mRef.IsEmpty(), "mIsRefValid/mRef invariant broken");
result.Truncate();
} else {
result = mRef;
}
return NS_OK;
}
// 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) { if (StaticPrefs::network_url_max_length() &&
aRef.Length() > StaticPrefs::network_url_max_length()) { return NS_ERROR_MALFORMED_URI;
}
if (ref.IsEmpty()) { // Empty string means to remove ref completely.
mIsRefValid = false;
mRef.Truncate(); // invariant: mRef should be empty when it's not valid
// Trim trailing invalid chars when ref and query are removed if (mScheme != "javascript" && mRef.IsEmpty() && mQuery.IsEmpty()) {
TrimTrailingCharactersFromPath();
}
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(); return NS_OK;
}
NS_IMETHODIMP
nsSimpleURI::GetAsciiSpec(nsACString& aResult) {
nsresult rv = GetSpec(aResult); if (NS_FAILED(rv)) return rv;
MOZ_ASSERT(IsAscii(aResult), "The spec should be ASCII"); return NS_OK;
}
nsresult nsSimpleURI::SetFilePath(const nsACString& aFilePath) { if (mPath.IsEmpty() || mPath.First() != '/') { // cannot-be-a-base return NS_ERROR_MALFORMED_URI;
} constchar* current = aFilePath.BeginReading(); constchar* 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));
}
if (query.IsEmpty()) { // Empty string means to remove query completely.
mIsQueryValid = false;
mQuery.Truncate(); // invariant: mQuery should be empty when it's not valid
// Trim trailing invalid chars when ref and query are removed if (mScheme != "javascript" && mRef.IsEmpty() && mQuery.IsEmpty()) {
TrimTrailingCharactersFromPath();
}
auto charFilter = [](char c) { returnstatic_cast<uint8_t>(c) > 0x20; }; constauto* 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);
}
}
// Queries this list of interfaces. If none match, it queries mURI.
NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleURI::Mutator, nsIURISetters,
nsIURIMutator, nsISerializable,
nsISimpleURIMutator)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.