2013-12-12 19:30:10 +00:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
#include "URLSearchParams.h"
|
|
|
|
#include "mozilla/dom/URLSearchParamsBinding.h"
|
2014-08-08 00:45:14 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-10-07 08:06:00 +00:00
|
|
|
#include "nsDOMString.h"
|
2013-12-12 19:30:10 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-29 08:57:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mObservers)
|
2013-12-12 19:30:27 +00:00
|
|
|
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
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
URLSearchParams::URLSearchParams()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
URLSearchParams::~URLSearchParams()
|
|
|
|
{
|
|
|
|
DeleteAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
URLSearchParams::WrapObject(JSContext* aCx)
|
2013-12-12 19:30:10 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return URLSearchParamsBinding::Wrap(aCx, this);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URLSearchParams>
|
|
|
|
URLSearchParams::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aInit,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<URLSearchParams> sp = new URLSearchParams();
|
2014-02-03 16:48:38 +00:00
|
|
|
sp->ParseInput(NS_ConvertUTF16toUTF8(aInit), nullptr);
|
2013-12-12 19:30:10 +00:00
|
|
|
return sp.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<URLSearchParams>
|
|
|
|
URLSearchParams::Constructor(const GlobalObject& aGlobal,
|
|
|
|
URLSearchParams& aInit,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<URLSearchParams> sp = new URLSearchParams();
|
2014-10-01 13:55:33 +00:00
|
|
|
sp->mSearchParams = aInit.mSearchParams;
|
2013-12-12 19:30:10 +00:00
|
|
|
return sp.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 16:48:38 +00:00
|
|
|
URLSearchParams::ParseInput(const nsACString& aInput,
|
|
|
|
URLSearchParamsObserver* aObserver)
|
2013-12-12 19:30:10 +00:00
|
|
|
{
|
2013-12-12 19:30:27 +00:00
|
|
|
// Remove all the existing data before parsing a new input.
|
|
|
|
DeleteAll();
|
|
|
|
|
|
|
|
nsACString::const_iterator start, end;
|
2013-12-12 19:30:10 +00:00
|
|
|
aInput.BeginReading(start);
|
|
|
|
aInput.EndReading(end);
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator iter(start);
|
2013-12-12 19:30:10 +00:00
|
|
|
|
|
|
|
while (start != end) {
|
2013-12-12 19:30:27 +00:00
|
|
|
nsAutoCString string;
|
2013-12-12 19:30:10 +00:00
|
|
|
|
|
|
|
if (FindCharInReadable('&', iter, end)) {
|
|
|
|
string.Assign(Substring(start, iter));
|
|
|
|
start = ++iter;
|
|
|
|
} else {
|
|
|
|
string.Assign(Substring(start, end));
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator eqStart, eqEnd;
|
2013-12-12 19:30:10 +00:00
|
|
|
string.BeginReading(eqStart);
|
|
|
|
string.EndReading(eqEnd);
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator eqIter(eqStart);
|
2013-12-12 19:30:10 +00:00
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
nsAutoCString name;
|
|
|
|
nsAutoCString value;
|
2013-12-12 19:30:10 +00:00
|
|
|
|
|
|
|
if (FindCharInReadable('=', eqIter, eqEnd)) {
|
|
|
|
name.Assign(Substring(eqStart, eqIter));
|
|
|
|
|
|
|
|
++eqIter;
|
|
|
|
value.Assign(Substring(eqIter, eqEnd));
|
|
|
|
} else {
|
|
|
|
name.Assign(string);
|
|
|
|
}
|
|
|
|
|
2014-08-08 00:45:14 +00:00
|
|
|
nsAutoString decodedName;
|
2013-12-12 19:30:10 +00:00
|
|
|
DecodeString(name, decodedName);
|
|
|
|
|
2014-08-08 00:45:14 +00:00
|
|
|
nsAutoString decodedValue;
|
2013-12-12 19:30:10 +00:00
|
|
|
DecodeString(value, decodedValue);
|
|
|
|
|
2014-08-08 00:45:14 +00:00
|
|
|
AppendInternal(decodedName, decodedValue);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2014-02-03 16:48:38 +00:00
|
|
|
NotifyObservers(aObserver);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-08 00:45:14 +00:00
|
|
|
URLSearchParams::DecodeString(const nsACString& aInput, nsAString& aOutput)
|
2013-12-12 19:30:10 +00:00
|
|
|
{
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator start, end;
|
2013-12-12 19:30:10 +00:00
|
|
|
aInput.BeginReading(start);
|
|
|
|
aInput.EndReading(end);
|
|
|
|
|
2014-08-08 00:45:14 +00:00
|
|
|
nsCString unescaped;
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
while (start != end) {
|
|
|
|
// replace '+' with U+0020
|
|
|
|
if (*start == '+') {
|
2014-08-08 00:45:14 +00:00
|
|
|
unescaped.Append(' ');
|
2013-12-12 19:30:10 +00:00
|
|
|
++start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Percent decode algorithm
|
|
|
|
if (*start == '%') {
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator first(start);
|
2013-12-12 19:30:10 +00:00
|
|
|
++first;
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
nsACString::const_iterator second(first);
|
2013-12-12 19:30:10 +00:00
|
|
|
++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)) {
|
2014-08-08 00:45:14 +00:00
|
|
|
unescaped.Append(HEX_DIGIT(first) * 16 + HEX_DIGIT(second));
|
2013-12-12 19:30:10 +00:00
|
|
|
start = ++second;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
2014-08-08 00:45:14 +00:00
|
|
|
unescaped.Append('%');
|
2013-12-12 19:30:10 +00:00
|
|
|
++start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 00:45:14 +00:00
|
|
|
unescaped.Append(*start);
|
2013-12-12 19:30:10 +00:00
|
|
|
++start;
|
|
|
|
}
|
2014-08-08 00:45:14 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-08-08 21:41:00 +00:00
|
|
|
nsresult rv = mDecoder->GetMaxLength(aInput.BeginReading(), inputLength,
|
2014-08-08 00:45:14 +00:00
|
|
|
&outputLength);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:41:00 +00:00
|
|
|
if (!aOutput.SetLength(outputLength, fallible_t())) {
|
2014-08-08 00:45:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:41:00 +00:00
|
|
|
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);
|
2014-08-08 00:45:14 +00:00
|
|
|
}
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:27 +00:00
|
|
|
void
|
2014-02-03 16:48:38 +00:00
|
|
|
URLSearchParams::AddObserver(URLSearchParamsObserver* aObserver)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
MOZ_ASSERT(!mObservers.Contains(aObserver));
|
|
|
|
mObservers.AppendElement(aObserver);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 16:48:38 +00:00
|
|
|
URLSearchParams::RemoveObserver(URLSearchParamsObserver* aObserver)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
MOZ_ASSERT(aObserver);
|
|
|
|
mObservers.RemoveElement(aObserver);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 00:45:21 +00:00
|
|
|
void
|
|
|
|
URLSearchParams::RemoveObservers()
|
|
|
|
{
|
|
|
|
mObservers.Clear();
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
void
|
|
|
|
URLSearchParams::Get(const nsAString& aName, nsString& aRetval)
|
|
|
|
{
|
2014-10-07 08:06:00 +00:00
|
|
|
SetDOMStringToNull(aRetval);
|
2013-12-12 19:30:10 +00:00
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
|
|
aRetval.Assign(mSearchParams[i].mValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLSearchParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval)
|
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
aRetval.Clear();
|
2013-12-12 19:30:10 +00:00
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
|
|
aRetval.AppendElement(mSearchParams[i].mValue);
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLSearchParams::Set(const nsAString& aName, const nsAString& aValue)
|
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
Param* param = nullptr;
|
2014-10-09 13:53:00 +00:00
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len;) {
|
|
|
|
if (!mSearchParams[i].mKey.Equals(aName)) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!param) {
|
2014-10-01 13:55:33 +00:00
|
|
|
param = &mSearchParams[i];
|
2014-10-09 13:53:00 +00:00
|
|
|
++i;
|
|
|
|
continue;
|
2014-10-01 13:55:33 +00:00
|
|
|
}
|
2014-10-09 13:53:00 +00:00
|
|
|
// Remove duplicates.
|
|
|
|
mSearchParams.RemoveElementAt(i);
|
|
|
|
--len;
|
2014-10-01 13:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!param) {
|
|
|
|
param = mSearchParams.AppendElement();
|
|
|
|
param->mKey = aName;
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
param->mValue = aValue;
|
|
|
|
|
2014-02-03 16:48:38 +00:00
|
|
|
NotifyObservers(nullptr);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLSearchParams::Append(const nsAString& aName, const nsAString& aValue)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
|
|
|
AppendInternal(aName, aValue);
|
2014-02-03 16:48:38 +00:00
|
|
|
NotifyObservers(nullptr);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLSearchParams::AppendInternal(const nsAString& aName, const nsAString& aValue)
|
2013-12-12 19:30:10 +00:00
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
Param* param = mSearchParams.AppendElement();
|
|
|
|
param->mKey = aName;
|
|
|
|
param->mValue = aValue;
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
URLSearchParams::Has(const nsAString& aName)
|
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
URLSearchParams::Delete(const nsAString& aName)
|
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
bool found = false;
|
|
|
|
for (uint32_t i = 0; i < mSearchParams.Length();) {
|
|
|
|
if (mSearchParams[i].mKey.Equals(aName)) {
|
|
|
|
mSearchParams.RemoveElementAt(i);
|
|
|
|
found = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
if (found) {
|
|
|
|
NotifyObservers(nullptr);
|
|
|
|
}
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
void
|
|
|
|
URLSearchParams::DeleteAll()
|
|
|
|
{
|
|
|
|
mSearchParams.Clear();
|
|
|
|
}
|
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
namespace {
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
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);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
2014-10-01 13:55:33 +00:00
|
|
|
|
|
|
|
++p;
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
2014-10-01 13:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2013-12-12 19:30:27 +00:00
|
|
|
|
|
|
|
void
|
2014-01-15 14:50:18 +00:00
|
|
|
URLSearchParams::Serialize(nsAString& aValue) const
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-10-01 13:55:33 +00:00
|
|
|
aValue.Truncate();
|
|
|
|
bool first = true;
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
2013-12-12 19:30:27 +00:00
|
|
|
} else {
|
2014-10-01 13:55:33 +00:00
|
|
|
aValue.Append('&');
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 13:55:33 +00:00
|
|
|
SerializeString(NS_ConvertUTF16toUTF8(mSearchParams[i].mKey), aValue);
|
|
|
|
aValue.Append('=');
|
|
|
|
SerializeString(NS_ConvertUTF16toUTF8(mSearchParams[i].mValue), aValue);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-03 16:48:38 +00:00
|
|
|
URLSearchParams::NotifyObservers(URLSearchParamsObserver* aExceptObserver)
|
2013-12-12 19:30:27 +00:00
|
|
|
{
|
2014-02-03 16:48:38 +00:00
|
|
|
for (uint32_t i = 0; i < mObservers.Length(); ++i) {
|
|
|
|
if (mObservers[i] != aExceptObserver) {
|
2014-08-08 00:45:21 +00:00
|
|
|
mObservers[i]->URLSearchParamsUpdated(this);
|
2014-02-03 16:48:38 +00:00
|
|
|
}
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|