mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
2a40ef2903
- Bug 1169044 - Patch 1 - Refactor setting referrer and referrer policy between fetch and XHR. r=khuey (3912ebaef)
- Bug 1150771 - Let ArrayBuffer through object Xrays. r=gabor (bed760277)
- Bug 1151385 - Fail early for cross-origin sandboxPrototype. r=gabor (3b65b1561)
- Bug 1131707 - Transparently forward the construct bit for function forwarders. r=gabor (1f5792775)
- Bug 1170311 - Stop asserting non-null argument to nsIPrincipal::{subsumes,equals}{,ConsideringDomain}. r=gabor (7e36d6683)
- Bug 1171175 - Improve BasePrincipal::IsCodebasePrincipal. r=baku (0d278e8f4)
- Bug 1174731 - patch 1 - Make searchParams attribute readonly, r=smaug (11f5d6dcf)
- Bug 1174731 - patch 2 - Make searchParams attribute readonly, r=annevk (4aa7ea1e4)
- Bug 1170097 - Part 1: Move OriginAttributeDictionary. r=bholley (63a1139dd)
- Bug 1084525 - Part 1: Create initial PromisesActor skeleton r=fitzgen (2ef0ad37d)
- Bug 1131643 - Implement a Location object;r=jlong (710fb9b79)
- Bug 1129834 - Store BreakpointActors by original location;r=jlong (67d16d37a)
- Bug 1129837 - Remove generatedLocation from BreakpointActor;r=jlongster (018a60746)
- Bug 1082837 - test cases for image redirects loaded from the imagelib cache. r=smaug, ckerschb (49d216725)
- Bug 1073352, part 2 - Enable some devtools tests. r=ejpbruel (0de7cfdc0)
- Bug 1131646 - Clean up the breakpoint code;r=jlongster (7fa9c6a76)
- Bug 1136146 - Merge the latest version of the source-map library with fx-team;r=fitzgen (983f2c2e9)
- Bug 1042976 follow up - Remove getInnerId from script actors; r=Mossop (43f935298)
- Bug 837630 - Stop hiding __proto__ from O.getOwnPropertyNames. r=Waldo,peterv,past (0f321614d)
- Bug 1138975 - Refactor breakpoint sliding for non-source mapped sources;r=jlong (9fd4be4e4)
- Fix breaking on the "load" event in the debugger (bug 1054159). r=ochameau (6b6b40e78)
- Bug 983469 - Pause on breakpoint condition exception. r=fitzgen (fb6dfab57)
- Bug 1135435 - Add UI for breakpoint condition throws. r=fitzgen (b2f49cb03)
- Bug 1137384 - Rename ThreadSources as TabSources and move it up into the TabActor. r=ejpbruel (fdf1db5d9)
369 lines
8.3 KiB
C++
369 lines
8.3 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* 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/. */
|
|
|
|
#include "URLSearchParams.h"
|
|
#include "mozilla/dom/URLSearchParamsBinding.h"
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
#include "nsDOMString.h"
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mObserver)
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearchParams)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(URLSearchParams)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
|
|
URLSearchParams::URLSearchParams(URLSearchParamsObserver* aObserver)
|
|
: mObserver(aObserver)
|
|
{
|
|
}
|
|
|
|
URLSearchParams::~URLSearchParams()
|
|
{
|
|
DeleteAll();
|
|
}
|
|
|
|
JSObject*
|
|
URLSearchParams::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return URLSearchParamsBinding::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
/* static */ already_AddRefed<URLSearchParams>
|
|
URLSearchParams::Constructor(const GlobalObject& aGlobal,
|
|
const nsAString& aInit,
|
|
ErrorResult& aRv)
|
|
{
|
|
nsRefPtr<URLSearchParams> sp = new URLSearchParams(nullptr);
|
|
sp->ParseInput(NS_ConvertUTF16toUTF8(aInit));
|
|
return sp.forget();
|
|
}
|
|
|
|
/* static */ already_AddRefed<URLSearchParams>
|
|
URLSearchParams::Constructor(const GlobalObject& aGlobal,
|
|
URLSearchParams& aInit,
|
|
ErrorResult& aRv)
|
|
{
|
|
nsRefPtr<URLSearchParams> sp = new URLSearchParams(nullptr);
|
|
sp->mSearchParams = aInit.mSearchParams;
|
|
return sp.forget();
|
|
}
|
|
|
|
void
|
|
URLSearchParams::ParseInput(const nsACString& aInput)
|
|
{
|
|
// Remove all the existing data before parsing a new input.
|
|
DeleteAll();
|
|
|
|
nsACString::const_iterator start, end;
|
|
aInput.BeginReading(start);
|
|
aInput.EndReading(end);
|
|
nsACString::const_iterator iter(start);
|
|
|
|
while (start != end) {
|
|
nsAutoCString string;
|
|
|
|
if (FindCharInReadable('&', iter, end)) {
|
|
string.Assign(Substring(start, iter));
|
|
start = ++iter;
|
|
} else {
|
|
string.Assign(Substring(start, end));
|
|
start = end;
|
|
}
|
|
|
|
if (string.IsEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
nsACString::const_iterator eqStart, eqEnd;
|
|
string.BeginReading(eqStart);
|
|
string.EndReading(eqEnd);
|
|
nsACString::const_iterator eqIter(eqStart);
|
|
|
|
nsAutoCString name;
|
|
nsAutoCString value;
|
|
|
|
if (FindCharInReadable('=', eqIter, eqEnd)) {
|
|
name.Assign(Substring(eqStart, eqIter));
|
|
|
|
++eqIter;
|
|
value.Assign(Substring(eqIter, eqEnd));
|
|
} else {
|
|
name.Assign(string);
|
|
}
|
|
|
|
nsAutoString decodedName;
|
|
DecodeString(name, decodedName);
|
|
|
|
nsAutoString decodedValue;
|
|
DecodeString(value, decodedValue);
|
|
|
|
AppendInternal(decodedName, decodedValue);
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::DecodeString(const nsACString& aInput, nsAString& aOutput)
|
|
{
|
|
nsACString::const_iterator start, end;
|
|
aInput.BeginReading(start);
|
|
aInput.EndReading(end);
|
|
|
|
nsCString unescaped;
|
|
|
|
while (start != end) {
|
|
// replace '+' with U+0020
|
|
if (*start == '+') {
|
|
unescaped.Append(' ');
|
|
++start;
|
|
continue;
|
|
}
|
|
|
|
// Percent decode algorithm
|
|
if (*start == '%') {
|
|
nsACString::const_iterator first(start);
|
|
++first;
|
|
|
|
nsACString::const_iterator second(first);
|
|
++second;
|
|
|
|
#define ASCII_HEX_DIGIT( x ) \
|
|
((x >= 0x41 && x <= 0x46) || \
|
|
(x >= 0x61 && x <= 0x66) || \
|
|
(x >= 0x30 && x <= 0x39))
|
|
|
|
#define HEX_DIGIT( x ) \
|
|
(*x >= 0x30 && *x <= 0x39 \
|
|
? *x - 0x30 \
|
|
: (*x >= 0x41 && *x <= 0x46 \
|
|
? *x - 0x37 \
|
|
: *x - 0x57))
|
|
|
|
if (first != end && second != end &&
|
|
ASCII_HEX_DIGIT(*first) && ASCII_HEX_DIGIT(*second)) {
|
|
unescaped.Append(HEX_DIGIT(first) * 16 + HEX_DIGIT(second));
|
|
start = ++second;
|
|
continue;
|
|
|
|
} else {
|
|
unescaped.Append('%');
|
|
++start;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
unescaped.Append(*start);
|
|
++start;
|
|
}
|
|
|
|
ConvertString(unescaped, aOutput);
|
|
}
|
|
|
|
void
|
|
URLSearchParams::ConvertString(const nsACString& aInput, nsAString& aOutput)
|
|
{
|
|
aOutput.Truncate();
|
|
|
|
if (!mDecoder) {
|
|
mDecoder = EncodingUtils::DecoderForEncoding("UTF-8");
|
|
if (!mDecoder) {
|
|
MOZ_ASSERT(mDecoder, "Failed to create a decoder.");
|
|
return;
|
|
}
|
|
}
|
|
|
|
int32_t inputLength = aInput.Length();
|
|
int32_t outputLength = 0;
|
|
|
|
nsresult rv = mDecoder->GetMaxLength(aInput.BeginReading(), inputLength,
|
|
&outputLength);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return;
|
|
}
|
|
|
|
if (!aOutput.SetLength(outputLength, fallible)) {
|
|
return;
|
|
}
|
|
|
|
int32_t newOutputLength = outputLength;
|
|
rv = mDecoder->Convert(aInput.BeginReading(), &inputLength,
|
|
aOutput.BeginWriting(), &newOutputLength);
|
|
if (NS_FAILED(rv)) {
|
|
aOutput.Truncate();
|
|
return;
|
|
}
|
|
|
|
if (newOutputLength < outputLength) {
|
|
aOutput.Truncate(newOutputLength);
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::Get(const nsAString& aName, nsString& aRetval)
|
|
{
|
|
SetDOMStringToNull(aRetval);
|
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
aRetval.Assign(mSearchParams[i].mValue);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval)
|
|
{
|
|
aRetval.Clear();
|
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
aRetval.AppendElement(mSearchParams[i].mValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::Set(const nsAString& aName, const nsAString& aValue)
|
|
{
|
|
Param* param = nullptr;
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len;) {
|
|
if (!mSearchParams[i].mKey.Equals(aName)) {
|
|
++i;
|
|
continue;
|
|
}
|
|
if (!param) {
|
|
param = &mSearchParams[i];
|
|
++i;
|
|
continue;
|
|
}
|
|
// Remove duplicates.
|
|
mSearchParams.RemoveElementAt(i);
|
|
--len;
|
|
}
|
|
|
|
if (!param) {
|
|
param = mSearchParams.AppendElement();
|
|
param->mKey = aName;
|
|
}
|
|
|
|
param->mValue = aValue;
|
|
|
|
NotifyObserver();
|
|
}
|
|
|
|
void
|
|
URLSearchParams::Append(const nsAString& aName, const nsAString& aValue)
|
|
{
|
|
AppendInternal(aName, aValue);
|
|
NotifyObserver();
|
|
}
|
|
|
|
void
|
|
URLSearchParams::AppendInternal(const nsAString& aName, const nsAString& aValue)
|
|
{
|
|
Param* param = mSearchParams.AppendElement();
|
|
param->mKey = aName;
|
|
param->mValue = aValue;
|
|
}
|
|
|
|
bool
|
|
URLSearchParams::Has(const nsAString& aName)
|
|
{
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void
|
|
URLSearchParams::Delete(const nsAString& aName)
|
|
{
|
|
bool found = false;
|
|
for (uint32_t i = 0; i < mSearchParams.Length();) {
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
mSearchParams.RemoveElementAt(i);
|
|
found = true;
|
|
} else {
|
|
++i;
|
|
}
|
|
}
|
|
|
|
if (found) {
|
|
NotifyObserver();
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::DeleteAll()
|
|
{
|
|
mSearchParams.Clear();
|
|
}
|
|
|
|
namespace {
|
|
|
|
void SerializeString(const nsCString& aInput, nsAString& aValue)
|
|
{
|
|
const unsigned char* p = (const unsigned char*) aInput.get();
|
|
|
|
while (p && *p) {
|
|
// ' ' to '+'
|
|
if (*p == 0x20) {
|
|
aValue.Append(0x2B);
|
|
// Percent Encode algorithm
|
|
} else if (*p == 0x2A || *p == 0x2D || *p == 0x2E ||
|
|
(*p >= 0x30 && *p <= 0x39) ||
|
|
(*p >= 0x41 && *p <= 0x5A) || *p == 0x5F ||
|
|
(*p >= 0x61 && *p <= 0x7A)) {
|
|
aValue.Append(*p);
|
|
} else {
|
|
aValue.AppendPrintf("%%%.2X", *p);
|
|
}
|
|
|
|
++p;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void
|
|
URLSearchParams::Serialize(nsAString& aValue) const
|
|
{
|
|
aValue.Truncate();
|
|
bool first = true;
|
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
aValue.Append('&');
|
|
}
|
|
|
|
SerializeString(NS_ConvertUTF16toUTF8(mSearchParams[i].mKey), aValue);
|
|
aValue.Append('=');
|
|
SerializeString(NS_ConvertUTF16toUTF8(mSearchParams[i].mValue), aValue);
|
|
}
|
|
}
|
|
|
|
void
|
|
URLSearchParams::NotifyObserver()
|
|
{
|
|
if (mObserver) {
|
|
mObserver->URLSearchParamsUpdated(this);
|
|
}
|
|
}
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|