Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=4 sw=2 sts=2 et cin: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsNetUtil_h__
8
#define nsNetUtil_h__
9
10
#include <functional>
11
#include "mozilla/Maybe.h"
12
#include "nsCOMPtr.h"
13
#include "nsIInterfaceRequestor.h"
14
#include "nsIInterfaceRequestorUtils.h"
15
#include "nsILoadGroup.h"
16
#include "nsINestedURI.h"
17
#include "nsINetUtil.h"
18
#include "nsIRequest.h"
19
#include "nsILoadInfo.h"
20
#include "nsIIOService.h"
21
#include "nsIURI.h"
22
#include "mozilla/NotNull.h"
23
#include "mozilla/Services.h"
24
#include "mozilla/Unused.h"
25
#include "nsNetCID.h"
26
#include "nsReadableUtils.h"
27
#include "nsServiceManagerUtils.h"
28
#include "nsString.h"
29
30
class nsIPrincipal;
31
class nsIAsyncStreamCopier;
32
class nsIAuthPrompt;
33
class nsIAuthPrompt2;
34
class nsIChannel;
35
class nsIChannelPolicy;
36
class nsICookieSettings;
37
class nsIDownloadObserver;
38
class nsIEventTarget;
39
class nsIFileProtocolHandler;
40
class nsIFileStream;
41
class nsIHttpChannel;
42
class nsIInputStream;
43
class nsIInputStreamPump;
44
class nsIInterfaceRequestor;
45
class nsIOutputStream;
46
class nsIParentChannel;
47
class nsIPersistentProperties;
48
class nsIProxyInfo;
49
class nsIRequestObserver;
50
class nsIStreamListener;
51
class nsIStreamLoader;
52
class nsIStreamLoaderObserver;
53
class nsIIncrementalStreamLoader;
54
class nsIIncrementalStreamLoaderObserver;
55
56
namespace mozilla {
57
class Encoding;
58
class OriginAttributes;
59
namespace dom {
60
class ClientInfo;
61
class PerformanceStorage;
62
class ServiceWorkerDescriptor;
63
} // namespace dom
64
} // namespace mozilla
65
66
template <class>
67
class nsCOMPtr;
68
template <typename>
69
struct already_AddRefed;
70
71
already_AddRefed<nsIIOService> do_GetIOService(nsresult* error = nullptr);
72
73
already_AddRefed<nsINetUtil> do_GetNetUtil(nsresult* error = nullptr);
74
75
// private little helper function... don't call this directly!
76
nsresult net_EnsureIOService(nsIIOService** ios, nsCOMPtr<nsIIOService>& grip);
77
78
nsresult NS_NewURI(nsIURI** result, const nsACString& spec,
79
const char* charset = nullptr, nsIURI* baseURI = nullptr,
80
nsIIOService* ioService =
81
nullptr); // pass in nsIIOService to optimize callers
82
83
nsresult NS_NewURI(nsIURI** result, const nsACString& spec,
84
mozilla::NotNull<const mozilla::Encoding*> encoding,
85
nsIURI* baseURI = nullptr,
86
nsIIOService* ioService =
87
nullptr); // pass in nsIIOService to optimize callers
88
89
nsresult NS_NewURI(nsIURI** result, const nsAString& spec,
90
const char* charset = nullptr, nsIURI* baseURI = nullptr,
91
nsIIOService* ioService =
92
nullptr); // pass in nsIIOService to optimize callers
93
94
nsresult NS_NewURI(nsIURI** result, const nsAString& spec,
95
mozilla::NotNull<const mozilla::Encoding*> encoding,
96
nsIURI* baseURI = nullptr,
97
nsIIOService* ioService =
98
nullptr); // pass in nsIIOService to optimize callers
99
100
nsresult NS_NewURI(nsIURI** result, const char* spec, nsIURI* baseURI = nullptr,
101
nsIIOService* ioService =
102
nullptr); // pass in nsIIOService to optimize callers
103
104
nsresult NS_NewFileURI(
105
nsIURI** result, nsIFile* spec,
106
nsIIOService* ioService =
107
nullptr); // pass in nsIIOService to optimize callers
108
109
// These methods will only mutate the URI if the ref of aInput doesn't already
110
// match the ref we are trying to set.
111
// If aInput has no ref, and we are calling NS_GetURIWithoutRef, or
112
// NS_GetURIWithNewRef with an empty string, then aOutput will be the same
113
// as aInput. The same is true if aRef is already equal to the ref of aInput.
114
// This is OK because URIs are immutable and threadsafe.
115
// If the URI doesn't support ref fragments aOutput will be the same as aInput.
116
nsresult NS_GetURIWithNewRef(nsIURI* aInput, const nsACString& aRef,
117
nsIURI** aOutput);
118
nsresult NS_GetURIWithoutRef(nsIURI* aInput, nsIURI** aOutput);
119
120
nsresult NS_GetSanitizedURIStringFromURI(nsIURI* aUri,
121
nsAString& aSanitizedSpec);
122
123
/*
124
* How to create a new Channel, using NS_NewChannel,
125
* NS_NewChannelWithTriggeringPrincipal,
126
* NS_NewInputStreamChannel, NS_NewChannelInternal
127
* and it's variations:
128
*
129
* What specific API function to use:
130
* * The NS_NewChannelInternal functions should almost never be directly
131
* called outside of necko code.
132
* * If possible, use NS_NewChannel() providing a loading *nsINode*
133
* * If no loading *nsINode* is available, try calling NS_NewChannel() providing
134
* a loading *ClientInfo*.
135
* * If no loading *nsINode* or *ClientInfo* are available, call NS_NewChannel()
136
* providing a loading *nsIPrincipal*.
137
* * Call NS_NewChannelWithTriggeringPrincipal if the triggeringPrincipal
138
* is different from the loadingPrincipal.
139
* * Call NS_NewChannelInternal() providing aLoadInfo object in cases where
140
* you already have loadInfo object, e.g in case of a channel redirect.
141
*
142
* @param aURI
143
* nsIURI from which to make a channel
144
* @param aLoadingNode
145
* @param aLoadingPrincipal
146
* @param aTriggeringPrincipal
147
* @param aSecurityFlags
148
* @param aContentPolicyType
149
* These will be used as values for the nsILoadInfo object on the
150
* created channel. For details, see nsILoadInfo in nsILoadInfo.idl
151
*
152
* Please note, if you provide both a loadingNode and a loadingPrincipal,
153
* then loadingPrincipal must be equal to loadingNode->NodePrincipal().
154
* But less error prone is to just supply a loadingNode.
155
*
156
* Note, if you provide a loading ClientInfo its principal must match the
157
* loading principal. Currently you must pass both as the loading principal
158
* may have additional mutable values like CSP on it. In the future these
159
* will be removed from nsIPrincipal and the API can be changed to take just
160
* the loading ClientInfo.
161
*
162
* Keep in mind that URIs coming from a webpage should *never* use the
163
* systemPrincipal as the loadingPrincipal.
164
*/
165
nsresult NS_NewChannelInternal(
166
nsIChannel** outChannel, nsIURI* aUri, nsINode* aLoadingNode,
167
nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal,
168
const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
169
const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
170
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
171
nsICookieSettings* aCookieSettings = nullptr,
172
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
173
nsILoadGroup* aLoadGroup = nullptr,
174
nsIInterfaceRequestor* aCallbacks = nullptr,
175
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
176
nsIIOService* aIoService = nullptr);
177
178
// See NS_NewChannelInternal for usage and argument description
179
nsresult NS_NewChannelInternal(
180
nsIChannel** outChannel, nsIURI* aUri, nsILoadInfo* aLoadInfo,
181
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
182
nsILoadGroup* aLoadGroup = nullptr,
183
nsIInterfaceRequestor* aCallbacks = nullptr,
184
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
185
nsIIOService* aIoService = nullptr);
186
187
// See NS_NewChannelInternal for usage and argument description
188
nsresult /*NS_NewChannelWithNodeAndTriggeringPrincipal */
189
NS_NewChannelWithTriggeringPrincipal(
190
nsIChannel** outChannel, nsIURI* aUri, nsINode* aLoadingNode,
191
nsIPrincipal* aTriggeringPrincipal, nsSecurityFlags aSecurityFlags,
192
nsContentPolicyType aContentPolicyType,
193
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
194
nsILoadGroup* aLoadGroup = nullptr,
195
nsIInterfaceRequestor* aCallbacks = nullptr,
196
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
197
nsIIOService* aIoService = nullptr);
198
199
// See NS_NewChannelInternal for usage and argument description
200
nsresult NS_NewChannelWithTriggeringPrincipal(
201
nsIChannel** outChannel, nsIURI* aUri, nsIPrincipal* aLoadingPrincipal,
202
nsIPrincipal* aTriggeringPrincipal, nsSecurityFlags aSecurityFlags,
203
nsContentPolicyType aContentPolicyType,
204
nsICookieSettings* aCookieSettings = nullptr,
205
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
206
nsILoadGroup* aLoadGroup = nullptr,
207
nsIInterfaceRequestor* aCallbacks = nullptr,
208
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
209
nsIIOService* aIoService = nullptr);
210
211
// See NS_NewChannelInternal for usage and argument description
212
nsresult NS_NewChannelWithTriggeringPrincipal(
213
nsIChannel** outChannel, nsIURI* aUri, nsIPrincipal* aLoadingPrincipal,
214
nsIPrincipal* aTriggeringPrincipal,
215
const mozilla::dom::ClientInfo& aLoadingClientInfo,
216
const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
217
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
218
nsICookieSettings* aCookieSettings = nullptr,
219
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
220
nsILoadGroup* aLoadGroup = nullptr,
221
nsIInterfaceRequestor* aCallbacks = nullptr,
222
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
223
nsIIOService* aIoService = nullptr);
224
225
// See NS_NewChannelInternal for usage and argument description
226
nsresult NS_NewChannel(
227
nsIChannel** outChannel, nsIURI* aUri, nsINode* aLoadingNode,
228
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
229
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
230
nsILoadGroup* aLoadGroup = nullptr,
231
nsIInterfaceRequestor* aCallbacks = nullptr,
232
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
233
nsIIOService* aIoService = nullptr);
234
235
// See NS_NewChannelInternal for usage and argument description
236
nsresult NS_NewChannel(
237
nsIChannel** outChannel, nsIURI* aUri, nsIPrincipal* aLoadingPrincipal,
238
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
239
nsICookieSettings* aCookieSettings = nullptr,
240
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
241
nsILoadGroup* aLoadGroup = nullptr,
242
nsIInterfaceRequestor* aCallbacks = nullptr,
243
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
244
nsIIOService* aIoService = nullptr);
245
246
// See NS_NewChannelInternal for usage and argument description
247
nsresult NS_NewChannel(
248
nsIChannel** outChannel, nsIURI* aUri, nsIPrincipal* aLoadingPrincipal,
249
const mozilla::dom::ClientInfo& aLoadingClientInfo,
250
const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
251
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
252
nsICookieSettings* aCookieSettings = nullptr,
253
mozilla::dom::PerformanceStorage* aPerformanceStorage = nullptr,
254
nsILoadGroup* aLoadGroup = nullptr,
255
nsIInterfaceRequestor* aCallbacks = nullptr,
256
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
257
nsIIOService* aIoService = nullptr);
258
259
nsresult NS_GetIsDocumentChannel(nsIChannel* aChannel, bool* aIsDocument);
260
261
nsresult NS_MakeAbsoluteURI(nsACString& result, const nsACString& spec,
262
nsIURI* baseURI);
263
264
nsresult NS_MakeAbsoluteURI(char** result, const char* spec, nsIURI* baseURI);
265
266
nsresult NS_MakeAbsoluteURI(nsAString& result, const nsAString& spec,
267
nsIURI* baseURI);
268
269
/**
270
* This function is a helper function to get a scheme's default port.
271
*/
272
int32_t NS_GetDefaultPort(const char* scheme,
273
nsIIOService* ioService = nullptr);
274
275
/**
276
* This function is a helper function to apply the ToAscii conversion
277
* to a string
278
*/
279
bool NS_StringToACE(const nsACString& idn, nsACString& result);
280
281
/**
282
* This function is a helper function to get a protocol's default port if the
283
* URI does not specify a port explicitly. Returns -1 if this protocol has no
284
* concept of ports or if there was an error getting the port.
285
*/
286
int32_t NS_GetRealPort(nsIURI* aURI);
287
288
nsresult NS_NewInputStreamChannelInternal(
289
nsIChannel** outChannel, nsIURI* aUri,
290
already_AddRefed<nsIInputStream> aStream, const nsACString& aContentType,
291
const nsACString& aContentCharset, nsILoadInfo* aLoadInfo);
292
293
nsresult NS_NewInputStreamChannelInternal(
294
nsIChannel** outChannel, nsIURI* aUri,
295
already_AddRefed<nsIInputStream> aStream, const nsACString& aContentType,
296
const nsACString& aContentCharset, nsINode* aLoadingNode,
297
nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal,
298
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType);
299
300
nsresult NS_NewInputStreamChannel(
301
nsIChannel** outChannel, nsIURI* aUri,
302
already_AddRefed<nsIInputStream> aStream, nsIPrincipal* aLoadingPrincipal,
303
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
304
const nsACString& aContentType = EmptyCString(),
305
const nsACString& aContentCharset = EmptyCString());
306
307
nsresult NS_NewInputStreamChannelInternal(
308
nsIChannel** outChannel, nsIURI* aUri, const nsAString& aData,
309
const nsACString& aContentType, nsINode* aLoadingNode,
310
nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal,
311
nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
312
bool aIsSrcdocChannel = false);
313
314
nsresult NS_NewInputStreamChannelInternal(nsIChannel** outChannel, nsIURI* aUri,
315
const nsAString& aData,
316
const nsACString& aContentType,
317
nsILoadInfo* aLoadInfo,
318
bool aIsSrcdocChannel = false);
319
320
nsresult NS_NewInputStreamChannel(nsIChannel** outChannel, nsIURI* aUri,
321
const nsAString& aData,
322
const nsACString& aContentType,
323
nsIPrincipal* aLoadingPrincipal,
324
nsSecurityFlags aSecurityFlags,
325
nsContentPolicyType aContentPolicyType,
326
bool aIsSrcdocChannel = false);
327
328
nsresult NS_NewInputStreamPump(nsIInputStreamPump** aResult,
329
already_AddRefed<nsIInputStream> aStream,
330
uint32_t aSegsize = 0, uint32_t aSegcount = 0,
331
bool aCloseWhenDone = false,
332
nsIEventTarget* aMainThreadTarget = nullptr);
333
334
nsresult NS_NewLoadGroup(nsILoadGroup** result, nsIRequestObserver* obs);
335
336
// Create a new nsILoadGroup that will match the given principal.
337
nsresult NS_NewLoadGroup(nsILoadGroup** aResult, nsIPrincipal* aPrincipal);
338
339
// Determine if the given loadGroup/principal pair will produce a principal
340
// with similar permissions when passed to NS_NewChannel(). This checks for
341
// things like making sure the browser element flag matches. Without
342
// an appropriate load group these values can be lost when getting the result
343
// principal back out of the channel. Null principals are also always allowed
344
// as they do not have permissions to actually use the load group.
345
bool NS_LoadGroupMatchesPrincipal(nsILoadGroup* aLoadGroup,
346
nsIPrincipal* aPrincipal);
347
348
nsresult NS_NewDownloader(nsIStreamListener** result,
349
nsIDownloadObserver* observer,
350
nsIFile* downloadLocation = nullptr);
351
352
nsresult NS_NewStreamLoader(nsIStreamLoader** result,
353
nsIStreamLoaderObserver* observer,
354
nsIRequestObserver* requestObserver = nullptr);
355
356
nsresult NS_NewIncrementalStreamLoader(
357
nsIIncrementalStreamLoader** result,
358
nsIIncrementalStreamLoaderObserver* observer);
359
360
nsresult NS_NewStreamLoaderInternal(
361
nsIStreamLoader** outStream, nsIURI* aUri,
362
nsIStreamLoaderObserver* aObserver, nsINode* aLoadingNode,
363
nsIPrincipal* aLoadingPrincipal, nsSecurityFlags aSecurityFlags,
364
nsContentPolicyType aContentPolicyType, nsILoadGroup* aLoadGroup = nullptr,
365
nsIInterfaceRequestor* aCallbacks = nullptr,
366
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL);
367
368
nsresult NS_NewStreamLoader(nsIStreamLoader** outStream, nsIURI* aUri,
369
nsIStreamLoaderObserver* aObserver,
370
nsINode* aLoadingNode,
371
nsSecurityFlags aSecurityFlags,
372
nsContentPolicyType aContentPolicyType,
373
nsILoadGroup* aLoadGroup = nullptr,
374
nsIInterfaceRequestor* aCallbacks = nullptr,
375
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL);
376
377
nsresult NS_NewStreamLoader(nsIStreamLoader** outStream, nsIURI* aUri,
378
nsIStreamLoaderObserver* aObserver,
379
nsIPrincipal* aLoadingPrincipal,
380
nsSecurityFlags aSecurityFlags,
381
nsContentPolicyType aContentPolicyType,
382
nsILoadGroup* aLoadGroup = nullptr,
383
nsIInterfaceRequestor* aCallbacks = nullptr,
384
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL);
385
386
nsresult NS_NewSyncStreamListener(nsIStreamListener** result,
387
nsIInputStream** stream);
388
389
/**
390
* Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
391
* AsyncOpen method.
392
*
393
* NOTE: Reading from the returned nsIInputStream may spin the current
394
* thread's event queue, which could result in any event being processed.
395
*/
396
nsresult NS_ImplementChannelOpen(nsIChannel* channel, nsIInputStream** result);
397
398
nsresult NS_NewRequestObserverProxy(nsIRequestObserver** result,
399
nsIRequestObserver* observer,
400
nsISupports* context);
401
402
nsresult NS_NewSimpleStreamListener(nsIStreamListener** result,
403
nsIOutputStream* sink,
404
nsIRequestObserver* observer = nullptr);
405
406
nsresult NS_CheckPortSafety(int32_t port, const char* scheme,
407
nsIIOService* ioService = nullptr);
408
409
// Determine if this URI is using a safe port.
410
nsresult NS_CheckPortSafety(nsIURI* uri);
411
412
nsresult NS_NewProxyInfo(const nsACString& type, const nsACString& host,
413
int32_t port, uint32_t flags, nsIProxyInfo** result);
414
415
nsresult NS_GetFileProtocolHandler(nsIFileProtocolHandler** result,
416
nsIIOService* ioService = nullptr);
417
418
nsresult NS_GetFileFromURLSpec(const nsACString& inURL, nsIFile** result,
419
nsIIOService* ioService = nullptr);
420
421
nsresult NS_GetURLSpecFromFile(nsIFile* file, nsACString& url,
422
nsIIOService* ioService = nullptr);
423
424
/**
425
* Converts the nsIFile to the corresponding URL string.
426
* Should only be called on files which are not directories,
427
* is otherwise identical to NS_GetURLSpecFromFile, but is
428
* usually more efficient.
429
* Warning: this restriction may not be enforced at runtime!
430
*/
431
nsresult NS_GetURLSpecFromActualFile(nsIFile* file, nsACString& url,
432
nsIIOService* ioService = nullptr);
433
434
/**
435
* Converts the nsIFile to the corresponding URL string.
436
* Should only be called on files which are directories,
437
* is otherwise identical to NS_GetURLSpecFromFile, but is
438
* usually more efficient.
439
* Warning: this restriction may not be enforced at runtime!
440
*/
441
nsresult NS_GetURLSpecFromDir(nsIFile* file, nsACString& url,
442
nsIIOService* ioService = nullptr);
443
444
/**
445
* Obtains the referrer for a given channel. This first tries to obtain the
446
* referrer from the property docshell.internalReferrer, and if that doesn't
447
* work and the channel is an nsIHTTPChannel, we check it's referrer property.
448
*
449
*/
450
void NS_GetReferrerFromChannel(nsIChannel* channel, nsIURI** referrer);
451
452
nsresult NS_ParseRequestContentType(const nsACString& rawContentType,
453
nsCString& contentType,
454
nsCString& contentCharset);
455
456
nsresult NS_ParseResponseContentType(const nsACString& rawContentType,
457
nsCString& contentType,
458
nsCString& contentCharset);
459
460
nsresult NS_ExtractCharsetFromContentType(const nsACString& rawContentType,
461
nsCString& contentCharset,
462
bool* hadCharset,
463
int32_t* charsetStart,
464
int32_t* charsetEnd);
465
466
nsresult NS_NewLocalFileInputStream(nsIInputStream** result, nsIFile* file,
467
int32_t ioFlags = -1, int32_t perm = -1,
468
int32_t behaviorFlags = 0);
469
470
nsresult NS_NewLocalFileOutputStream(nsIOutputStream** result, nsIFile* file,
471
int32_t ioFlags = -1, int32_t perm = -1,
472
int32_t behaviorFlags = 0);
473
474
// returns a file output stream which can be QI'ed to nsISafeOutputStream.
475
nsresult NS_NewAtomicFileOutputStream(nsIOutputStream** result, nsIFile* file,
476
int32_t ioFlags = -1, int32_t perm = -1,
477
int32_t behaviorFlags = 0);
478
479
// returns a file output stream which can be QI'ed to nsISafeOutputStream.
480
nsresult NS_NewSafeLocalFileOutputStream(nsIOutputStream** result,
481
nsIFile* file, int32_t ioFlags = -1,
482
int32_t perm = -1,
483
int32_t behaviorFlags = 0);
484
485
nsresult NS_NewLocalFileStream(nsIFileStream** result, nsIFile* file,
486
int32_t ioFlags = -1, int32_t perm = -1,
487
int32_t behaviorFlags = 0);
488
489
MOZ_MUST_USE nsresult NS_NewBufferedInputStream(
490
nsIInputStream** aResult, already_AddRefed<nsIInputStream> aInputStream,
491
uint32_t aBufferSize);
492
493
// note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
494
// provided stream supports it.
495
nsresult NS_NewBufferedOutputStream(
496
nsIOutputStream** aResult, already_AddRefed<nsIOutputStream> aOutputStream,
497
uint32_t aBufferSize);
498
499
/**
500
* This function reads an inputStream and stores its content into a buffer. In
501
* general, you should avoid using this function because, it blocks the current
502
* thread until the operation is done.
503
* If the inputStream is async, the reading happens on an I/O thread.
504
*
505
* @param aInputStream the inputStream.
506
* @param aDest the destination buffer. if *aDest is null, it will be allocated
507
* with the size of the written data. if aDest is not null, aCount
508
* must greater than 0.
509
* @param aCount the amount of data to read. Use -1 if you want that all the
510
* stream is read.
511
* @param aWritten this pointer will be used to store the number of data
512
* written in the buffer. If you don't need, pass nullptr.
513
*/
514
nsresult NS_ReadInputStreamToBuffer(nsIInputStream* aInputStream, void** aDest,
515
int64_t aCount,
516
uint64_t* aWritten = nullptr);
517
518
/**
519
* See the comment for NS_ReadInputStreamToBuffer
520
*/
521
nsresult NS_ReadInputStreamToString(nsIInputStream* aInputStream,
522
nsACString& aDest, int64_t aCount,
523
uint64_t* aWritten = nullptr);
524
525
nsresult NS_LoadPersistentPropertiesFromURISpec(
526
nsIPersistentProperties** outResult, const nsACString& aSpec);
527
528
/**
529
* NS_QueryNotificationCallbacks implements the canonical algorithm for
530
* querying interfaces from a channel's notification callbacks. It first
531
* searches the channel's notificationCallbacks attribute, and if the interface
532
* is not found there, then it inspects the notificationCallbacks attribute of
533
* the channel's loadGroup.
534
*
535
* Note: templatized only because nsIWebSocketChannel is currently not an
536
* nsIChannel.
537
*/
538
template <class T>
539
inline void NS_QueryNotificationCallbacks(T* channel, const nsIID& iid,
540
void** result) {
541
MOZ_ASSERT(channel, "null channel");
542
*result = nullptr;
543
544
nsCOMPtr<nsIInterfaceRequestor> cbs;
545
mozilla::Unused << channel->GetNotificationCallbacks(getter_AddRefs(cbs));
546
if (cbs) cbs->GetInterface(iid, result);
547
if (!*result) {
548
// try load group's notification callbacks...
549
nsCOMPtr<nsILoadGroup> loadGroup;
550
mozilla::Unused << channel->GetLoadGroup(getter_AddRefs(loadGroup));
551
if (loadGroup) {
552
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
553
if (cbs) cbs->GetInterface(iid, result);
554
}
555
}
556
}
557
558
// template helper:
559
// Note: "class C" templatized only because nsIWebSocketChannel is currently not
560
// an nsIChannel.
561
562
template <class C, class T>
563
inline void NS_QueryNotificationCallbacks(C* channel, nsCOMPtr<T>& result) {
564
NS_QueryNotificationCallbacks(channel, NS_GET_TEMPLATE_IID(T),
565
getter_AddRefs(result));
566
}
567
568
/**
569
* Alternate form of NS_QueryNotificationCallbacks designed for use by
570
* nsIChannel implementations.
571
*/
572
inline void NS_QueryNotificationCallbacks(nsIInterfaceRequestor* callbacks,
573
nsILoadGroup* loadGroup,
574
const nsIID& iid, void** result) {
575
*result = nullptr;
576
577
if (callbacks) callbacks->GetInterface(iid, result);
578
if (!*result) {
579
// try load group's notification callbacks...
580
if (loadGroup) {
581
nsCOMPtr<nsIInterfaceRequestor> cbs;
582
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
583
if (cbs) cbs->GetInterface(iid, result);
584
}
585
}
586
}
587
588
/**
589
* Returns true if channel is using Private Browsing, or false if not.
590
* Returns false if channel's callbacks don't implement nsILoadContext.
591
*/
592
bool NS_UsePrivateBrowsing(nsIChannel* channel);
593
594
/**
595
* Extract the OriginAttributes from the channel's triggering principal.
596
* If aUsingStoragePrincipal is set to true, the originAttributes could have
597
* first-party isolation domain set to the top-level URI.
598
*/
599
bool NS_GetOriginAttributes(nsIChannel* aChannel,
600
mozilla::OriginAttributes& aAttributes,
601
bool aUsingStoragePrincipal = false);
602
603
/**
604
* Returns true if the channel has visited any cross-origin URLs on any
605
* URLs that it was redirected through.
606
*/
607
bool NS_HasBeenCrossOrigin(nsIChannel* aChannel, bool aReport = false);
608
609
/**
610
* Returns true if the channel is a safe top-level navigation.
611
*/
612
bool NS_IsSafeTopLevelNav(nsIChannel* aChannel);
613
614
/**
615
* Returns true if the channel is a foreign with respect to the host-uri.
616
* For loads of TYPE_DOCUMENT, this function returns true if it's a
617
* cross origin navigation.
618
*/
619
bool NS_IsSameSiteForeign(nsIChannel* aChannel, nsIURI* aHostURI);
620
621
// Unique first-party domain for separating the safebrowsing cookie.
622
// Note if this value is changed, code in test_cookiejars_safebrowsing.js and
623
// nsUrlClassifierHashCompleter.js should also be changed.
624
#define NECKO_SAFEBROWSING_FIRST_PARTY_DOMAIN \
625
"safebrowsing.86868755-6b82-4842-b301-72671a0db32e.mozilla"
626
627
// Unique first-party domain for separating about uri.
628
#define ABOUT_URI_FIRST_PARTY_DOMAIN \
629
"about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla"
630
631
/**
632
* Determines whether appcache should be checked for a given principal.
633
*/
634
bool NS_ShouldCheckAppCache(nsIPrincipal* aPrincipal);
635
636
/**
637
* Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
638
* method is provided mainly for use by other methods in this file.
639
*
640
* *aAuthPrompt2 should be set to null before calling this function.
641
*/
642
void NS_WrapAuthPrompt(nsIAuthPrompt* aAuthPrompt,
643
nsIAuthPrompt2** aAuthPrompt2);
644
645
/**
646
* Gets an auth prompt from an interface requestor. This takes care of wrapping
647
* an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2.
648
*/
649
void NS_QueryAuthPrompt2(nsIInterfaceRequestor* aCallbacks,
650
nsIAuthPrompt2** aAuthPrompt);
651
652
/**
653
* Gets an nsIAuthPrompt2 from a channel. Use this instead of
654
* NS_QueryNotificationCallbacks for better backwards compatibility.
655
*/
656
void NS_QueryAuthPrompt2(nsIChannel* aChannel, nsIAuthPrompt2** aAuthPrompt);
657
658
/* template helper */
659
template <class T>
660
inline void NS_QueryNotificationCallbacks(nsIInterfaceRequestor* callbacks,
661
nsILoadGroup* loadGroup,
662
nsCOMPtr<T>& result) {
663
NS_QueryNotificationCallbacks(callbacks, loadGroup, NS_GET_TEMPLATE_IID(T),
664
getter_AddRefs(result));
665
}
666
667
/* template helper */
668
template <class T>
669
inline void NS_QueryNotificationCallbacks(
670
const nsCOMPtr<nsIInterfaceRequestor>& aCallbacks,
671
const nsCOMPtr<nsILoadGroup>& aLoadGroup, nsCOMPtr<T>& aResult) {
672
NS_QueryNotificationCallbacks(aCallbacks.get(), aLoadGroup.get(), aResult);
673
}
674
675
/* template helper */
676
template <class T>
677
inline void NS_QueryNotificationCallbacks(const nsCOMPtr<nsIChannel>& aChannel,
678
nsCOMPtr<T>& aResult) {
679
NS_QueryNotificationCallbacks(aChannel.get(), aResult);
680
}
681
682
/**
683
* This function returns a nsIInterfaceRequestor instance that returns the
684
* same result as NS_QueryNotificationCallbacks when queried. It is useful
685
* as the value for nsISocketTransport::securityCallbacks.
686
*/
687
nsresult NS_NewNotificationCallbacksAggregation(
688
nsIInterfaceRequestor* callbacks, nsILoadGroup* loadGroup,
689
nsIEventTarget* target, nsIInterfaceRequestor** result);
690
691
nsresult NS_NewNotificationCallbacksAggregation(
692
nsIInterfaceRequestor* callbacks, nsILoadGroup* loadGroup,
693
nsIInterfaceRequestor** result);
694
695
/**
696
* Helper function for testing online/offline state of the browser.
697
*/
698
bool NS_IsOffline();
699
700
/**
701
* Helper functions for implementing nsINestedURI::innermostURI.
702
*
703
* Note that NS_DoImplGetInnermostURI is "private" -- call
704
* NS_ImplGetInnermostURI instead.
705
*/
706
nsresult NS_DoImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result);
707
708
nsresult NS_ImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result);
709
710
/**
711
* Helper function for testing whether the given URI, or any of its
712
* inner URIs, has all the given protocol flags.
713
*/
714
nsresult NS_URIChainHasFlags(nsIURI* uri, uint32_t flags, bool* result);
715
716
/**
717
* Helper function for getting the innermost URI for a given URI. The return
718
* value could be just the object passed in if it's not a nested URI.
719
*/
720
already_AddRefed<nsIURI> NS_GetInnermostURI(nsIURI* aURI);
721
722
/**
723
* Helper function for getting the host name of the innermost URI for a given
724
* URI. The return value could be the host name of the URI passed in if it's
725
* not a nested URI.
726
*/
727
inline nsresult NS_GetInnermostURIHost(nsIURI* aURI, nsACString& aHost) {
728
aHost.Truncate();
729
730
// This block is optimized in order to avoid the overhead of calling
731
// NS_GetInnermostURI() which incurs a lot of overhead in terms of
732
// AddRef/Release calls.
733
nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(aURI);
734
if (nestedURI) {
735
// We have a nested URI!
736
nsCOMPtr<nsIURI> uri;
737
nsresult rv = nestedURI->GetInnermostURI(getter_AddRefs(uri));
738
if (NS_FAILED(rv)) {
739
return rv;
740
}
741
742
rv = uri->GetAsciiHost(aHost);
743
if (NS_FAILED(rv)) {
744
return rv;
745
}
746
} else {
747
// We have a non-nested URI!
748
nsresult rv = aURI->GetAsciiHost(aHost);
749
if (NS_FAILED(rv)) {
750
return rv;
751
}
752
}
753
754
return NS_OK;
755
}
756
757
/**
758
* Get the "final" URI for a channel. This is either channel's load info
759
* resultPrincipalURI, if set, or GetOriginalURI. In most cases (but not all)
760
* load info resultPrincipalURI, if set, corresponds to URI of the channel if
761
* it's required to represent the actual principal for the channel.
762
*/
763
nsresult NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri);
764
765
// NS_SecurityHashURI must return the same hash value for any two URIs that
766
// compare equal according to NS_SecurityCompareURIs. Unfortunately, in the
767
// case of files, it's not clear we can do anything better than returning
768
// the schemeHash, so hashing files degenerates to storing them in a list.
769
uint32_t NS_SecurityHashURI(nsIURI* aURI);
770
771
bool NS_SecurityCompareURIs(nsIURI* aSourceURI, nsIURI* aTargetURI,
772
bool aStrictFileOriginPolicy);
773
774
bool NS_URIIsLocalFile(nsIURI* aURI);
775
776
// When strict file origin policy is enabled, SecurityCompareURIs will fail for
777
// file URIs that do not point to the same local file. This call provides an
778
// alternate file-specific origin check that allows target files that are
779
// contained in the same directory as the source.
780
//
782
bool NS_RelaxStrictFileOriginPolicy(nsIURI* aTargetURI, nsIURI* aSourceURI,
783
bool aAllowDirectoryTarget = false);
784
785
bool NS_IsInternalSameURIRedirect(nsIChannel* aOldChannel,
786
nsIChannel* aNewChannel, uint32_t aFlags);
787
788
bool NS_IsHSTSUpgradeRedirect(nsIChannel* aOldChannel, nsIChannel* aNewChannel,
789
uint32_t aFlags);
790
791
nsresult NS_LinkRedirectChannels(uint32_t channelId,
792
nsIParentChannel* parentChannel,
793
nsIChannel** _result);
794
795
/**
796
* Helper function which checks whether the channel can be
797
* openend using Open() or has to fall back to opening
798
* the channel using Open().
799
*/
800
nsresult NS_MaybeOpenChannelUsingOpen(nsIChannel* aChannel,
801
nsIInputStream** aStream);
802
803
/**
804
* Helper function which checks whether the channel can be
805
* openend using AsyncOpen() or has to fall back to opening
806
* the channel using AsyncOpen().
807
*/
808
nsresult NS_MaybeOpenChannelUsingAsyncOpen(nsIChannel* aChannel,
809
nsIStreamListener* aListener);
810
811
/** Given the first (disposition) token from a Content-Disposition header,
812
* tell whether it indicates the content is inline or attachment
813
* @param aDispToken the disposition token from the content-disposition header
814
*/
815
uint32_t NS_GetContentDispositionFromToken(const nsAString& aDispToken);
816
817
/** Determine the disposition (inline/attachment) of the content based on the
818
* Content-Disposition header
819
* @param aHeader the content-disposition header (full value)
820
* @param aChan the channel the header came from
821
*/
822
uint32_t NS_GetContentDispositionFromHeader(const nsACString& aHeader,
823
nsIChannel* aChan = nullptr);
824
825
/** Extracts the filename out of a content-disposition header
826
* @param aFilename [out] The filename. Can be empty on error.
827
* @param aDisposition Value of a Content-Disposition header
828
* @param aURI Optional. Will be used to get a fallback charset for the
829
* filename, if it is QI'able to nsIURL
830
*/
831
nsresult NS_GetFilenameFromDisposition(nsAString& aFilename,
832
const nsACString& aDisposition,
833
nsIURI* aURI = nullptr);
834
835
/**
836
* Make sure Personal Security Manager is initialized
837
*/
838
void net_EnsurePSMInit();
839
840
/**
841
* Test whether a URI is "about:blank". |uri| must not be null
842
*/
843
bool NS_IsAboutBlank(nsIURI* uri);
844
845
nsresult NS_GenerateHostPort(const nsCString& host, int32_t port,
846
nsACString& hostLine);
847
848
/**
849
* Sniff the content type for a given request or a given buffer.
850
*
851
* aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or
852
* NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type
853
* in the aSniffedType argument. This argument will not be modified if the
854
* content type could not be sniffed.
855
*/
856
void NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest,
857
const uint8_t* aData, uint32_t aLength,
858
nsACString& aSniffedType);
859
860
/**
861
* Whether the channel was created to load a srcdoc document.
862
* Note that view-source:about:srcdoc is classified as a srcdoc document by
863
* this function, which may not be applicable everywhere.
864
*/
865
bool NS_IsSrcdocChannel(nsIChannel* aChannel);
866
867
/**
868
* Return true if the given string is a reasonable HTTP header value given the
869
* definition in RFC 2616 section 4.2. Currently we don't pay the cost to do
870
* full, sctrict validation here since it would require fulling parsing the
871
* value.
872
*/
873
bool NS_IsReasonableHTTPHeaderValue(const nsACString& aValue);
874
875
/**
876
* Return true if the given string is a valid HTTP token per RFC 2616 section
877
* 2.2.
878
*/
879
bool NS_IsValidHTTPToken(const nsACString& aToken);
880
881
/**
882
* Strip the leading or trailing HTTP whitespace per fetch spec section 2.2.
883
*/
884
void NS_TrimHTTPWhitespace(const nsACString& aSource, nsACString& aDest);
885
886
/**
887
* Return true if the given request must be upgraded to HTTPS.
888
* If |aResultCallback| is provided and the storage is not ready to read, the
889
* result will be sent back through the callback and |aWillCallback| will be
890
* true. Otherwiew, the result will be set to |aShouldUpgrade| and
891
* |aWillCallback| is false.
892
*/
893
nsresult NS_ShouldSecureUpgrade(
894
nsIURI* aURI, nsILoadInfo* aLoadInfo, nsIPrincipal* aChannelResultPrincipal,
895
bool aPrivateBrowsing, bool aAllowSTS,
896
const mozilla::OriginAttributes& aOriginAttributes, bool& aShouldUpgrade,
897
std::function<void(bool, nsresult)>&& aResultCallback, bool& aWillCallback);
898
899
/**
900
* Returns an https URI for channels that need to go through secure upgrades.
901
*/
902
nsresult NS_GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI);
903
904
nsresult NS_CompareLoadInfoAndLoadContext(nsIChannel* aChannel);
905
906
/**
907
* Return true if this channel should be classified by the URL classifier.
908
*/
909
bool NS_ShouldClassifyChannel(nsIChannel* aChannel);
910
911
/**
912
* Helper to set the blocking reason on loadinfo of the channel.
913
*/
914
nsresult NS_SetRequestBlockingReason(nsIChannel* channel, uint32_t reason);
915
nsresult NS_SetRequestBlockingReason(nsILoadInfo* loadInfo, uint32_t reason);
916
nsresult NS_SetRequestBlockingReasonIfNull(nsILoadInfo* loadInfo,
917
uint32_t reason);
918
919
namespace mozilla {
920
namespace net {
921
922
const static uint64_t kJS_MAX_SAFE_UINTEGER = +9007199254740991ULL;
923
const static int64_t kJS_MIN_SAFE_INTEGER = -9007199254740991LL;
924
const static int64_t kJS_MAX_SAFE_INTEGER = +9007199254740991LL;
925
926
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
927
bool InScriptableRange(int64_t val);
928
929
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
930
bool InScriptableRange(uint64_t val);
931
932
/**
933
* Given the value of a single header field (such as
934
* Content-Disposition and Content-Type) and the name of a parameter
935
* (e.g. filename, name, charset), returns the value of the parameter.
936
* See nsIMIMEHeaderParam.idl for more information.
937
*
938
* @param aHeaderVal a header string to get the value of a parameter
939
* from.
940
* @param aParamName the name of a MIME header parameter (e.g.
941
* filename, name, charset). If empty or nullptr,
942
* returns the first (possibly) _unnamed_ 'parameter'.
943
* @return the value of <code>aParamName</code> in Unichar(UTF-16).
944
*/
945
nsresult GetParameterHTTP(const nsACString& aHeaderVal, const char* aParamName,
946
nsAString& aResult);
947
948
/**
949
* Convenience functions for verifying nsIURI schemes. These functions simply
950
* wrap aURI->SchemeIs(), but specify the protocol as part of the function name.
951
*/
952
953
bool SchemeIsHTTP(nsIURI* aURI);
954
bool SchemeIsHTTPS(nsIURI* aURI);
955
bool SchemeIsJavascript(nsIURI* aURI);
956
bool SchemeIsChrome(nsIURI* aURI);
957
bool SchemeIsAbout(nsIURI* aURI);
958
bool SchemeIsBlob(nsIURI* aURI);
959
bool SchemeIsFile(nsIURI* aURI);
960
bool SchemeIsData(nsIURI* aURI);
961
bool SchemeIsViewSource(nsIURI* aURI);
962
bool SchemeIsResource(nsIURI* aURI);
963
bool SchemeIsFTP(nsIURI* aURI);
964
} // namespace net
965
} // namespace mozilla
966
967
#endif // !nsNetUtil_h__