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