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
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
nsICookieSettings* aCookieSettings = 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);
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
nsICookieSettings* aCookieSettings = 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
nsICookieSettings* aCookieSettings = 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);
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
nsICookieSettings* aCookieSettings = 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);
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
nsICookieSettings* aCookieSettings = 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);
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 is a foreign with respect to the host-uri.
607
* For loads of TYPE_DOCUMENT, this function returns true if it's a
608
* cross origin navigation.
609
*/
610
bool NS_IsSameSiteForeign(nsIChannel* aChannel, nsIURI* aHostURI);
611
612
// Unique first-party domain for separating the safebrowsing cookie.
613
// Note if this value is changed, code in test_cookiejars_safebrowsing.js and
614
// nsUrlClassifierHashCompleter.js should also be changed.
615
#define NECKO_SAFEBROWSING_FIRST_PARTY_DOMAIN \
616
"safebrowsing.86868755-6b82-4842-b301-72671a0db32e.mozilla"
617
618
// Unique first-party domain for separating about uri.
619
#define ABOUT_URI_FIRST_PARTY_DOMAIN \
620
"about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla"
621
622
/**
623
* Determines whether appcache should be checked for a given principal.
624
*/
625
bool NS_ShouldCheckAppCache(nsIPrincipal* aPrincipal);
626
627
/**
628
* Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
629
* method is provided mainly for use by other methods in this file.
630
*
631
* *aAuthPrompt2 should be set to null before calling this function.
632
*/
633
void NS_WrapAuthPrompt(nsIAuthPrompt* aAuthPrompt,
634
nsIAuthPrompt2** aAuthPrompt2);
635
636
/**
637
* Gets an auth prompt from an interface requestor. This takes care of wrapping
638
* an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2.
639
*/
640
void NS_QueryAuthPrompt2(nsIInterfaceRequestor* aCallbacks,
641
nsIAuthPrompt2** aAuthPrompt);
642
643
/**
644
* Gets an nsIAuthPrompt2 from a channel. Use this instead of
645
* NS_QueryNotificationCallbacks for better backwards compatibility.
646
*/
647
void NS_QueryAuthPrompt2(nsIChannel* aChannel, nsIAuthPrompt2** aAuthPrompt);
648
649
/* template helper */
650
template <class T>
651
inline void NS_QueryNotificationCallbacks(nsIInterfaceRequestor* callbacks,
652
nsILoadGroup* loadGroup,
653
nsCOMPtr<T>& result) {
654
NS_QueryNotificationCallbacks(callbacks, loadGroup, NS_GET_TEMPLATE_IID(T),
655
getter_AddRefs(result));
656
}
657
658
/* template helper */
659
template <class T>
660
inline void NS_QueryNotificationCallbacks(
661
const nsCOMPtr<nsIInterfaceRequestor>& aCallbacks,
662
const nsCOMPtr<nsILoadGroup>& aLoadGroup, nsCOMPtr<T>& aResult) {
663
NS_QueryNotificationCallbacks(aCallbacks.get(), aLoadGroup.get(), aResult);
664
}
665
666
/* template helper */
667
template <class T>
668
inline void NS_QueryNotificationCallbacks(const nsCOMPtr<nsIChannel>& aChannel,
669
nsCOMPtr<T>& aResult) {
670
NS_QueryNotificationCallbacks(aChannel.get(), aResult);
671
}
672
673
/**
674
* This function returns a nsIInterfaceRequestor instance that returns the
675
* same result as NS_QueryNotificationCallbacks when queried. It is useful
676
* as the value for nsISocketTransport::securityCallbacks.
677
*/
678
nsresult NS_NewNotificationCallbacksAggregation(
679
nsIInterfaceRequestor* callbacks, nsILoadGroup* loadGroup,
680
nsIEventTarget* target, nsIInterfaceRequestor** result);
681
682
nsresult NS_NewNotificationCallbacksAggregation(
683
nsIInterfaceRequestor* callbacks, nsILoadGroup* loadGroup,
684
nsIInterfaceRequestor** result);
685
686
/**
687
* Helper function for testing online/offline state of the browser.
688
*/
689
bool NS_IsOffline();
690
691
/**
692
* Helper functions for implementing nsINestedURI::innermostURI.
693
*
694
* Note that NS_DoImplGetInnermostURI is "private" -- call
695
* NS_ImplGetInnermostURI instead.
696
*/
697
nsresult NS_DoImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result);
698
699
nsresult NS_ImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result);
700
701
/**
702
* Helper function for testing whether the given URI, or any of its
703
* inner URIs, has all the given protocol flags.
704
*/
705
nsresult NS_URIChainHasFlags(nsIURI* uri, uint32_t flags, bool* result);
706
707
/**
708
* Helper function for getting the innermost URI for a given URI. The return
709
* value could be just the object passed in if it's not a nested URI.
710
*/
711
already_AddRefed<nsIURI> NS_GetInnermostURI(nsIURI* aURI);
712
713
/**
714
* Helper function for getting the host name of the innermost URI for a given
715
* URI. The return value could be the host name of the URI passed in if it's
716
* not a nested URI.
717
*/
718
inline nsresult NS_GetInnermostURIHost(nsIURI* aURI, nsACString& aHost) {
719
aHost.Truncate();
720
721
// This block is optimized in order to avoid the overhead of calling
722
// NS_GetInnermostURI() which incurs a lot of overhead in terms of
723
// AddRef/Release calls.
724
nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(aURI);
725
if (nestedURI) {
726
// We have a nested URI!
727
nsCOMPtr<nsIURI> uri;
728
nsresult rv = nestedURI->GetInnermostURI(getter_AddRefs(uri));
729
if (NS_FAILED(rv)) {
730
return rv;
731
}
732
733
rv = uri->GetAsciiHost(aHost);
734
if (NS_FAILED(rv)) {
735
return rv;
736
}
737
} else {
738
// We have a non-nested URI!
739
nsresult rv = aURI->GetAsciiHost(aHost);
740
if (NS_FAILED(rv)) {
741
return rv;
742
}
743
}
744
745
return NS_OK;
746
}
747
748
/**
749
* Get the "final" URI for a channel. This is either channel's load info
750
* resultPrincipalURI, if set, or GetOriginalURI. In most cases (but not all)
751
* load info resultPrincipalURI, if set, corresponds to URI of the channel if
752
* it's required to represent the actual principal for the channel.
753
*/
754
nsresult NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri);
755
756
// NS_SecurityHashURI must return the same hash value for any two URIs that
757
// compare equal according to NS_SecurityCompareURIs. Unfortunately, in the
758
// case of files, it's not clear we can do anything better than returning
759
// the schemeHash, so hashing files degenerates to storing them in a list.
760
uint32_t NS_SecurityHashURI(nsIURI* aURI);
761
762
bool NS_SecurityCompareURIs(nsIURI* aSourceURI, nsIURI* aTargetURI,
763
bool aStrictFileOriginPolicy);
764
765
bool NS_URIIsLocalFile(nsIURI* aURI);
766
767
// When strict file origin policy is enabled, SecurityCompareURIs will fail for
768
// file URIs that do not point to the same local file. This call provides an
769
// alternate file-specific origin check that allows target files that are
770
// contained in the same directory as the source.
771
//
773
bool NS_RelaxStrictFileOriginPolicy(nsIURI* aTargetURI, nsIURI* aSourceURI,
774
bool aAllowDirectoryTarget = false);
775
776
bool NS_IsInternalSameURIRedirect(nsIChannel* aOldChannel,
777
nsIChannel* aNewChannel, uint32_t aFlags);
778
779
bool NS_IsHSTSUpgradeRedirect(nsIChannel* aOldChannel, nsIChannel* aNewChannel,
780
uint32_t aFlags);
781
782
nsresult NS_LinkRedirectChannels(uint32_t channelId,
783
nsIParentChannel* parentChannel,
784
nsIChannel** _result);
785
786
/**
787
* Helper function which checks whether the channel can be
788
* openend using Open() or has to fall back to opening
789
* the channel using Open().
790
*/
791
nsresult NS_MaybeOpenChannelUsingOpen(nsIChannel* aChannel,
792
nsIInputStream** aStream);
793
794
/**
795
* Helper function which checks whether the channel can be
796
* openend using AsyncOpen() or has to fall back to opening
797
* the channel using AsyncOpen().
798
*/
799
nsresult NS_MaybeOpenChannelUsingAsyncOpen(nsIChannel* aChannel,
800
nsIStreamListener* aListener);
801
802
/** Given the first (disposition) token from a Content-Disposition header,
803
* tell whether it indicates the content is inline or attachment
804
* @param aDispToken the disposition token from the content-disposition header
805
*/
806
uint32_t NS_GetContentDispositionFromToken(const nsAString& aDispToken);
807
808
/** Determine the disposition (inline/attachment) of the content based on the
809
* Content-Disposition header
810
* @param aHeader the content-disposition header (full value)
811
* @param aChan the channel the header came from
812
*/
813
uint32_t NS_GetContentDispositionFromHeader(const nsACString& aHeader,
814
nsIChannel* aChan = nullptr);
815
816
/** Extracts the filename out of a content-disposition header
817
* @param aFilename [out] The filename. Can be empty on error.
818
* @param aDisposition Value of a Content-Disposition header
819
* @param aURI Optional. Will be used to get a fallback charset for the
820
* filename, if it is QI'able to nsIURL
821
*/
822
nsresult NS_GetFilenameFromDisposition(nsAString& aFilename,
823
const nsACString& aDisposition,
824
nsIURI* aURI = nullptr);
825
826
/**
827
* Make sure Personal Security Manager is initialized
828
*/
829
void net_EnsurePSMInit();
830
831
/**
832
* Test whether a URI is "about:blank". |uri| must not be null
833
*/
834
bool NS_IsAboutBlank(nsIURI* uri);
835
836
nsresult NS_GenerateHostPort(const nsCString& host, int32_t port,
837
nsACString& hostLine);
838
839
/**
840
* Sniff the content type for a given request or a given buffer.
841
*
842
* aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or
843
* NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type
844
* in the aSniffedType argument. This argument will not be modified if the
845
* content type could not be sniffed.
846
*/
847
void NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest,
848
const uint8_t* aData, uint32_t aLength,
849
nsACString& aSniffedType);
850
851
/**
852
* Whether the channel was created to load a srcdoc document.
853
* Note that view-source:about:srcdoc is classified as a srcdoc document by
854
* this function, which may not be applicable everywhere.
855
*/
856
bool NS_IsSrcdocChannel(nsIChannel* aChannel);
857
858
/**
859
* Return true if the given string is a reasonable HTTP header value given the
860
* definition in RFC 2616 section 4.2. Currently we don't pay the cost to do
861
* full, sctrict validation here since it would require fulling parsing the
862
* value.
863
*/
864
bool NS_IsReasonableHTTPHeaderValue(const nsACString& aValue);
865
866
/**
867
* Return true if the given string is a valid HTTP token per RFC 2616 section
868
* 2.2.
869
*/
870
bool NS_IsValidHTTPToken(const nsACString& aToken);
871
872
/**
873
* Strip the leading or trailing HTTP whitespace per fetch spec section 2.2.
874
*/
875
void NS_TrimHTTPWhitespace(const nsACString& aSource, nsACString& aDest);
876
877
/**
878
* Return true if the given request must be upgraded to HTTPS.
879
* If |aResultCallback| is provided and the storage is not ready to read, the
880
* result will be sent back through the callback and |aWillCallback| will be
881
* true. Otherwiew, the result will be set to |aShouldUpgrade| and
882
* |aWillCallback| is false.
883
*/
884
nsresult NS_ShouldSecureUpgrade(
885
nsIURI* aURI, nsILoadInfo* aLoadInfo, nsIPrincipal* aChannelResultPrincipal,
886
bool aPrivateBrowsing, bool aAllowSTS,
887
const mozilla::OriginAttributes& aOriginAttributes, bool& aShouldUpgrade,
888
std::function<void(bool, nsresult)>&& aResultCallback, bool& aWillCallback);
889
890
/**
891
* Returns an https URI for channels that need to go through secure upgrades.
892
*/
893
nsresult NS_GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI);
894
895
nsresult NS_CompareLoadInfoAndLoadContext(nsIChannel* aChannel);
896
897
/**
898
* Return true if this channel should be classified by the URL classifier.
899
*/
900
bool NS_ShouldClassifyChannel(nsIChannel* aChannel);
901
902
/**
903
* Helper to set the blocking reason on loadinfo of the channel.
904
*/
905
nsresult NS_SetRequestBlockingReason(nsIChannel* channel, uint32_t reason);
906
nsresult NS_SetRequestBlockingReason(nsILoadInfo* loadInfo, uint32_t reason);
907
nsresult NS_SetRequestBlockingReasonIfNull(nsILoadInfo* loadInfo,
908
uint32_t reason);
909
910
namespace mozilla {
911
namespace net {
912
913
const static uint64_t kJS_MAX_SAFE_UINTEGER = +9007199254740991ULL;
914
const static int64_t kJS_MIN_SAFE_INTEGER = -9007199254740991LL;
915
const static int64_t kJS_MAX_SAFE_INTEGER = +9007199254740991LL;
916
917
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
918
bool InScriptableRange(int64_t val);
919
920
// Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
921
bool InScriptableRange(uint64_t val);
922
923
/**
924
* Given the value of a single header field (such as
925
* Content-Disposition and Content-Type) and the name of a parameter
926
* (e.g. filename, name, charset), returns the value of the parameter.
927
* See nsIMIMEHeaderParam.idl for more information.
928
*
929
* @param aHeaderVal a header string to get the value of a parameter
930
* from.
931
* @param aParamName the name of a MIME header parameter (e.g.
932
* filename, name, charset). If empty or nullptr,
933
* returns the first (possibly) _unnamed_ 'parameter'.
934
* @return the value of <code>aParamName</code> in Unichar(UTF-16).
935
*/
936
nsresult GetParameterHTTP(const nsACString& aHeaderVal, const char* aParamName,
937
nsAString& aResult);
938
939
/**
940
* Convenience functions for verifying nsIURI schemes. These functions simply
941
* wrap aURI->SchemeIs(), but specify the protocol as part of the function name.
942
*/
943
944
bool SchemeIsHTTP(nsIURI* aURI);
945
bool SchemeIsHTTPS(nsIURI* aURI);
946
bool SchemeIsJavascript(nsIURI* aURI);
947
bool SchemeIsChrome(nsIURI* aURI);
948
bool SchemeIsAbout(nsIURI* aURI);
949
bool SchemeIsBlob(nsIURI* aURI);
950
bool SchemeIsFile(nsIURI* aURI);
951
bool SchemeIsData(nsIURI* aURI);
952
bool SchemeIsViewSource(nsIURI* aURI);
953
bool SchemeIsResource(nsIURI* aURI);
954
bool SchemeIsFTP(nsIURI* aURI);
955
} // namespace net
956
} // namespace mozilla
957
958
#endif // !nsNetUtil_h__