nsURLHelper.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* -*- Mode: C++; tab-width: 4; 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. #ifndef nsURLHelper_h__
  6. #define nsURLHelper_h__
  7. #include "nsString.h"
  8. class nsIFile;
  9. class nsIURLParser;
  10. enum netCoalesceFlags
  11. {
  12. NET_COALESCE_NORMAL = 0,
  13. /**
  14. * retains /../ that reach above dir root (useful for FTP
  15. * servers in which the root of the FTP URL is not necessarily
  16. * the root of the FTP filesystem).
  17. */
  18. NET_COALESCE_ALLOW_RELATIVE_ROOT = 1<<0,
  19. /**
  20. * recognizes /%2F and // as markers for the root directory
  21. * and handles them properly.
  22. */
  23. NET_COALESCE_DOUBLE_SLASH_IS_ROOT = 1<<1
  24. };
  25. //----------------------------------------------------------------------------
  26. // This module contains some private helper functions related to URL parsing.
  27. //----------------------------------------------------------------------------
  28. /* shutdown frees URL parser */
  29. void net_ShutdownURLHelper();
  30. #ifdef XP_MACOSX
  31. void net_ShutdownURLHelperOSX();
  32. #endif
  33. /* access URL parsers */
  34. nsIURLParser * net_GetAuthURLParser();
  35. nsIURLParser * net_GetNoAuthURLParser();
  36. nsIURLParser * net_GetStdURLParser();
  37. /* convert between nsIFile and file:// URL spec
  38. * net_GetURLSpecFromFile does an extra stat, so callers should
  39. * avoid it if possible in favor of net_GetURLSpecFromActualFile
  40. * and net_GetURLSpecFromDir */
  41. nsresult net_GetURLSpecFromFile(nsIFile *, nsACString &);
  42. nsresult net_GetURLSpecFromDir(nsIFile *, nsACString &);
  43. nsresult net_GetURLSpecFromActualFile(nsIFile *, nsACString &);
  44. nsresult net_GetFileFromURLSpec(const nsACString &, nsIFile **);
  45. /* extract file path components from file:// URL */
  46. nsresult net_ParseFileURL(const nsACString &inURL,
  47. nsACString &outDirectory,
  48. nsACString &outFileBaseName,
  49. nsACString &outFileExtension);
  50. /* handle .. in dirs while resolving URLs (path is UTF-8) */
  51. void net_CoalesceDirs(netCoalesceFlags flags, char* path);
  52. /**
  53. * Resolves a relative path string containing "." and ".."
  54. * with respect to a base path (assumed to already be resolved).
  55. * For example, resolving "../../foo/./bar/../baz.html" w.r.t.
  56. * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to
  57. * ascend above the base results in the NS_ERROR_MALFORMED_URI
  58. * exception. If basePath is null, it treats it as "/".
  59. *
  60. * @param relativePath a relative URI
  61. * @param basePath a base URI
  62. *
  63. * @return a new string, representing canonical uri
  64. */
  65. nsresult net_ResolveRelativePath(const nsACString &relativePath,
  66. const nsACString &basePath,
  67. nsACString &result);
  68. /**
  69. * Check if a URL is absolute
  70. *
  71. * @param inURL URL spec
  72. * @return true if the given spec represents an absolute URL
  73. */
  74. bool net_IsAbsoluteURL(const nsACString& inURL);
  75. /**
  76. * Extract URI-Scheme if possible
  77. *
  78. * @param inURI URI spec
  79. * @param scheme scheme copied to this buffer on return (may be null)
  80. */
  81. nsresult net_ExtractURLScheme(const nsACString &inURI,
  82. nsACString &scheme);
  83. /* check that the given scheme conforms to RFC 2396 */
  84. bool net_IsValidScheme(const char *scheme, uint32_t schemeLen);
  85. inline bool net_IsValidScheme(const nsAFlatCString &scheme)
  86. {
  87. return net_IsValidScheme(scheme.get(), scheme.Length());
  88. }
  89. /**
  90. * This function strips out all C0 controls and space at the beginning and end
  91. * of the URL and filters out \r, \n, \t from the middle of the URL. This makes
  92. * it safe to call on things like javascript: urls or data: urls, where we may
  93. * in fact run into whitespace that is not properly encoded.
  94. *
  95. * @param input the URL spec we want to filter
  96. * @param result the out param to write to if filtering happens
  97. */
  98. void net_FilterURIString(const nsACString& input, nsACString& result);
  99. #if defined(XP_WIN)
  100. /**
  101. * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a
  102. * forward-slash. This function maps any back-slashes to forward-slashes.
  103. *
  104. * @param aURL
  105. * The URL string to normalize (UTF-8 encoded). This can be a
  106. * relative URL segment.
  107. * @param aResultBuf
  108. * The resulting string is appended to this string. If the input URL
  109. * is already normalized, then aResultBuf is unchanged.
  110. *
  111. * @returns false if aURL is already normalized. Otherwise, returns true.
  112. */
  113. bool net_NormalizeFileURL(const nsACString &aURL,
  114. nsCString &aResultBuf);
  115. #endif
  116. /*****************************************************************************
  117. * generic string routines follow (XXX move to someplace more generic).
  118. */
  119. /* convert to lower case */
  120. void net_ToLowerCase(char* str, uint32_t length);
  121. void net_ToLowerCase(char* str);
  122. /**
  123. * returns pointer to first character of |str| in the given set. if not found,
  124. * then |end| is returned. stops prematurely if a null byte is encountered,
  125. * and returns the address of the null byte.
  126. */
  127. char * net_FindCharInSet(const char *str, const char *end, const char *set);
  128. /**
  129. * returns pointer to first character of |str| NOT in the given set. if all
  130. * characters are in the given set, then |end| is returned. if '\0' is not
  131. * included in |set|, then stops prematurely if a null byte is encountered,
  132. * and returns the address of the null byte.
  133. */
  134. char * net_FindCharNotInSet(const char *str, const char *end, const char *set);
  135. /**
  136. * returns pointer to last character of |str| NOT in the given set. if all
  137. * characters are in the given set, then |str - 1| is returned.
  138. */
  139. char * net_RFindCharNotInSet(const char *str, const char *end, const char *set);
  140. /**
  141. * Parses a content-type header and returns the content type and
  142. * charset (if any). aCharset is not modified if no charset is
  143. * specified in anywhere in aHeaderStr. In that case (no charset
  144. * specified), aHadCharset is set to false. Otherwise, it's set to
  145. * true. Note that aContentCharset can be empty even if aHadCharset
  146. * is true.
  147. *
  148. * This parsing is suitable for HTTP request. Use net_ParseContentType
  149. * for parsing this header in HTTP responses.
  150. */
  151. void net_ParseRequestContentType(const nsACString &aHeaderStr,
  152. nsACString &aContentType,
  153. nsACString &aContentCharset,
  154. bool* aHadCharset);
  155. /**
  156. * Parses a content-type header and returns the content type and
  157. * charset (if any). aCharset is not modified if no charset is
  158. * specified in anywhere in aHeaderStr. In that case (no charset
  159. * specified), aHadCharset is set to false. Otherwise, it's set to
  160. * true. Note that aContentCharset can be empty even if aHadCharset
  161. * is true.
  162. */
  163. void net_ParseContentType(const nsACString &aHeaderStr,
  164. nsACString &aContentType,
  165. nsACString &aContentCharset,
  166. bool* aHadCharset);
  167. /**
  168. * As above, but also returns the start and end indexes for the charset
  169. * parameter in aHeaderStr. These are indices for the entire parameter, NOT
  170. * just the value. If there is "effectively" no charset parameter (e.g. if an
  171. * earlier type with one is overridden by a later type without one),
  172. * *aHadCharset will be true but *aCharsetStart will be set to -1. Note that
  173. * it's possible to have aContentCharset empty and *aHadCharset true when
  174. * *aCharsetStart is nonnegative; this corresponds to charset="".
  175. */
  176. void net_ParseContentType(const nsACString &aHeaderStr,
  177. nsACString &aContentType,
  178. nsACString &aContentCharset,
  179. bool *aHadCharset,
  180. int32_t *aCharsetStart,
  181. int32_t *aCharsetEnd);
  182. /* inline versions */
  183. /* remember the 64-bit platforms ;-) */
  184. #define NET_MAX_ADDRESS (((char*)0)-1)
  185. inline char *net_FindCharInSet(const char *str, const char *set)
  186. {
  187. return net_FindCharInSet(str, NET_MAX_ADDRESS, set);
  188. }
  189. inline char *net_FindCharNotInSet(const char *str, const char *set)
  190. {
  191. return net_FindCharNotInSet(str, NET_MAX_ADDRESS, set);
  192. }
  193. inline char *net_RFindCharNotInSet(const char *str, const char *set)
  194. {
  195. return net_RFindCharNotInSet(str, str + strlen(str), set);
  196. }
  197. /**
  198. * This function returns true if the given hostname does not include any
  199. * restricted characters. Otherwise, false is returned.
  200. */
  201. bool net_IsValidHostName(const nsCSubstring &host);
  202. /**
  203. * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2.
  204. */
  205. bool net_IsValidIPv4Addr(const char *addr, int32_t addrLen);
  206. /**
  207. * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2.
  208. */
  209. bool net_IsValidIPv6Addr(const char *addr, int32_t addrLen);
  210. /**
  211. * Returns the max length of a URL. The default is 1048576 (1 MB).
  212. * Can be changed by pref "network.standard-url.max-length"
  213. */
  214. int32_t net_GetURLMaxLength();
  215. #endif // !nsURLHelper_h__