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