2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-12-12 19:30:10 +00: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/. */
|
|
|
|
|
2020-11-23 16:06:52 +00:00
|
|
|
#include "mozilla/dom/URLSearchParams.h"
|
|
|
|
|
|
|
|
// XXX encoding_rs.h is not self-contained, this order is required
|
|
|
|
#include "mozilla/Encoding.h"
|
|
|
|
#include "encoding_rs.h"
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include "js/StructuredClone.h"
|
|
|
|
#include "mozilla/ArrayIterator.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/MacroForEach.h"
|
|
|
|
#include "mozilla/NotNull.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/Record.h"
|
2019-06-25 06:48:33 +00:00
|
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
2013-12-12 19:30:10 +00:00
|
|
|
#include "mozilla/dom/URLSearchParamsBinding.h"
|
2020-11-23 16:06:52 +00:00
|
|
|
#include "mozilla/fallible.h"
|
2014-10-07 08:06:00 +00:00
|
|
|
#include "nsDOMString.h"
|
2020-11-23 16:06:52 +00:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFlags.h"
|
|
|
|
#include "nsStringIterator.h"
|
2016-07-11 19:03:44 +00:00
|
|
|
#include "nsStringStream.h"
|
2020-11-13 08:11:32 +00:00
|
|
|
#include "nsURLHelper.h"
|
2013-12-12 19:30:10 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-06-29 16:02:53 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mParent, mObserver)
|
2015-06-04 20:45:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearchParams)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams)
|
2014-10-01 13:55:33 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
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:27 +00:00
|
|
|
|
2015-06-29 16:02:53 +00:00
|
|
|
URLSearchParams::URLSearchParams(nsISupports* aParent,
|
|
|
|
URLSearchParamsObserver* aObserver)
|
|
|
|
: mParams(new URLParams()), mParent(aParent), mObserver(aObserver) {}
|
2014-10-01 13:55:33 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
URLSearchParams::~URLSearchParams() { DeleteAll(); }
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
JSObject* URLSearchParams::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-25 21:20:54 +00:00
|
|
|
return URLSearchParams_Binding::Wrap(aCx, this, aGivenProto);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<URLSearchParams> URLSearchParams::Constructor(
|
2015-06-04 20:45:24 +00:00
|
|
|
const GlobalObject& aGlobal,
|
2017-03-02 20:48:51 +00:00
|
|
|
const USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString& aInit,
|
2015-06-04 20:45:24 +00:00
|
|
|
ErrorResult& aRv) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<URLSearchParams> sp =
|
2015-06-29 16:02:53 +00:00
|
|
|
new URLSearchParams(aGlobal.GetAsSupports(), nullptr);
|
2016-04-28 11:53:16 +00:00
|
|
|
|
2017-01-17 10:13:06 +00:00
|
|
|
if (aInit.IsUSVString()) {
|
|
|
|
NS_ConvertUTF16toUTF8 input(aInit.GetAsUSVString());
|
|
|
|
if (StringBeginsWith(input, "?"_ns)) {
|
|
|
|
sp->ParseInput(Substring(input, 1, input.Length() - 1));
|
|
|
|
} else {
|
|
|
|
sp->ParseInput(input);
|
|
|
|
}
|
|
|
|
} else if (aInit.IsUSVStringSequenceSequence()) {
|
|
|
|
const Sequence<Sequence<nsString>>& list =
|
|
|
|
aInit.GetAsUSVStringSequenceSequence();
|
|
|
|
for (uint32_t i = 0; i < list.Length(); ++i) {
|
|
|
|
const Sequence<nsString>& item = list[i];
|
|
|
|
if (item.Length() != 2) {
|
2020-01-20 02:03:36 +00:00
|
|
|
nsPrintfCString err("Expected 2 items in pair but got %zu",
|
|
|
|
item.Length());
|
2020-03-06 21:08:02 +00:00
|
|
|
aRv.ThrowTypeError(err);
|
2017-01-17 10:13:06 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
sp->Append(item[0], item[1]);
|
|
|
|
}
|
2017-03-02 20:48:51 +00:00
|
|
|
} else if (aInit.IsUSVStringUSVStringRecord()) {
|
|
|
|
const Record<nsString, nsString>& record =
|
|
|
|
aInit.GetAsUSVStringUSVStringRecord();
|
|
|
|
for (auto& entry : record.Entries()) {
|
|
|
|
sp->Append(entry.mKey, entry.mValue);
|
|
|
|
}
|
2016-04-28 11:53:16 +00:00
|
|
|
} else {
|
2017-01-17 10:13:06 +00:00
|
|
|
MOZ_CRASH("This should not happen.");
|
2016-04-28 11:53:16 +00:00
|
|
|
}
|
2015-06-29 16:02:53 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
return sp.forget();
|
|
|
|
}
|
2014-10-01 13:55:33 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::ParseInput(const nsACString& aInput) {
|
|
|
|
mParams->ParseInput(aInput);
|
|
|
|
}
|
2013-12-12 19:30:10 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::Get(const nsAString& aName, nsString& aRetval) {
|
|
|
|
return mParams->Get(aName, aRetval);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::GetAll(const nsAString& aName,
|
|
|
|
nsTArray<nsString>& aRetval) {
|
|
|
|
return mParams->GetAll(aName, aRetval);
|
2013-12-12 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::Set(const nsAString& aName, const nsAString& aValue) {
|
|
|
|
mParams->Set(aName, aValue);
|
|
|
|
NotifyObserver();
|
|
|
|
}
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::Append(const nsAString& aName, const nsAString& aValue) {
|
|
|
|
mParams->Append(aName, aValue);
|
|
|
|
NotifyObserver();
|
|
|
|
}
|
2014-10-01 13:55:33 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
bool URLSearchParams::Has(const nsAString& aName) {
|
|
|
|
return mParams->Has(aName);
|
|
|
|
}
|
2014-10-01 13:55:33 +00:00
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::Delete(const nsAString& aName) {
|
2017-07-19 07:12:41 +00:00
|
|
|
mParams->Delete(aName);
|
|
|
|
NotifyObserver();
|
2014-10-01 13:55:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 20:45:24 +00:00
|
|
|
void URLSearchParams::DeleteAll() { mParams->DeleteAll(); }
|
2013-12-12 19:30:27 +00:00
|
|
|
|
2014-01-15 14:50:18 +00:00
|
|
|
void URLSearchParams::Serialize(nsAString& aValue) const {
|
2015-06-04 20:45:24 +00:00
|
|
|
mParams->Serialize(aValue);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:15:59 +00:00
|
|
|
void URLSearchParams::NotifyObserver() {
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->URLSearchParamsUpdated(this);
|
2013-12-12 19:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 15:07:40 +00:00
|
|
|
uint32_t URLSearchParams::GetIterableLength() const {
|
|
|
|
return mParams->Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString& URLSearchParams::GetKeyAtIndex(uint32_t aIndex) const {
|
|
|
|
return mParams->GetKeyAtIndex(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString& URLSearchParams::GetValueAtIndex(uint32_t aIndex) const {
|
|
|
|
return mParams->GetValueAtIndex(aIndex);
|
|
|
|
}
|
|
|
|
|
2017-01-27 19:35:47 +00:00
|
|
|
void URLSearchParams::Sort(ErrorResult& aRv) {
|
2020-11-09 18:06:10 +00:00
|
|
|
mParams->Sort();
|
|
|
|
NotifyObserver();
|
2017-01-27 19:35:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 08:11:32 +00:00
|
|
|
bool URLSearchParams::WriteStructuredClone(
|
|
|
|
JSStructuredCloneWriter* aWriter) const {
|
|
|
|
const uint32_t& nParams = mParams->Length();
|
2016-07-11 19:03:44 +00:00
|
|
|
if (!JS_WriteUint32Pair(aWriter, nParams, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < nParams; ++i) {
|
2020-11-13 08:11:32 +00:00
|
|
|
if (!StructuredCloneHolder::WriteString(aWriter,
|
|
|
|
mParams->GetKeyAtIndex(i)) ||
|
|
|
|
!StructuredCloneHolder::WriteString(aWriter,
|
|
|
|
mParams->GetValueAtIndex(i))) {
|
2016-07-11 19:03:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-13 08:11:32 +00:00
|
|
|
bool URLSearchParams::ReadStructuredClone(JSStructuredCloneReader* aReader) {
|
2016-07-11 19:03:44 +00:00
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
|
|
|
DeleteAll();
|
|
|
|
|
|
|
|
uint32_t nParams, zero;
|
|
|
|
nsAutoString key, value;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &nParams, &zero)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(zero == 0);
|
|
|
|
for (uint32_t i = 0; i < nParams; ++i) {
|
2019-06-25 06:48:33 +00:00
|
|
|
if (!StructuredCloneHolder::ReadString(aReader, key) ||
|
|
|
|
!StructuredCloneHolder::ReadString(aReader, value)) {
|
2016-07-11 19:03:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Append(key, value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool URLSearchParams::WriteStructuredClone(
|
2019-06-25 06:48:53 +00:00
|
|
|
JSContext* aCx, JSStructuredCloneWriter* aWriter) const {
|
2020-11-13 08:11:32 +00:00
|
|
|
return WriteStructuredClone(aWriter);
|
2016-07-11 19:03:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 06:48:53 +00:00
|
|
|
// static
|
|
|
|
already_AddRefed<URLSearchParams> URLSearchParams::ReadStructuredClone(
|
|
|
|
JSContext* aCx, nsIGlobalObject* aGlobal,
|
|
|
|
JSStructuredCloneReader* aReader) {
|
|
|
|
RefPtr<URLSearchParams> params = new URLSearchParams(aGlobal);
|
2020-11-13 08:11:32 +00:00
|
|
|
if (!params->ReadStructuredClone(aReader)) {
|
2019-06-25 06:48:53 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return params.forget();
|
2016-07-11 19:03:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 01:32:12 +00:00
|
|
|
// contentTypeWithCharset can be set to the contentType or
|
|
|
|
// contentType+charset based on what the spec says.
|
|
|
|
// See: https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
2016-07-11 19:03:44 +00:00
|
|
|
nsresult URLSearchParams::GetSendInfo(nsIInputStream** aBody,
|
|
|
|
uint64_t* aContentLength,
|
2017-01-24 11:13:31 +00:00
|
|
|
nsACString& aContentTypeWithCharset,
|
2017-09-20 01:32:12 +00:00
|
|
|
nsACString& aCharset) const {
|
2017-01-24 11:13:31 +00:00
|
|
|
aContentTypeWithCharset.AssignLiteral(
|
|
|
|
"application/x-www-form-urlencoded;charset=UTF-8");
|
2016-07-11 19:03:44 +00:00
|
|
|
aCharset.AssignLiteral("UTF-8");
|
|
|
|
|
|
|
|
nsAutoString serialized;
|
|
|
|
Serialize(serialized);
|
|
|
|
NS_ConvertUTF16toUTF8 converted(serialized);
|
|
|
|
*aContentLength = converted.Length();
|
2018-05-30 19:15:35 +00:00
|
|
|
return NS_NewCStringInputStream(aBody, std::move(converted));
|
2016-07-11 19:03:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 19:30:10 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|