1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
/* -*- 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"
#include "nsIInputStream.h"
#include "nsStringStream.h"
namespace mozilla {
namespace dom {
bool
URLParams::Has(const nsAString& aName)
{
for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) {
if (mParams[i].mKey.Equals(aName)) {
return true;
}
}
return false;
}
void
URLParams::Get(const nsAString& aName, nsString& aRetval)
{
SetDOMStringToNull(aRetval);
for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) {
if (mParams[i].mKey.Equals(aName)) {
aRetval.Assign(mParams[i].mValue);
break;
}
}
}
void
URLParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval)
{
aRetval.Clear();
for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) {
if (mParams[i].mKey.Equals(aName)) {
aRetval.AppendElement(mParams[i].mValue);
}
}
}
void
URLParams::Append(const nsAString& aName, const nsAString& aValue)
{
Param* param = mParams.AppendElement();
param->mKey = aName;
param->mValue = aValue;
}
void
URLParams::Set(const nsAString& aName, const nsAString& aValue)
{
Param* param = nullptr;
for (uint32_t i = 0, len = mParams.Length(); i < len;) {
if (!mParams[i].mKey.Equals(aName)) {
++i;
continue;
}
if (!param) {
param = &mParams[i];
++i;
continue;
}
// Remove duplicates.
mParams.RemoveElementAt(i);
--len;
}
if (!param) {
param = mParams.AppendElement();
param->mKey = aName;
}
param->mValue = aValue;
}
bool
URLParams::Delete(const nsAString& aName)
{
bool found = false;
for (uint32_t i = 0; i < mParams.Length();) {
if (mParams[i].mKey.Equals(aName)) {
mParams.RemoveElementAt(i);
found = true;
} else {
++i;
}
}
return found;
}
void
URLParams::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
URLParams::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
URLParams::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);
Append(decodedName, decodedValue);
}
}
namespace {
void SerializeString(const nsCString& aInput, nsAString& aValue)
{
const unsigned char* p = (const unsigned char*) aInput.get();
const unsigned char* end = p + aInput.Length();
while (p != end) {
// ' ' 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
URLParams::Serialize(nsAString& aValue) const
{
aValue.Truncate();
bool first = true;
for (uint32_t i = 0, len = mParams.Length(); i < len; ++i) {
if (first) {
first = false;
} else {
aValue.Append('&');
}
SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mKey), aValue);
aValue.Append('=');
SerializeString(NS_ConvertUTF16toUTF8(mParams[i].mValue), aValue);
}
}
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mParent, 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(nsIXHRSendable)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
URLSearchParams::URLSearchParams(nsISupports* aParent,
URLSearchParamsObserver* aObserver)
: mParams(new URLParams())
, mParent(aParent)
, 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 USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString& aInit,
ErrorResult& aRv)
{
RefPtr<URLSearchParams> sp =
new URLSearchParams(aGlobal.GetAsSupports(), nullptr);
if (aInit.IsUSVString()) {
NS_ConvertUTF16toUTF8 input(aInit.GetAsUSVString());
if (StringBeginsWith(input, NS_LITERAL_CSTRING("?"))) {
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) {
aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
return nullptr;
}
sp->Append(item[0], item[1]);
}
} else if (aInit.IsUSVStringUSVStringRecord()) {
const Record<nsString, nsString>& record =
aInit.GetAsUSVStringUSVStringRecord();
for (auto& entry : record.Entries()) {
sp->Append(entry.mKey, entry.mValue);
}
} else {
MOZ_CRASH("URLSearchParams: Invalid string");
}
return sp.forget();
}
void
URLSearchParams::ParseInput(const nsACString& aInput)
{
mParams->ParseInput(aInput);
}
void
URLSearchParams::Get(const nsAString& aName, nsString& aRetval)
{
return mParams->Get(aName, aRetval);
}
void
URLSearchParams::GetAll(const nsAString& aName, nsTArray<nsString>& aRetval)
{
return mParams->GetAll(aName, aRetval);
}
void
URLSearchParams::Set(const nsAString& aName, const nsAString& aValue)
{
mParams->Set(aName, aValue);
NotifyObserver();
}
void
URLSearchParams::Append(const nsAString& aName, const nsAString& aValue)
{
mParams->Append(aName, aValue);
NotifyObserver();
}
bool
URLSearchParams::Has(const nsAString& aName)
{
return mParams->Has(aName);
}
void
URLSearchParams::Delete(const nsAString& aName)
{
if (mParams->Delete(aName)) {
NotifyObserver();
}
}
void
URLSearchParams::DeleteAll()
{
mParams->DeleteAll();
}
void
URLSearchParams::Serialize(nsAString& aValue) const
{
mParams->Serialize(aValue);
}
void
URLSearchParams::NotifyObserver()
{
if (mObserver) {
mObserver->URLSearchParamsUpdated(this);
}
}
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);
}
// Helper functions for structured cloning
inline bool
ReadString(JSStructuredCloneReader* aReader, nsString& aString)
{
MOZ_ASSERT(aReader);
bool read;
uint32_t nameLength, zero;
read = JS_ReadUint32Pair(aReader, &nameLength, &zero);
if (!read) {
return false;
}
MOZ_ASSERT(zero == 0);
aString.SetLength(nameLength);
size_t charSize = sizeof(nsString::char_type);
read = JS_ReadBytes(aReader, (void*) aString.BeginWriting(),
nameLength * charSize);
if (!read) {
return false;
}
return true;
}
inline bool
WriteString(JSStructuredCloneWriter* aWriter, const nsString& aString)
{
MOZ_ASSERT(aWriter);
size_t charSize = sizeof(nsString::char_type);
return JS_WriteUint32Pair(aWriter, aString.Length(), 0) &&
JS_WriteBytes(aWriter, aString.get(), aString.Length() * charSize);
}
bool
URLParams::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
{
const uint32_t& nParams = mParams.Length();
if (!JS_WriteUint32Pair(aWriter, nParams, 0)) {
return false;
}
for (uint32_t i = 0; i < nParams; ++i) {
if (!WriteString(aWriter, mParams[i].mKey) ||
!WriteString(aWriter, mParams[i].mValue)) {
return false;
}
}
return true;
}
bool
URLParams::ReadStructuredClone(JSStructuredCloneReader* aReader)
{
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) {
if (!ReadString(aReader, key) || !ReadString(aReader, value)) {
return false;
}
Append(key, value);
}
return true;
}
bool
URLSearchParams::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
{
return mParams->WriteStructuredClone(aWriter);
}
bool
URLSearchParams::ReadStructuredClone(JSStructuredCloneReader* aReader)
{
return mParams->ReadStructuredClone(aReader);
}
NS_IMETHODIMP
URLSearchParams::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType, nsACString& aCharset)
{
aContentType.AssignLiteral("application/x-www-form-urlencoded");
aCharset.AssignLiteral("UTF-8");
nsAutoString serialized;
Serialize(serialized);
NS_ConvertUTF16toUTF8 converted(serialized);
*aContentLength = converted.Length();
return NS_NewCStringInputStream(aBody, converted);
}
} // namespace dom
} // namespace mozilla
|