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