nsINetUtil.idl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsISupports.idl"
  6. interface nsIURI;
  7. interface nsIPrefBranch;
  8. /**
  9. * nsINetUtil provides various network-related utility methods.
  10. */
  11. [scriptable, uuid(fe2625ec-b884-4df1-b39c-9e830e47aa94)]
  12. interface nsINetUtil : nsISupports
  13. {
  14. /**
  15. * Parse a Content-Type header value in strict mode. This is a more
  16. * conservative parser that reject things that violate RFC 7231 section
  17. * 3.1.1.1. This is typically useful for parsing Content-Type header values
  18. * that are used for HTTP requests, and those that are used to make security
  19. * decisions.
  20. *
  21. * @param aTypeHeader the header string to parse
  22. * @param [out] aCharset the charset parameter specified in the
  23. * header, if any.
  24. * @param [out] aHadCharset whether a charset was explicitly specified.
  25. * @return the MIME type specified in the header, in lower-case.
  26. */
  27. AUTF8String parseRequestContentType(in AUTF8String aTypeHeader,
  28. out AUTF8String aCharset,
  29. out boolean aHadCharset);
  30. /**
  31. * Parse a Content-Type header value in relaxed mode. This is a more
  32. * permissive parser that ignores things that go against RFC 7231 section
  33. * 3.1.1.1. This is typically useful for parsing Content-Type header values
  34. * received from web servers where we want to make a best effort attempt
  35. * at extracting a useful MIME type and charset.
  36. *
  37. * NOTE: DO NOT USE THIS if you're going to make security decisions
  38. * based on the result.
  39. *
  40. * @param aTypeHeader the header string to parse
  41. * @param [out] aCharset the charset parameter specified in the
  42. * header, if any.
  43. * @param [out] aHadCharset whether a charset was explicitly specified.
  44. * @return the MIME type specified in the header, in lower-case.
  45. */
  46. AUTF8String parseResponseContentType(in AUTF8String aTypeHeader,
  47. out AUTF8String aCharset,
  48. out boolean aHadCharset);
  49. /**
  50. * Test whether the given URI's handler has the given protocol flags.
  51. *
  52. * @param aURI the URI in question
  53. * @param aFlags the flags we're testing for.
  54. *
  55. * @return whether the protocol handler for aURI has all the flags
  56. * in aFlags.
  57. */
  58. boolean protocolHasFlags(in nsIURI aURI, in unsigned long aFlag);
  59. /**
  60. * Test whether the protocol handler for this URI or that for any of
  61. * its inner URIs has the given protocol flags. This will QI aURI to
  62. * nsINestedURI and walk the nested URI chain.
  63. *
  64. * @param aURI the URI in question
  65. * @param aFlags the flags we're testing for.
  66. *
  67. * @return whether any of the protocol handlers involved have all the flags
  68. * in aFlags.
  69. */
  70. boolean URIChainHasFlags(in nsIURI aURI, in unsigned long aFlags);
  71. /**
  72. * Take aURI and produce an immutable version of it for the caller. If aURI
  73. * is immutable this will be aURI itself; otherwise this will be a clone,
  74. * marked immutable if possible. Passing null to this method is allowed; in
  75. * that case it will return null.
  76. */
  77. nsIURI toImmutableURI(in nsIURI aURI);
  78. /**
  79. * Create a simple nested URI using the result of
  80. * toImmutableURI on the passed-in aURI which may not be null.
  81. * Note: The return URI will not have had its spec set yet.
  82. */
  83. nsIURI newSimpleNestedURI(in nsIURI aURI);
  84. /** Escape every character with its %XX-escaped equivalent */
  85. const unsigned long ESCAPE_ALL = 0;
  86. /** Leave alphanumeric characters intact and %XX-escape all others */
  87. const unsigned long ESCAPE_XALPHAS = 1;
  88. /** Leave alphanumeric characters intact, convert spaces to '+',
  89. %XX-escape all others */
  90. const unsigned long ESCAPE_XPALPHAS = 2;
  91. /** Leave alphanumeric characters and forward slashes intact,
  92. %XX-escape all others */
  93. const unsigned long ESCAPE_URL_PATH = 4;
  94. /**
  95. * escape a string with %00-style escaping
  96. */
  97. ACString escapeString(in ACString aString, in unsigned long aEscapeType);
  98. /** %XX-escape URL scheme */
  99. const unsigned long ESCAPE_URL_SCHEME = 1;
  100. /** %XX-escape username in the URL */
  101. const unsigned long ESCAPE_URL_USERNAME = 1 << 1;
  102. /** %XX-escape password in the URL */
  103. const unsigned long ESCAPE_URL_PASSWORD = 1 << 2;
  104. /** %XX-escape URL host */
  105. const unsigned long ESCAPE_URL_HOST = 1 << 3;
  106. /** %XX-escape URL directory */
  107. const unsigned long ESCAPE_URL_DIRECTORY = 1 << 4;
  108. /** %XX-escape file basename in the URL */
  109. const unsigned long ESCAPE_URL_FILE_BASENAME = 1 << 5;
  110. /** %XX-escape file extension in the URL */
  111. const unsigned long ESCAPE_URL_FILE_EXTENSION = 1 << 6;
  112. /** %XX-escape URL parameters */
  113. const unsigned long ESCAPE_URL_PARAM = 1 << 7;
  114. /** %XX-escape URL query */
  115. const unsigned long ESCAPE_URL_QUERY = 1 << 8;
  116. /** %XX-escape URL ref */
  117. const unsigned long ESCAPE_URL_REF = 1 << 9;
  118. /** %XX-escape URL path - same as escaping directory, basename and extension */
  119. const unsigned long ESCAPE_URL_FILEPATH =
  120. ESCAPE_URL_DIRECTORY | ESCAPE_URL_FILE_BASENAME | ESCAPE_URL_FILE_EXTENSION;
  121. /** %XX-escape scheme, username, password, host, path, params, query and ref */
  122. const unsigned long ESCAPE_URL_MINIMAL =
  123. ESCAPE_URL_SCHEME | ESCAPE_URL_USERNAME | ESCAPE_URL_PASSWORD |
  124. ESCAPE_URL_HOST | ESCAPE_URL_FILEPATH | ESCAPE_URL_PARAM |
  125. ESCAPE_URL_QUERY | ESCAPE_URL_REF;
  126. /** Force %XX-escaping of already escaped sequences */
  127. const unsigned long ESCAPE_URL_FORCED = 1 << 10;
  128. /** Skip non-ascii octets, %XX-escape all others */
  129. const unsigned long ESCAPE_URL_ONLY_ASCII = 1 << 11;
  130. /**
  131. * Skip graphic octets (0x20-0x7E) when escaping
  132. * Skips all ASCII octets (0x00-0x7F) when unescaping
  133. */
  134. const unsigned long ESCAPE_URL_ONLY_NONASCII = 1 << 12;
  135. /** Force %XX-escape of colon */
  136. const unsigned long ESCAPE_URL_COLON = 1 << 14;
  137. /** Skip C0 and DEL from unescaping */
  138. const unsigned long ESCAPE_URL_SKIP_CONTROL = 1 << 15;
  139. /** %XX-escape external protocol handler URL */
  140. const unsigned long ESCAPE_URL_EXT_HANDLER = 1 << 17;
  141. /**
  142. * %XX-Escape invalid chars in a URL segment.
  143. *
  144. * @param aStr the URL to be escaped
  145. * @param aFlags the URL segment type flags
  146. *
  147. * @return the escaped string (the string itself if escaping did not happen)
  148. *
  149. */
  150. ACString escapeURL(in ACString aStr, in unsigned long aFlags);
  151. /**
  152. * Expands URL escape sequences
  153. *
  154. * @param aStr the URL to be unescaped
  155. * @param aFlags only ESCAPE_URL_ONLY_NONASCII and ESCAPE_URL_SKIP_CONTROL
  156. * are recognized. If |aFlags| is 0 all escape sequences are
  157. * unescaped
  158. * @return unescaped string
  159. */
  160. ACString unescapeString(in AUTF8String aStr, in unsigned long aFlags);
  161. /**
  162. * Extract the charset parameter location and value from a content-type
  163. * header.
  164. *
  165. * @param aTypeHeader the header string to parse
  166. * @param [out] aCharset the charset parameter specified in the
  167. * header, if any.
  168. * @param [out] aCharsetStart index of the start of the charset parameter
  169. * (the ';' separating it from what came before) in aTypeHeader.
  170. * If this function returns false, this argument will still be
  171. * set, to the index of the location where a new charset should
  172. * be inserted.
  173. * @param [out] aCharsetEnd index of the end of the charset parameter (the
  174. * ';' separating it from what comes after, or the end
  175. * of the string) in aTypeHeader. If this function returns
  176. * false, this argument will still be set, to the index of the
  177. * location where a new charset should be inserted.
  178. *
  179. * @return whether a charset parameter was found. This can be false even in
  180. * cases when parseContentType would claim to have a charset, if the type
  181. * that won out does not have a charset parameter specified.
  182. */
  183. boolean extractCharsetFromContentType(in AUTF8String aTypeHeader,
  184. out AUTF8String aCharset,
  185. out long aCharsetStart,
  186. out long aCharsetEnd);
  187. /**
  188. * Parse an attribute referrer policy string (no-referrer, origin, unsafe-url)
  189. * and return the according integer code (defined in nsIHttpChannel.idl)
  190. *
  191. * @param aPolicyString
  192. * the policy string given as attribute
  193. * @return aPolicyEnum
  194. * referrer policy code from nsIHttpChannel.idl, (see parser in
  195. * ReferrerPolicy.h for details)
  196. */
  197. unsigned long parseAttributePolicyString(in AString aPolicyString);
  198. };