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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/* 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 "DefaultURI.h"
#include "nsIClassInfoImpl.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsURLHelper.h"
#include "mozilla/ipc/URIParams.h"
namespace mozilla {
namespace net {
#define NS_DEFAULTURI_CID \
{ /* 04445aa0-fd27-4c99-bd41-6be6318ae92c */ \
0x04445aa0, 0xfd27, 0x4c99, { \
0xbd, 0x41, 0x6b, 0xe6, 0x31, 0x8a, 0xe9, 0x2c \
} \
}
#define ASSIGN_AND_ADDREF_THIS(ptrToMutator) \
do { \
if (ptrToMutator) { \
*(ptrToMutator) = do_AddRef(this).take(); \
} \
} while (0)
static NS_DEFINE_CID(kDefaultURICID, NS_DEFAULTURI_CID);
//----------------------------------------------------------------------------
// nsIClassInfo
//----------------------------------------------------------------------------
NS_IMPL_CLASSINFO(DefaultURI, nullptr, nsIClassInfo::THREADSAFE,
NS_DEFAULTURI_CID)
// Empty CI getter. We only need nsIClassInfo for Serialization
NS_IMPL_CI_INTERFACE_GETTER0(DefaultURI)
//----------------------------------------------------------------------------
// nsISupports
//----------------------------------------------------------------------------
NS_IMPL_ADDREF(DefaultURI)
NS_IMPL_RELEASE(DefaultURI)
NS_INTERFACE_TABLE_HEAD(DefaultURI)
NS_INTERFACE_TABLE(DefaultURI, nsIURI, nsISerializable)
NS_INTERFACE_TABLE_TO_MAP_SEGUE
NS_IMPL_QUERY_CLASSINFO(DefaultURI)
if (aIID.Equals(kDefaultURICID)) {
foundInterface = static_cast<nsIURI*>(this);
} else
NS_INTERFACE_MAP_ENTRY(nsISizeOf)
NS_INTERFACE_MAP_END
//----------------------------------------------------------------------------
// nsISerializable
//----------------------------------------------------------------------------
NS_IMETHODIMP DefaultURI::Read(nsIObjectInputStream* aInputStream) {
MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DefaultURI::Write(nsIObjectOutputStream* aOutputStream) {
nsAutoCString spec(mURL->Spec());
return aOutputStream->WriteStringZ(spec.get());
}
//----------------------------------------------------------------------------
// nsISizeOf
//----------------------------------------------------------------------------
size_t DefaultURI::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
return mURL->SizeOf();
}
size_t DefaultURI::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}
//----------------------------------------------------------------------------
// nsIURI
//----------------------------------------------------------------------------
NS_IMETHODIMP DefaultURI::GetSpec(nsACString& aSpec) {
aSpec = mURL->Spec();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetPrePath(nsACString& aPrePath) {
aPrePath = mURL->PrePath();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetScheme(nsACString& aScheme) {
aScheme = mURL->Scheme();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetUserPass(nsACString& aUserPass) {
aUserPass = mURL->Username();
nsAutoCString pass(mURL->Password());
if (pass.IsEmpty()) {
return NS_OK;
}
aUserPass.Append(':');
aUserPass.Append(pass);
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetUsername(nsACString& aUsername) {
aUsername = mURL->Username();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetPassword(nsACString& aPassword) {
aPassword = mURL->Password();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHostPort(nsACString& aHostPort) {
aHostPort = mURL->HostPort();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHost(nsACString& aHost) {
aHost = mURL->Host();
// Historically nsIURI.host has always returned an IPv6 address that isn't
// enclosed in brackets. Ideally we want to change that, but for the sake of
// consitency we'll leave it like that for the moment.
// Bug 1603199 should fix this.
if (StringBeginsWith(aHost, "["_ns) && StringEndsWith(aHost, "]"_ns) &&
aHost.FindChar(':') != kNotFound) {
aHost = Substring(aHost, 1, aHost.Length() - 2);
}
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetPort(int32_t* aPort) {
*aPort = mURL->Port();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetPathQueryRef(nsACString& aPathQueryRef) {
aPathQueryRef = mURL->Path();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::Equals(nsIURI* other, bool* _retval) {
if (!other) {
*_retval = false;
return NS_OK;
}
RefPtr<DefaultURI> otherUri;
nsresult rv = other->QueryInterface(kDefaultURICID, getter_AddRefs(otherUri));
if (NS_FAILED(rv)) {
*_retval = false;
return NS_OK;
}
*_retval = mURL->Spec() == otherUri->mURL->Spec();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::SchemeIs(const char* scheme, bool* _retval) {
if (!scheme) {
*_retval = false;
return NS_OK;
}
*_retval = mURL->Scheme().Equals(scheme);
return NS_OK;
}
NS_IMETHODIMP DefaultURI::Resolve(const nsACString& aRelativePath,
nsACString& aResult) {
nsAutoCString scheme;
nsresult rv = net_ExtractURLScheme(aRelativePath, scheme);
if (NS_SUCCEEDED(rv)) {
aResult = aRelativePath;
return NS_OK;
}
// We try to create another URL with this one as its base.
RefPtr<MozURL> resolvedURL;
rv = MozURL::Init(getter_AddRefs(resolvedURL), aRelativePath, mURL);
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.
aResult = aRelativePath;
return NS_OK;
}
aResult = resolvedURL->Spec();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetAsciiSpec(nsACString& aAsciiSpec) {
return GetSpec(aAsciiSpec);
}
NS_IMETHODIMP DefaultURI::GetAsciiHostPort(nsACString& aAsciiHostPort) {
return GetHostPort(aAsciiHostPort);
}
NS_IMETHODIMP DefaultURI::GetAsciiHost(nsACString& aAsciiHost) {
return GetHost(aAsciiHost);
}
NS_IMETHODIMP DefaultURI::GetRef(nsACString& aRef) {
aRef = mURL->Ref();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::EqualsExceptRef(nsIURI* other, bool* _retval) {
if (!_retval || !other) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<DefaultURI> otherUri;
nsresult rv = other->QueryInterface(kDefaultURICID, getter_AddRefs(otherUri));
if (NS_FAILED(rv)) {
*_retval = false;
return NS_OK;
}
*_retval = mURL->SpecNoRef().Equals(otherUri->mURL->SpecNoRef());
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetSpecIgnoringRef(nsACString& aSpecIgnoringRef) {
aSpecIgnoringRef = mURL->SpecNoRef();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHasRef(bool* aHasRef) {
*aHasRef = mURL->HasFragment();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHasUserPass(bool* aHasUserPass) {
*aHasUserPass = !mURL->Username().IsEmpty() || !mURL->Password().IsEmpty();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetFilePath(nsACString& aFilePath) {
aFilePath = mURL->FilePath();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetQuery(nsACString& aQuery) {
aQuery = mURL->Query();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetHasQuery(bool* aHasQuery) {
*aHasQuery = mURL->HasQuery();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetDisplayHost(nsACString& aDisplayHost) {
// At the moment it doesn't seem useful to decode the hostname if it happens
// to contain punycode.
return GetHost(aDisplayHost);
}
NS_IMETHODIMP DefaultURI::GetDisplayHostPort(nsACString& aDisplayHostPort) {
// At the moment it doesn't seem useful to decode the hostname if it happens
// to contain punycode.
return GetHostPort(aDisplayHostPort);
}
NS_IMETHODIMP DefaultURI::GetDisplaySpec(nsACString& aDisplaySpec) {
// At the moment it doesn't seem useful to decode the hostname if it happens
// to contain punycode.
return GetSpec(aDisplaySpec);
}
NS_IMETHODIMP DefaultURI::GetDisplayPrePath(nsACString& aDisplayPrePath) {
// At the moment it doesn't seem useful to decode the hostname if it happens
// to contain punycode.
return GetPrePath(aDisplayPrePath);
}
NS_IMETHODIMP DefaultURI::Mutate(nsIURIMutator** _retval) {
RefPtr<DefaultURI::Mutator> mutator = new DefaultURI::Mutator();
mutator->Init(this);
mutator.forget(_retval);
return NS_OK;
}
void DefaultURI::Serialize(ipc::URIParams& aParams) {
ipc::DefaultURIParams params;
params.spec() = mURL->Spec();
aParams = params;
}
//----------------------------------------------------------------------------
// nsIURIMutator
//----------------------------------------------------------------------------
NS_IMPL_ADDREF(DefaultURI::Mutator)
NS_IMPL_RELEASE(DefaultURI::Mutator)
NS_IMETHODIMP DefaultURI::Mutator::QueryInterface(REFNSIID aIID,
void** aInstancePtr) {
NS_ASSERTION(aInstancePtr, "QueryInterface requires a non-NULL destination!");
nsISupports* foundInterface = nullptr;
if (aIID.Equals(NS_GET_IID(nsIURI))) {
RefPtr<DefaultURI> defaultURI = new DefaultURI();
mMutator->Finalize(getter_AddRefs(defaultURI->mURL));
foundInterface =
static_cast<nsISupports*>(static_cast<nsIURI*>((defaultURI.get())));
NS_ADDREF(foundInterface);
*aInstancePtr = foundInterface;
return NS_OK;
}
if (aIID.Equals(NS_GET_IID(nsIURIMutator)) ||
aIID.Equals(NS_GET_IID(nsISupports))) {
foundInterface =
static_cast<nsISupports*>(static_cast<nsIURIMutator*>(this));
} else if (aIID.Equals(NS_GET_IID(nsIURISetters))) {
foundInterface =
static_cast<nsISupports*>(static_cast<nsIURISetters*>(this));
} else if (aIID.Equals(NS_GET_IID(nsIURISetSpec))) {
foundInterface =
static_cast<nsISupports*>(static_cast<nsIURISetSpec*>(this));
} else if (aIID.Equals(NS_GET_IID(nsISerializable))) {
foundInterface =
static_cast<nsISupports*>(static_cast<nsISerializable*>(this));
}
if (foundInterface) {
NS_ADDREF(foundInterface);
*aInstancePtr = foundInterface;
return NS_OK;
}
return NS_NOINTERFACE;
}
NS_IMETHODIMP DefaultURI::Mutator::Read(nsIObjectInputStream* aStream) {
nsAutoCString spec;
nsresult rv = aStream->ReadCString(spec);
if (NS_FAILED(rv)) {
return rv;
}
return SetSpec(spec, nullptr);
}
NS_IMETHODIMP DefaultURI::Mutator::Deserialize(
const mozilla::ipc::URIParams& aParams) {
if (aParams.type() != ipc::URIParams::TDefaultURIParams) {
NS_ERROR("Received unknown parameters from the other process!");
return NS_ERROR_FAILURE;
}
const ipc::DefaultURIParams& params = aParams.get_DefaultURIParams();
auto result = MozURL::Mutator::FromSpec(params.spec());
if (result.isErr()) {
return result.unwrapErr();
}
mMutator = Some(result.unwrap());
return NS_OK;
}
NS_IMETHODIMP DefaultURI::Mutator::Finalize(nsIURI** aURI) {
if (!mMutator.isSome()) {
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<DefaultURI> uri = new DefaultURI();
mMutator->Finalize(getter_AddRefs(uri->mURL));
mMutator = Nothing();
uri.forget(aURI);
return NS_OK;
}
NS_IMETHODIMP DefaultURI::Mutator::SetSpec(const nsACString& aSpec,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
auto result = MozURL::Mutator::FromSpec(aSpec);
if (result.isErr()) {
return result.unwrapErr();
}
mMutator = Some(result.unwrap());
return NS_OK;
}
NS_IMETHODIMP
DefaultURI::Mutator::SetScheme(const nsACString& aScheme,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetScheme(aScheme);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetUserPass(const nsACString& aUserPass,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
int32_t index = aUserPass.FindChar(':');
if (index == kNotFound) {
mMutator->SetUsername(aUserPass);
mMutator->SetPassword(""_ns);
return mMutator->GetStatus();
}
mMutator->SetUsername(Substring(aUserPass, 0, index));
nsresult rv = mMutator->GetStatus();
if (NS_FAILED(rv)) {
return rv;
}
mMutator->SetPassword(Substring(aUserPass, index + 1));
rv = mMutator->GetStatus();
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP
DefaultURI::Mutator::SetUsername(const nsACString& aUsername,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetUsername(aUsername);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetPassword(const nsACString& aPassword,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetPassword(aPassword);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetHostPort(const nsACString& aHostPort,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetHostPort(aHostPort);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetHost(const nsACString& aHost,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetHostname(aHost);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetPort(int32_t aPort, nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetPort(aPort);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetPathQueryRef(const nsACString& aPathQueryRef,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
if (aPathQueryRef.IsEmpty()) {
mMutator->SetFilePath(""_ns);
mMutator->SetQuery(""_ns);
mMutator->SetRef(""_ns);
return mMutator->GetStatus();
}
RefPtr<MozURL> url;
mMutator->Finalize(getter_AddRefs(url));
mMutator = Nothing();
if (!url) {
return NS_ERROR_FAILURE;
}
nsAutoCString pathQueryRef(aPathQueryRef);
if (url->CannotBeABase()) {
// If the base URL cannot be a base, then setting the pathQueryRef
// needs to change everything after the scheme.
pathQueryRef.Insert(":", 0);
pathQueryRef.Insert(url->Scheme(), 0);
// Clear the URL to make sure FromSpec creates an absolute URL
url = nullptr;
} else if (!StringBeginsWith(pathQueryRef, "/"_ns)) {
// If the base URL can be a base, make sure the path
// begins with a /
pathQueryRef.Insert('/', 0);
}
auto result = MozURL::Mutator::FromSpec(pathQueryRef, url);
if (result.isErr()) {
return result.unwrapErr();
}
mMutator = Some(result.unwrap());
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetRef(const nsACString& aRef, nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetRef(aRef);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetFilePath(const nsACString& aFilePath,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetFilePath(aFilePath);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetQuery(const nsACString& aQuery,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
mMutator->SetQuery(aQuery);
return mMutator->GetStatus();
}
NS_IMETHODIMP
DefaultURI::Mutator::SetQueryWithEncoding(const nsACString& aQuery,
const mozilla::Encoding* aEncoding,
nsIURIMutator** aMutator) {
ASSIGN_AND_ADDREF_THIS(aMutator);
if (!mMutator.isSome()) {
return NS_ERROR_NULL_POINTER;
}
// we only support UTF-8 for DefaultURI
mMutator->SetQuery(aQuery);
return mMutator->GetStatus();
}
} // namespace net
} // namespace mozilla
|