nsNetUtil.h 43 KB

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