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/. */
11 #include "mozilla/Maybe.h"
12 #include "mozilla/ResultExtensions.h"
13 #include "nsAttrValue.h"
15 #include "nsIInterfaceRequestor.h"
16 #include "nsIInterfaceRequestorUtils.h"
17 #include "nsILoadGroup.h"
18 #include "nsINestedURI.h"
19 #include "nsINetUtil.h"
20 #include "nsIRequest.h"
21 #include "nsILoadInfo.h"
22 #include "nsIIOService.h"
24 #include "mozilla/NotNull.h"
25 #include "mozilla/Services.h"
26 #include "mozilla/Unused.h"
28 #include "nsReadableUtils.h"
29 #include "nsServiceManagerUtils.h"
34 class nsIAsyncStreamCopier
;
38 class nsIChannelPolicy
;
39 class nsICookieJarSettings
;
40 class nsIDownloadObserver
;
42 class nsIFileProtocolHandler
;
43 class nsIFileRandomAccessStream
;
46 class nsIInputStreamPump
;
47 class nsIInterfaceRequestor
;
48 class nsIOutputStream
;
49 class nsIParentChannel
;
50 class nsIPersistentProperties
;
52 class nsIRandomAccessStream
;
53 class nsIRequestObserver
;
54 class nsISerialEventTarget
;
55 class nsIStreamListener
;
56 class nsIStreamLoader
;
57 class nsIStreamLoaderObserver
;
58 class nsIIncrementalStreamLoader
;
59 class nsIIncrementalStreamLoaderObserver
;
63 class OriginAttributes
;
67 class PerformanceStorage
;
68 class ServiceWorkerDescriptor
;
75 } // namespace mozilla
80 struct already_AddRefed
;
82 already_AddRefed
<nsIIOService
> do_GetIOService(nsresult
* error
= nullptr);
84 already_AddRefed
<nsINetUtil
> do_GetNetUtil(nsresult
* error
= nullptr);
86 // private little helper function... don't call this directly!
87 nsresult
net_EnsureIOService(nsIIOService
** ios
, nsCOMPtr
<nsIIOService
>& grip
);
89 nsresult
NS_NewURI(nsIURI
** aURI
, const nsACString
& spec
,
90 const char* charset
= nullptr, nsIURI
* baseURI
= nullptr);
92 nsresult
NS_NewURI(nsIURI
** result
, const nsACString
& spec
,
93 mozilla::NotNull
<const mozilla::Encoding
*> encoding
,
94 nsIURI
* baseURI
= nullptr);
96 nsresult
NS_NewURI(nsIURI
** result
, const nsAString
& spec
,
97 const char* charset
= nullptr, nsIURI
* baseURI
= nullptr);
99 nsresult
NS_NewURI(nsIURI
** result
, const nsAString
& spec
,
100 mozilla::NotNull
<const mozilla::Encoding
*> encoding
,
101 nsIURI
* baseURI
= nullptr);
103 nsresult
NS_NewURI(nsIURI
** result
, const char* spec
,
104 nsIURI
* baseURI
= nullptr);
106 nsresult
NS_NewFileURI(
107 nsIURI
** result
, nsIFile
* spec
,
108 nsIIOService
* ioService
=
109 nullptr); // pass in nsIIOService to optimize callers
111 // Functions for adding additional encoding to a URL for compatibility with
112 // Apple's NSURL class URLWithString method.
115 // Out parameter for the encoded URL spec
117 // The spec for the URL to be encoded
118 nsresult
NS_GetSpecWithNSURLEncoding(nsACString
& aResult
,
119 const nsACString
& aSpec
);
121 // Out parameter for the encoded URI
123 // The spec for the URL to be encoded
124 nsresult
NS_NewURIWithNSURLEncoding(nsIURI
** aResult
, const nsACString
& aSpec
);
126 // These methods will only mutate the URI if the ref of aInput doesn't already
127 // match the ref we are trying to set.
128 // If aInput has no ref, and we are calling NS_GetURIWithoutRef, or
129 // NS_GetURIWithNewRef with an empty string, then aOutput will be the same
130 // as aInput. The same is true if aRef is already equal to the ref of aInput.
131 // This is OK because URIs are immutable and threadsafe.
132 // If the URI doesn't support ref fragments aOutput will be the same as aInput.
133 nsresult
NS_GetURIWithNewRef(nsIURI
* aInput
, const nsACString
& aRef
,
135 nsresult
NS_GetURIWithoutRef(nsIURI
* aInput
, nsIURI
** aOutput
);
137 nsresult
NS_GetSanitizedURIStringFromURI(nsIURI
* aUri
,
138 nsAString
& aSanitizedSpec
);
141 * How to create a new Channel, using NS_NewChannel,
142 * NS_NewChannelWithTriggeringPrincipal,
143 * NS_NewInputStreamChannel, NS_NewChannelInternal
144 * and it's variations:
146 * What specific API function to use:
147 * * The NS_NewChannelInternal functions should almost never be directly
148 * called outside of necko code.
149 * * If possible, use NS_NewChannel() providing a loading *nsINode*
150 * * If no loading *nsINode* is available, try calling NS_NewChannel() providing
151 * a loading *ClientInfo*.
152 * * If no loading *nsINode* or *ClientInfo* are available, call NS_NewChannel()
153 * providing a loading *nsIPrincipal*.
154 * * Call NS_NewChannelWithTriggeringPrincipal if the triggeringPrincipal
155 * is different from the loadingPrincipal.
156 * * Call NS_NewChannelInternal() providing aLoadInfo object in cases where
157 * you already have loadInfo object, e.g in case of a channel redirect.
160 * nsIURI from which to make a channel
161 * @param aLoadingNode
162 * @param aLoadingPrincipal
163 * @param aTriggeringPrincipal
164 * @param aSecurityFlags
165 * @param aContentPolicyType
166 * These will be used as values for the nsILoadInfo object on the
167 * created channel. For details, see nsILoadInfo in nsILoadInfo.idl
169 * Please note, if you provide both a loadingNode and a loadingPrincipal,
170 * then loadingPrincipal must be equal to loadingNode->NodePrincipal().
171 * But less error prone is to just supply a loadingNode.
173 * Note, if you provide a loading ClientInfo its principal must match the
174 * loading principal. Currently you must pass both as the loading principal
175 * may have additional mutable values like CSP on it. In the future these
176 * will be removed from nsIPrincipal and the API can be changed to take just
177 * the loading ClientInfo.
179 * Keep in mind that URIs coming from a webpage should *never* use the
180 * systemPrincipal as the loadingPrincipal.
182 nsresult
NS_NewChannelInternal(
183 nsIChannel
** outChannel
, nsIURI
* aUri
, nsINode
* aLoadingNode
,
184 nsIPrincipal
* aLoadingPrincipal
, nsIPrincipal
* aTriggeringPrincipal
,
185 const mozilla::Maybe
<mozilla::dom::ClientInfo
>& aLoadingClientInfo
,
186 const mozilla::Maybe
<mozilla::dom::ServiceWorkerDescriptor
>& aController
,
187 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
188 nsICookieJarSettings
* aCookieJarSettings
= nullptr,
189 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
190 nsILoadGroup
* aLoadGroup
= nullptr,
191 nsIInterfaceRequestor
* aCallbacks
= nullptr,
192 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
193 nsIIOService
* aIoService
= nullptr, uint32_t aSandboxFlags
= 0,
194 bool aSkipCheckForBrokenURLOrZeroSized
= false);
196 // See NS_NewChannelInternal for usage and argument description
197 nsresult
NS_NewChannelInternal(
198 nsIChannel
** outChannel
, nsIURI
* aUri
, nsILoadInfo
* aLoadInfo
,
199 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
200 nsILoadGroup
* aLoadGroup
= nullptr,
201 nsIInterfaceRequestor
* aCallbacks
= nullptr,
202 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
203 nsIIOService
* aIoService
= nullptr);
205 // See NS_NewChannelInternal for usage and argument description
206 nsresult
/*NS_NewChannelWithNodeAndTriggeringPrincipal */
207 NS_NewChannelWithTriggeringPrincipal(
208 nsIChannel
** outChannel
, nsIURI
* aUri
, nsINode
* aLoadingNode
,
209 nsIPrincipal
* aTriggeringPrincipal
, nsSecurityFlags aSecurityFlags
,
210 nsContentPolicyType aContentPolicyType
,
211 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
212 nsILoadGroup
* aLoadGroup
= nullptr,
213 nsIInterfaceRequestor
* aCallbacks
= nullptr,
214 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
215 nsIIOService
* aIoService
= nullptr);
217 // See NS_NewChannelInternal for usage and argument description
218 nsresult
NS_NewChannelWithTriggeringPrincipal(
219 nsIChannel
** outChannel
, nsIURI
* aUri
, nsIPrincipal
* aLoadingPrincipal
,
220 nsIPrincipal
* aTriggeringPrincipal
, nsSecurityFlags aSecurityFlags
,
221 nsContentPolicyType aContentPolicyType
,
222 nsICookieJarSettings
* aCookieJarSettings
= nullptr,
223 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
224 nsILoadGroup
* aLoadGroup
= nullptr,
225 nsIInterfaceRequestor
* aCallbacks
= nullptr,
226 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
227 nsIIOService
* aIoService
= nullptr);
229 // See NS_NewChannelInternal for usage and argument description
230 nsresult
NS_NewChannelWithTriggeringPrincipal(
231 nsIChannel
** outChannel
, nsIURI
* aUri
, nsIPrincipal
* aLoadingPrincipal
,
232 nsIPrincipal
* aTriggeringPrincipal
,
233 const mozilla::dom::ClientInfo
& aLoadingClientInfo
,
234 const mozilla::Maybe
<mozilla::dom::ServiceWorkerDescriptor
>& aController
,
235 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
236 nsICookieJarSettings
* aCookieJarSettings
= nullptr,
237 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
238 nsILoadGroup
* aLoadGroup
= nullptr,
239 nsIInterfaceRequestor
* aCallbacks
= nullptr,
240 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
241 nsIIOService
* aIoService
= nullptr);
243 // See NS_NewChannelInternal for usage and argument description
244 nsresult
NS_NewChannel(
245 nsIChannel
** outChannel
, nsIURI
* aUri
, nsINode
* aLoadingNode
,
246 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
247 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
248 nsILoadGroup
* aLoadGroup
= nullptr,
249 nsIInterfaceRequestor
* aCallbacks
= nullptr,
250 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
251 nsIIOService
* aIoService
= nullptr, uint32_t aSandboxFlags
= 0,
252 bool aSkipCheckForBrokenURLOrZeroSized
= false);
254 // See NS_NewChannelInternal for usage and argument description
255 nsresult
NS_NewChannel(
256 nsIChannel
** outChannel
, nsIURI
* aUri
, nsIPrincipal
* aLoadingPrincipal
,
257 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
258 nsICookieJarSettings
* aCookieJarSettings
= nullptr,
259 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
260 nsILoadGroup
* aLoadGroup
= nullptr,
261 nsIInterfaceRequestor
* aCallbacks
= nullptr,
262 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
263 nsIIOService
* aIoService
= nullptr, uint32_t aSandboxFlags
= 0,
264 bool aSkipCheckForBrokenURLOrZeroSized
= false);
266 // See NS_NewChannelInternal for usage and argument description
267 nsresult
NS_NewChannel(
268 nsIChannel
** outChannel
, nsIURI
* aUri
, nsIPrincipal
* aLoadingPrincipal
,
269 const mozilla::dom::ClientInfo
& aLoadingClientInfo
,
270 const mozilla::Maybe
<mozilla::dom::ServiceWorkerDescriptor
>& aController
,
271 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
272 nsICookieJarSettings
* aCookieJarSettings
= nullptr,
273 mozilla::dom::PerformanceStorage
* aPerformanceStorage
= nullptr,
274 nsILoadGroup
* aLoadGroup
= nullptr,
275 nsIInterfaceRequestor
* aCallbacks
= nullptr,
276 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
,
277 nsIIOService
* aIoService
= nullptr, uint32_t aSandboxFlags
= 0,
278 bool aSkipCheckForBrokenURLOrZeroSized
= false);
280 nsresult
NS_GetIsDocumentChannel(nsIChannel
* aChannel
, bool* aIsDocument
);
282 nsresult
NS_MakeAbsoluteURI(nsACString
& result
, const nsACString
& spec
,
285 nsresult
NS_MakeAbsoluteURI(char** result
, const char* spec
, nsIURI
* baseURI
);
287 nsresult
NS_MakeAbsoluteURI(nsAString
& result
, const nsAString
& spec
,
291 * This function is a helper function to get a scheme's default port.
293 int32_t NS_GetDefaultPort(const char* scheme
,
294 nsIIOService
* ioService
= nullptr);
297 * This function is a helper function to apply the ToAscii conversion
300 bool NS_StringToACE(const nsACString
& idn
, nsACString
& result
);
303 * This function is a helper function to get a protocol's default port if the
304 * URI does not specify a port explicitly. Returns -1 if this protocol has no
305 * concept of ports or if there was an error getting the port.
307 int32_t NS_GetRealPort(nsIURI
* aURI
);
309 nsresult
NS_NewInputStreamChannelInternal(
310 nsIChannel
** outChannel
, nsIURI
* aUri
,
311 already_AddRefed
<nsIInputStream
> aStream
, const nsACString
& aContentType
,
312 const nsACString
& aContentCharset
, nsILoadInfo
* aLoadInfo
);
314 nsresult
NS_NewInputStreamChannelInternal(
315 nsIChannel
** outChannel
, nsIURI
* aUri
,
316 already_AddRefed
<nsIInputStream
> aStream
, const nsACString
& aContentType
,
317 const nsACString
& aContentCharset
, nsINode
* aLoadingNode
,
318 nsIPrincipal
* aLoadingPrincipal
, nsIPrincipal
* aTriggeringPrincipal
,
319 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
);
321 nsresult
NS_NewInputStreamChannel(nsIChannel
** outChannel
, nsIURI
* aUri
,
322 already_AddRefed
<nsIInputStream
> aStream
,
323 nsIPrincipal
* aLoadingPrincipal
,
324 nsSecurityFlags aSecurityFlags
,
325 nsContentPolicyType aContentPolicyType
,
326 const nsACString
& aContentType
= ""_ns
,
327 const nsACString
& aContentCharset
= ""_ns
);
329 nsresult
NS_NewInputStreamChannelInternal(
330 nsIChannel
** outChannel
, nsIURI
* aUri
, const nsAString
& aData
,
331 const nsACString
& aContentType
, nsINode
* aLoadingNode
,
332 nsIPrincipal
* aLoadingPrincipal
, nsIPrincipal
* aTriggeringPrincipal
,
333 nsSecurityFlags aSecurityFlags
, nsContentPolicyType aContentPolicyType
,
334 bool aIsSrcdocChannel
= false);
336 nsresult
NS_NewInputStreamChannelInternal(nsIChannel
** outChannel
, nsIURI
* aUri
,
337 const nsAString
& aData
,
338 const nsACString
& aContentType
,
339 nsILoadInfo
* aLoadInfo
,
340 bool aIsSrcdocChannel
= false);
342 nsresult
NS_NewInputStreamChannel(nsIChannel
** outChannel
, nsIURI
* aUri
,
343 const nsAString
& aData
,
344 const nsACString
& aContentType
,
345 nsIPrincipal
* aLoadingPrincipal
,
346 nsSecurityFlags aSecurityFlags
,
347 nsContentPolicyType aContentPolicyType
,
348 bool aIsSrcdocChannel
= false);
350 nsresult
NS_NewInputStreamPump(
351 nsIInputStreamPump
** aResult
, already_AddRefed
<nsIInputStream
> aStream
,
352 uint32_t aSegsize
= 0, uint32_t aSegcount
= 0, bool aCloseWhenDone
= false,
353 nsISerialEventTarget
* aMainThreadTarget
= nullptr);
355 nsresult
NS_NewLoadGroup(nsILoadGroup
** result
, nsIRequestObserver
* obs
);
357 // Create a new nsILoadGroup that will match the given principal.
358 nsresult
NS_NewLoadGroup(nsILoadGroup
** aResult
, nsIPrincipal
* aPrincipal
);
360 // Determine if the given loadGroup/principal pair will produce a principal
361 // with similar permissions when passed to NS_NewChannel(). This checks for
362 // things like making sure the browser element flag matches. Without
363 // an appropriate load group these values can be lost when getting the result
364 // principal back out of the channel. Null principals are also always allowed
365 // as they do not have permissions to actually use the load group.
366 bool NS_LoadGroupMatchesPrincipal(nsILoadGroup
* aLoadGroup
,
367 nsIPrincipal
* aPrincipal
);
369 nsresult
NS_NewDownloader(nsIStreamListener
** result
,
370 nsIDownloadObserver
* observer
,
371 nsIFile
* downloadLocation
= nullptr);
373 nsresult
NS_NewStreamLoader(nsIStreamLoader
** result
,
374 nsIStreamLoaderObserver
* observer
,
375 nsIRequestObserver
* requestObserver
= nullptr);
377 nsresult
NS_NewIncrementalStreamLoader(
378 nsIIncrementalStreamLoader
** result
,
379 nsIIncrementalStreamLoaderObserver
* observer
);
381 nsresult
NS_NewStreamLoaderInternal(
382 nsIStreamLoader
** outStream
, nsIURI
* aUri
,
383 nsIStreamLoaderObserver
* aObserver
, nsINode
* aLoadingNode
,
384 nsIPrincipal
* aLoadingPrincipal
, nsSecurityFlags aSecurityFlags
,
385 nsContentPolicyType aContentPolicyType
, nsILoadGroup
* aLoadGroup
= nullptr,
386 nsIInterfaceRequestor
* aCallbacks
= nullptr,
387 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
);
389 nsresult
NS_NewStreamLoader(nsIStreamLoader
** outStream
, nsIURI
* aUri
,
390 nsIStreamLoaderObserver
* aObserver
,
391 nsINode
* aLoadingNode
,
392 nsSecurityFlags aSecurityFlags
,
393 nsContentPolicyType aContentPolicyType
,
394 nsILoadGroup
* aLoadGroup
= nullptr,
395 nsIInterfaceRequestor
* aCallbacks
= nullptr,
396 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
);
398 nsresult
NS_NewStreamLoader(nsIStreamLoader
** outStream
, nsIURI
* aUri
,
399 nsIStreamLoaderObserver
* aObserver
,
400 nsIPrincipal
* aLoadingPrincipal
,
401 nsSecurityFlags aSecurityFlags
,
402 nsContentPolicyType aContentPolicyType
,
403 nsILoadGroup
* aLoadGroup
= nullptr,
404 nsIInterfaceRequestor
* aCallbacks
= nullptr,
405 nsLoadFlags aLoadFlags
= nsIRequest::LOAD_NORMAL
);
407 nsresult
NS_NewSyncStreamListener(nsIStreamListener
** result
,
408 nsIInputStream
** stream
);
411 * Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
414 * NOTE: Reading from the returned nsIInputStream may spin the current
415 * thread's event queue, which could result in any event being processed.
417 nsresult
NS_ImplementChannelOpen(nsIChannel
* channel
, nsIInputStream
** result
);
419 nsresult
NS_NewRequestObserverProxy(nsIRequestObserver
** result
,
420 nsIRequestObserver
* observer
,
421 nsISupports
* context
);
423 nsresult
NS_NewSimpleStreamListener(nsIStreamListener
** result
,
424 nsIOutputStream
* sink
,
425 nsIRequestObserver
* observer
= nullptr);
427 nsresult
NS_CheckPortSafety(int32_t port
, const char* scheme
,
428 nsIIOService
* ioService
= nullptr);
430 // Determine if this URI is using a safe port.
431 nsresult
NS_CheckPortSafety(nsIURI
* uri
);
433 nsresult
NS_NewProxyInfo(const nsACString
& type
, const nsACString
& host
,
434 int32_t port
, uint32_t flags
, nsIProxyInfo
** result
);
436 nsresult
NS_GetFileProtocolHandler(nsIFileProtocolHandler
** result
,
437 nsIIOService
* ioService
= nullptr);
439 nsresult
NS_GetFileFromURLSpec(const nsACString
& inURL
, nsIFile
** result
,
440 nsIIOService
* ioService
= nullptr);
442 nsresult
NS_GetURLSpecFromFile(nsIFile
* file
, nsACString
& url
,
443 nsIIOService
* ioService
= nullptr);
446 * Converts the nsIFile to the corresponding URL string.
447 * Should only be called on files which are not directories,
448 * is otherwise identical to NS_GetURLSpecFromFile, but is
449 * usually more efficient.
450 * Warning: this restriction may not be enforced at runtime!
452 nsresult
NS_GetURLSpecFromActualFile(nsIFile
* file
, nsACString
& url
,
453 nsIIOService
* ioService
= nullptr);
456 * Converts the nsIFile to the corresponding URL string.
457 * Should only be called on files which are directories,
458 * is otherwise identical to NS_GetURLSpecFromFile, but is
459 * usually more efficient.
460 * Warning: this restriction may not be enforced at runtime!
462 nsresult
NS_GetURLSpecFromDir(nsIFile
* file
, nsACString
& url
,
463 nsIIOService
* ioService
= nullptr);
466 * Obtains the referrer for a given channel. This first tries to obtain the
467 * referrer from the property docshell.internalReferrer, and if that doesn't
468 * work and the channel is an nsIHTTPChannel, we check it's referrer property.
471 void NS_GetReferrerFromChannel(nsIChannel
* channel
, nsIURI
** referrer
);
473 nsresult
NS_ParseRequestContentType(const nsACString
& rawContentType
,
474 nsCString
& contentType
,
475 nsCString
& contentCharset
);
477 nsresult
NS_ParseResponseContentType(const nsACString
& rawContentType
,
478 nsCString
& contentType
,
479 nsCString
& contentCharset
);
481 nsresult
NS_ExtractCharsetFromContentType(const nsACString
& rawContentType
,
482 nsCString
& contentCharset
,
484 int32_t* charsetStart
,
485 int32_t* charsetEnd
);
487 nsresult
NS_NewLocalFileInputStream(nsIInputStream
** result
, nsIFile
* file
,
488 int32_t ioFlags
= -1, int32_t perm
= -1,
489 int32_t behaviorFlags
= 0);
491 mozilla::Result
<nsCOMPtr
<nsIInputStream
>, nsresult
> NS_NewLocalFileInputStream(
492 nsIFile
* file
, int32_t ioFlags
= -1, int32_t perm
= -1,
493 int32_t behaviorFlags
= 0);
495 nsresult
NS_NewLocalFileOutputStream(nsIOutputStream
** result
, nsIFile
* file
,
496 int32_t ioFlags
= -1, int32_t perm
= -1,
497 int32_t behaviorFlags
= 0);
499 mozilla::Result
<nsCOMPtr
<nsIOutputStream
>, nsresult
>
500 NS_NewLocalFileOutputStream(nsIFile
* file
, int32_t ioFlags
= -1,
501 int32_t perm
= -1, int32_t behaviorFlags
= 0);
503 nsresult
NS_NewLocalFileOutputStream(nsIOutputStream
** result
,
504 const mozilla::ipc::FileDescriptor
& fd
);
506 // returns a file output stream which can be QI'ed to nsISafeOutputStream.
507 nsresult
NS_NewAtomicFileOutputStream(nsIOutputStream
** result
, nsIFile
* file
,
508 int32_t ioFlags
= -1, int32_t perm
= -1,
509 int32_t behaviorFlags
= 0);
511 // returns a file output stream which can be QI'ed to nsISafeOutputStream.
512 nsresult
NS_NewSafeLocalFileOutputStream(nsIOutputStream
** result
,
513 nsIFile
* file
, int32_t ioFlags
= -1,
515 int32_t behaviorFlags
= 0);
517 nsresult
NS_NewLocalFileRandomAccessStream(nsIRandomAccessStream
** result
,
518 nsIFile
* file
, int32_t ioFlags
= -1,
520 int32_t behaviorFlags
= 0);
522 mozilla::Result
<nsCOMPtr
<nsIRandomAccessStream
>, nsresult
>
523 NS_NewLocalFileRandomAccessStream(nsIFile
* file
, int32_t ioFlags
= -1,
524 int32_t perm
= -1, int32_t behaviorFlags
= 0);
526 [[nodiscard
]] nsresult
NS_NewBufferedInputStream(
527 nsIInputStream
** aResult
, already_AddRefed
<nsIInputStream
> aInputStream
,
528 uint32_t aBufferSize
);
530 mozilla::Result
<nsCOMPtr
<nsIInputStream
>, nsresult
> NS_NewBufferedInputStream(
531 already_AddRefed
<nsIInputStream
> aInputStream
, uint32_t aBufferSize
);
533 // note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
534 // provided stream supports it.
535 nsresult
NS_NewBufferedOutputStream(
536 nsIOutputStream
** aResult
, already_AddRefed
<nsIOutputStream
> aOutputStream
,
537 uint32_t aBufferSize
);
540 * This function reads an inputStream and stores its content into a buffer. In
541 * general, you should avoid using this function because, it blocks the current
542 * thread until the operation is done.
543 * If the inputStream is async, the reading happens on an I/O thread.
545 * @param aInputStream the inputStream.
546 * @param aDest the destination buffer. if *aDest is null, it will be allocated
547 * with the size of the written data. if aDest is not null, aCount
548 * must greater than 0.
549 * @param aCount the amount of data to read. Use -1 if you want that all the
551 * @param aWritten this pointer will be used to store the number of data
552 * written in the buffer. If you don't need, pass nullptr.
554 nsresult
NS_ReadInputStreamToBuffer(nsIInputStream
* aInputStream
, void** aDest
,
556 uint64_t* aWritten
= nullptr);
559 * See the comment for NS_ReadInputStreamToBuffer
561 nsresult
NS_ReadInputStreamToString(nsIInputStream
* aInputStream
,
562 nsACString
& aDest
, int64_t aCount
,
563 uint64_t* aWritten
= nullptr);
565 nsresult
NS_LoadPersistentPropertiesFromURISpec(
566 nsIPersistentProperties
** outResult
, const nsACString
& aSpec
);
569 * NS_QueryNotificationCallbacks implements the canonical algorithm for
570 * querying interfaces from a channel's notification callbacks. It first
571 * searches the channel's notificationCallbacks attribute, and if the interface
572 * is not found there, then it inspects the notificationCallbacks attribute of
573 * the channel's loadGroup.
575 * Note: templatized only because nsIWebSocketChannel is currently not an
579 inline void NS_QueryNotificationCallbacks(T
* channel
, const nsIID
& iid
,
581 MOZ_ASSERT(channel
, "null channel");
584 nsCOMPtr
<nsIInterfaceRequestor
> cbs
;
585 mozilla::Unused
<< channel
->GetNotificationCallbacks(getter_AddRefs(cbs
));
586 if (cbs
) cbs
->GetInterface(iid
, result
);
588 // try load group's notification callbacks...
589 nsCOMPtr
<nsILoadGroup
> loadGroup
;
590 mozilla::Unused
<< channel
->GetLoadGroup(getter_AddRefs(loadGroup
));
592 loadGroup
->GetNotificationCallbacks(getter_AddRefs(cbs
));
593 if (cbs
) cbs
->GetInterface(iid
, result
);
599 // Note: "class C" templatized only because nsIWebSocketChannel is currently not
602 template <class C
, class T
>
603 inline void NS_QueryNotificationCallbacks(C
* channel
, nsCOMPtr
<T
>& result
) {
604 NS_QueryNotificationCallbacks(channel
, NS_GET_TEMPLATE_IID(T
),
605 getter_AddRefs(result
));
609 * Alternate form of NS_QueryNotificationCallbacks designed for use by
610 * nsIChannel implementations.
612 inline void NS_QueryNotificationCallbacks(nsIInterfaceRequestor
* callbacks
,
613 nsILoadGroup
* loadGroup
,
614 const nsIID
& iid
, void** result
) {
617 if (callbacks
) callbacks
->GetInterface(iid
, result
);
619 // try load group's notification callbacks...
621 nsCOMPtr
<nsIInterfaceRequestor
> cbs
;
622 loadGroup
->GetNotificationCallbacks(getter_AddRefs(cbs
));
623 if (cbs
) cbs
->GetInterface(iid
, result
);
629 * Returns true if channel is using Private Browsing, or false if not.
630 * Returns false if channel's callbacks don't implement nsILoadContext.
632 bool NS_UsePrivateBrowsing(nsIChannel
* channel
);
635 * Returns true if the channel has visited any cross-origin URLs on any
636 * URLs that it was redirected through.
638 bool NS_HasBeenCrossOrigin(nsIChannel
* aChannel
, bool aReport
= false);
641 * Returns true if the channel has a safe method.
643 bool NS_IsSafeMethodNav(nsIChannel
* aChannel
);
645 // Unique first-party domain for separating the safebrowsing cookie.
646 // Note if this value is changed, code in test_cookiejars_safebrowsing.js and
647 // nsUrlClassifierHashCompleter.js should also be changed.
648 #define NECKO_SAFEBROWSING_FIRST_PARTY_DOMAIN \
649 "safebrowsing.86868755-6b82-4842-b301-72671a0db32e.mozilla"
651 // Unique first-party domain for separating about uri.
652 #define ABOUT_URI_FIRST_PARTY_DOMAIN \
653 "about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla"
656 * Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
657 * method is provided mainly for use by other methods in this file.
659 * *aAuthPrompt2 should be set to null before calling this function.
661 void NS_WrapAuthPrompt(nsIAuthPrompt
* aAuthPrompt
,
662 nsIAuthPrompt2
** aAuthPrompt2
);
665 * Gets an auth prompt from an interface requestor. This takes care of wrapping
666 * an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2.
668 void NS_QueryAuthPrompt2(nsIInterfaceRequestor
* aCallbacks
,
669 nsIAuthPrompt2
** aAuthPrompt
);
672 * Gets an nsIAuthPrompt2 from a channel. Use this instead of
673 * NS_QueryNotificationCallbacks for better backwards compatibility.
675 void NS_QueryAuthPrompt2(nsIChannel
* aChannel
, nsIAuthPrompt2
** aAuthPrompt
);
677 /* template helper */
679 inline void NS_QueryNotificationCallbacks(nsIInterfaceRequestor
* callbacks
,
680 nsILoadGroup
* loadGroup
,
681 nsCOMPtr
<T
>& result
) {
682 NS_QueryNotificationCallbacks(callbacks
, loadGroup
, NS_GET_TEMPLATE_IID(T
),
683 getter_AddRefs(result
));
686 /* template helper */
688 inline void NS_QueryNotificationCallbacks(
689 const nsCOMPtr
<nsIInterfaceRequestor
>& aCallbacks
,
690 const nsCOMPtr
<nsILoadGroup
>& aLoadGroup
, nsCOMPtr
<T
>& aResult
) {
691 NS_QueryNotificationCallbacks(aCallbacks
.get(), aLoadGroup
.get(), aResult
);
694 /* template helper */
696 inline void NS_QueryNotificationCallbacks(const nsCOMPtr
<nsIChannel
>& aChannel
,
697 nsCOMPtr
<T
>& aResult
) {
698 NS_QueryNotificationCallbacks(aChannel
.get(), aResult
);
702 * This function returns a nsIInterfaceRequestor instance that returns the
703 * same result as NS_QueryNotificationCallbacks when queried. It is useful
704 * as the value for nsISocketTransport::securityCallbacks.
706 nsresult
NS_NewNotificationCallbacksAggregation(
707 nsIInterfaceRequestor
* callbacks
, nsILoadGroup
* loadGroup
,
708 nsIEventTarget
* target
, nsIInterfaceRequestor
** result
);
710 nsresult
NS_NewNotificationCallbacksAggregation(
711 nsIInterfaceRequestor
* callbacks
, nsILoadGroup
* loadGroup
,
712 nsIInterfaceRequestor
** result
);
715 * Helper function for testing online/offline state of the browser.
720 * Helper functions for implementing nsINestedURI::innermostURI.
722 * Note that NS_DoImplGetInnermostURI is "private" -- call
723 * NS_ImplGetInnermostURI instead.
725 nsresult
NS_DoImplGetInnermostURI(nsINestedURI
* nestedURI
, nsIURI
** result
);
727 nsresult
NS_ImplGetInnermostURI(nsINestedURI
* nestedURI
, nsIURI
** result
);
730 * Helper function for testing whether the given URI, or any of its
731 * inner URIs, has all the given protocol flags.
733 nsresult
NS_URIChainHasFlags(nsIURI
* uri
, uint32_t flags
, bool* result
);
736 * Helper function for getting the innermost URI for a given URI. The return
737 * value could be just the object passed in if it's not a nested URI.
739 already_AddRefed
<nsIURI
> NS_GetInnermostURI(nsIURI
* aURI
);
742 * Helper function for getting the host name of the innermost URI for a given
743 * URI. The return value could be the host name of the URI passed in if it's
746 inline nsresult
NS_GetInnermostURIHost(nsIURI
* aURI
, nsACString
& aHost
) {
749 // This block is optimized in order to avoid the overhead of calling
750 // NS_GetInnermostURI() which incurs a lot of overhead in terms of
751 // AddRef/Release calls.
752 nsCOMPtr
<nsINestedURI
> nestedURI
= do_QueryInterface(aURI
);
754 // We have a nested URI!
755 nsCOMPtr
<nsIURI
> uri
;
756 nsresult rv
= nestedURI
->GetInnermostURI(getter_AddRefs(uri
));
761 rv
= uri
->GetAsciiHost(aHost
);
766 // We have a non-nested URI!
767 nsresult rv
= aURI
->GetAsciiHost(aHost
);
777 * Get the "final" URI for a channel. This is either channel's load info
778 * resultPrincipalURI, if set, or GetOriginalURI. In most cases (but not all)
779 * load info resultPrincipalURI, if set, corresponds to URI of the channel if
780 * it's required to represent the actual principal for the channel.
782 nsresult
NS_GetFinalChannelURI(nsIChannel
* channel
, nsIURI
** uri
);
784 // NS_SecurityHashURI must return the same hash value for any two URIs that
785 // compare equal according to NS_SecurityCompareURIs. Unfortunately, in the
786 // case of files, it's not clear we can do anything better than returning
787 // the schemeHash, so hashing files degenerates to storing them in a list.
788 uint32_t NS_SecurityHashURI(nsIURI
* aURI
);
790 bool NS_SecurityCompareURIs(nsIURI
* aSourceURI
, nsIURI
* aTargetURI
,
791 bool aStrictFileOriginPolicy
);
793 bool NS_URIIsLocalFile(nsIURI
* aURI
);
795 // When strict file origin policy is enabled, SecurityCompareURIs will fail for
796 // file URIs that do not point to the same local file. This call provides an
797 // alternate file-specific origin check that allows target files that are
798 // contained in the same directory as the source.
800 // https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs
801 bool NS_RelaxStrictFileOriginPolicy(nsIURI
* aTargetURI
, nsIURI
* aSourceURI
,
802 bool aAllowDirectoryTarget
= false);
804 bool NS_IsInternalSameURIRedirect(nsIChannel
* aOldChannel
,
805 nsIChannel
* aNewChannel
, uint32_t aFlags
);
807 bool NS_IsHSTSUpgradeRedirect(nsIChannel
* aOldChannel
, nsIChannel
* aNewChannel
,
810 bool NS_ShouldRemoveAuthHeaderOnRedirect(nsIChannel
* aOldChannel
,
811 nsIChannel
* aNewChannel
,
814 nsresult
NS_LinkRedirectChannels(uint64_t channelId
,
815 nsIParentChannel
* parentChannel
,
816 nsIChannel
** _result
);
819 * Returns nsILoadInfo::EMBEDDER_POLICY_REQUIRE_CORP if `aHeader` is
820 * "require-corp" and nsILoadInfo::EMBEDDER_POLICY_NULL otherwise.
822 * See: https://mikewest.github.io/corpp/#parsing
824 nsILoadInfo::CrossOriginEmbedderPolicy
825 NS_GetCrossOriginEmbedderPolicyFromHeader(
826 const nsACString
& aHeader
, bool aIsOriginTrialCoepCredentiallessEnabled
);
828 /** Given the first (disposition) token from a Content-Disposition header,
829 * tell whether it indicates the content is inline or attachment
830 * @param aDispToken the disposition token from the content-disposition header
832 uint32_t NS_GetContentDispositionFromToken(const nsAString
& aDispToken
);
834 /** Determine the disposition (inline/attachment) of the content based on the
835 * Content-Disposition header
836 * @param aHeader the content-disposition header (full value)
837 * @param aChan the channel the header came from
839 uint32_t NS_GetContentDispositionFromHeader(const nsACString
& aHeader
,
840 nsIChannel
* aChan
= nullptr);
842 /** Extracts the filename out of a content-disposition header
843 * @param aFilename [out] The filename. Can be empty on error.
844 * @param aDisposition Value of a Content-Disposition header
847 nsresult
NS_GetFilenameFromDisposition(nsAString
& aFilename
,
848 const nsACString
& aDisposition
);
851 * Make sure Personal Security Manager is initialized
853 void net_EnsurePSMInit();
856 * Test whether a URI is "about:blank". |uri| must not be null
858 bool NS_IsAboutBlank(nsIURI
* uri
);
861 * Test whether a URI is "about:srcdoc". |uri| must not be null
863 bool NS_IsAboutSrcdoc(nsIURI
* uri
);
865 nsresult
NS_GenerateHostPort(const nsCString
& host
, int32_t port
,
866 nsACString
& hostLine
);
869 * Sniff the content type for a given request or a given buffer.
871 * aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or
872 * NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type
873 * in the aSniffedType argument. This argument will not be modified if the
874 * content type could not be sniffed.
876 void NS_SniffContent(const char* aSnifferType
, nsIRequest
* aRequest
,
877 const uint8_t* aData
, uint32_t aLength
,
878 nsACString
& aSniffedType
);
881 * Whether the channel was created to load a srcdoc document.
882 * Note that view-source:about:srcdoc is classified as a srcdoc document by
883 * this function, which may not be applicable everywhere.
885 bool NS_IsSrcdocChannel(nsIChannel
* aChannel
);
888 * Return true if the given string is a reasonable HTTP header value given the
889 * definition in RFC 2616 section 4.2. Currently we don't pay the cost to do
890 * full, sctrict validation here since it would require fulling parsing the
893 bool NS_IsReasonableHTTPHeaderValue(const nsACString
& aValue
);
896 * Return true if the given string is a valid HTTP token per RFC 2616 section
899 bool NS_IsValidHTTPToken(const nsACString
& aToken
);
902 * Strip the leading or trailing HTTP whitespace per fetch spec section 2.2.
904 void NS_TrimHTTPWhitespace(const nsACString
& aSource
, nsACString
& aDest
);
906 template <typename Char
>
907 constexpr bool NS_IsHTTPTokenPoint(Char aChar
) {
908 using UnsignedChar
= typename
mozilla::detail::MakeUnsignedChar
<Char
>::Type
;
909 auto c
= static_cast<UnsignedChar
>(aChar
);
910 return c
== '!' || c
== '#' || c
== '$' || c
== '%' || c
== '&' ||
911 c
== '\'' || c
== '*' || c
== '+' || c
== '-' || c
== '.' ||
912 c
== '^' || c
== '_' || c
== '`' || c
== '|' || c
== '~' ||
913 mozilla::IsAsciiAlphanumeric(c
);
916 template <typename Char
>
917 constexpr bool NS_IsHTTPQuotedStringTokenPoint(Char aChar
) {
918 using UnsignedChar
= typename
mozilla::detail::MakeUnsignedChar
<Char
>::Type
;
919 auto c
= static_cast<UnsignedChar
>(aChar
);
920 return c
== 0x9 || (c
>= ' ' && c
<= '~') || mozilla::IsNonAsciiLatin1(c
);
923 template <typename Char
>
924 constexpr bool NS_IsHTTPWhitespace(Char aChar
) {
925 using UnsignedChar
= typename
mozilla::detail::MakeUnsignedChar
<Char
>::Type
;
926 auto c
= static_cast<UnsignedChar
>(aChar
);
927 return c
== 0x9 || c
== 0xA || c
== 0xD || c
== 0x20;
931 * Return true if the given request must be upgraded to HTTPS.
932 * If |aResultCallback| is provided and the storage is not ready to read, the
933 * result will be sent back through the callback and |aWillCallback| will be
934 * true. Otherwiew, the result will be set to |aShouldUpgrade| and
935 * |aWillCallback| is false.
937 nsresult
NS_ShouldSecureUpgrade(
938 nsIURI
* aURI
, nsILoadInfo
* aLoadInfo
, nsIPrincipal
* aChannelResultPrincipal
,
939 bool aAllowSTS
, const mozilla::OriginAttributes
& aOriginAttributes
,
940 bool& aShouldUpgrade
, std::function
<void(bool, nsresult
)>&& aResultCallback
,
941 bool& aWillCallback
);
944 * Returns an https URI for channels that need to go through secure upgrades.
946 nsresult
NS_GetSecureUpgradedURI(nsIURI
* aURI
, nsIURI
** aUpgradedURI
);
948 nsresult
NS_CompareLoadInfoAndLoadContext(nsIChannel
* aChannel
);
951 * Return true if this channel should be classified by the URL classifier.
953 bool NS_ShouldClassifyChannel(nsIChannel
* aChannel
);
956 * Helper to set the blocking reason on loadinfo of the channel.
958 nsresult
NS_SetRequestBlockingReason(nsIChannel
* channel
, uint32_t reason
);
959 nsresult
NS_SetRequestBlockingReason(nsILoadInfo
* loadInfo
, uint32_t reason
);
960 nsresult
NS_SetRequestBlockingReasonIfNull(nsILoadInfo
* loadInfo
,
966 const static uint64_t kJS_MAX_SAFE_UINTEGER
= +9007199254740991ULL;
967 const static int64_t kJS_MIN_SAFE_INTEGER
= -9007199254740991LL;
968 const static int64_t kJS_MAX_SAFE_INTEGER
= +9007199254740991LL;
970 // Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
971 bool InScriptableRange(int64_t val
);
973 // Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER
974 bool InScriptableRange(uint64_t val
);
977 * Given the value of a single header field (such as
978 * Content-Disposition and Content-Type) and the name of a parameter
979 * (e.g. filename, name, charset), returns the value of the parameter.
980 * See nsIMIMEHeaderParam.idl for more information.
982 * @param aHeaderVal a header string to get the value of a parameter
984 * @param aParamName the name of a MIME header parameter (e.g.
985 * filename, name, charset). If empty or nullptr,
986 * returns the first (possibly) _unnamed_ 'parameter'.
987 * @return the value of <code>aParamName</code> in Unichar(UTF-16).
989 nsresult
GetParameterHTTP(const nsACString
& aHeaderVal
, const char* aParamName
,
993 * Helper function that determines if channel is an HTTP POST.
996 * The channel to test
998 * @return True if channel is an HTTP post.
1000 bool ChannelIsPost(nsIChannel
* aChannel
);
1003 * Convenience functions for verifying nsIURI schemes. These functions simply
1004 * wrap aURI->SchemeIs(), but specify the protocol as part of the function name.
1007 bool SchemeIsHTTP(nsIURI
* aURI
);
1008 bool SchemeIsHTTPS(nsIURI
* aURI
);
1009 bool SchemeIsJavascript(nsIURI
* aURI
);
1010 bool SchemeIsChrome(nsIURI
* aURI
);
1011 bool SchemeIsAbout(nsIURI
* aURI
);
1012 bool SchemeIsBlob(nsIURI
* aURI
);
1013 bool SchemeIsFile(nsIURI
* aURI
);
1014 bool SchemeIsData(nsIURI
* aURI
);
1015 bool SchemeIsViewSource(nsIURI
* aURI
);
1016 bool SchemeIsResource(nsIURI
* aURI
);
1017 bool SchemeIsFTP(nsIURI
* aURI
);
1019 // Helper functions for SetProtocol methods to follow
1020 // step 2.1 in https://url.spec.whatwg.org/#scheme-state
1021 bool SchemeIsSpecial(const nsACString
&);
1022 bool IsSchemeChangePermitted(nsIURI
*, const nsACString
&);
1023 already_AddRefed
<nsIURI
> TryChangeProtocol(nsIURI
*, const nsAString
&);
1030 nsString mIntegrity
;
1036 nsString mCrossOrigin
;
1037 nsString mReferrerPolicy
;
1039 nsString mFetchPriority
;
1044 nsresult
NewResolveHref(nsIURI
** aOutURI
, nsIURI
* aBaseURI
) const;
1046 bool operator==(const LinkHeader
& rhs
) const;
1048 void MaybeUpdateAttribute(const nsAString
& aAttribute
,
1049 const char16_t
* aValue
);
1052 // Implements roughly step 2 to 4 of
1053 // <https://httpwg.org/specs/rfc8288.html#parse-set>.
1054 nsTArray
<LinkHeader
> ParseLinkHeader(const nsAString
& aLinkData
);
1056 enum ASDestination
: uint8_t {
1057 DESTINATION_INVALID
,
1059 DESTINATION_DOCUMENT
,
1063 DESTINATION_MANIFEST
,
1067 DESTINATION_SERVICEWORKER
,
1068 DESTINATION_SHAREDWORKER
,
1077 void ParseAsValue(const nsAString
& aValue
, nsAttrValue
& aResult
);
1078 nsContentPolicyType
AsValueToContentPolicy(const nsAttrValue
& aValue
);
1079 bool IsScriptLikeOrInvalid(const nsAString
& aAs
);
1081 bool CheckPreloadAttrs(const nsAttrValue
& aAs
, const nsAString
& aType
,
1082 const nsAString
& aMedia
,
1083 mozilla::dom::Document
* aDocument
);
1084 void WarnIgnoredPreload(const mozilla::dom::Document
&, nsIURI
&);
1087 * Returns true if the |aInput| in is part of the root domain of |aHost|.
1088 * For example, if |aInput| is "www.mozilla.org", and we pass in
1089 * "mozilla.org" as |aHost|, this will return true. It would return false
1090 * the other way around.
1092 * @param aInput The host to be analyzed.
1093 * @param aHost The host to compare to.
1095 nsresult
HasRootDomain(const nsACString
& aInput
, const nsACString
& aHost
,
1098 void CheckForBrokenChromeURL(nsILoadInfo
* aLoadInfo
, nsIURI
* aURI
);
1100 bool IsCoepCredentiallessEnabled(bool aIsOriginTrialCoepCredentiallessEnabled
);
1103 } // namespace mozilla
1105 #endif // !nsNetUtil_h__