1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Andreas Otte.
19 : * Portions created by the Initial Developer are Copyright (C) 2000
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsURLHelper_h__
39 : #define nsURLHelper_h__
40 :
41 : #include "nsString.h"
42 :
43 : class nsIFile;
44 : class nsIURLParser;
45 :
46 : enum netCoalesceFlags
47 : {
48 : NET_COALESCE_NORMAL = 0,
49 :
50 : /**
51 : * retains /../ that reach above dir root (useful for FTP
52 : * servers in which the root of the FTP URL is not necessarily
53 : * the root of the FTP filesystem).
54 : */
55 : NET_COALESCE_ALLOW_RELATIVE_ROOT = 1<<0,
56 :
57 : /**
58 : * recognizes /%2F and // as markers for the root directory
59 : * and handles them properly.
60 : */
61 : NET_COALESCE_DOUBLE_SLASH_IS_ROOT = 1<<1
62 : };
63 :
64 : //----------------------------------------------------------------------------
65 : // This module contains some private helper functions related to URL parsing.
66 : //----------------------------------------------------------------------------
67 :
68 : /* shutdown frees URL parser */
69 : NS_HIDDEN_(void) net_ShutdownURLHelper();
70 : #ifdef XP_MACOSX
71 : NS_HIDDEN_(void) net_ShutdownURLHelperOSX();
72 : #endif
73 :
74 : /* access URL parsers */
75 : NS_HIDDEN_(nsIURLParser *) net_GetAuthURLParser();
76 : NS_HIDDEN_(nsIURLParser *) net_GetNoAuthURLParser();
77 : NS_HIDDEN_(nsIURLParser *) net_GetStdURLParser();
78 :
79 : /* convert between nsIFile and file:// URL spec
80 : * net_GetURLSpecFromFile does an extra stat, so callers should
81 : * avoid it if possible in favor of net_GetURLSpecFromActualFile
82 : * and net_GetURLSpecFromDir */
83 : NS_HIDDEN_(nsresult) net_GetURLSpecFromFile(nsIFile *, nsACString &);
84 : NS_HIDDEN_(nsresult) net_GetURLSpecFromDir(nsIFile *, nsACString &);
85 : NS_HIDDEN_(nsresult) net_GetURLSpecFromActualFile(nsIFile *, nsACString &);
86 : NS_HIDDEN_(nsresult) net_GetFileFromURLSpec(const nsACString &, nsIFile **);
87 :
88 : /* extract file path components from file:// URL */
89 : NS_HIDDEN_(nsresult) net_ParseFileURL(const nsACString &inURL,
90 : nsACString &outDirectory,
91 : nsACString &outFileBaseName,
92 : nsACString &outFileExtension);
93 :
94 : /* handle .. in dirs while resolving URLs (path is UTF-8) */
95 : NS_HIDDEN_(void) net_CoalesceDirs(netCoalesceFlags flags, char* path);
96 :
97 : /**
98 : * Resolves a relative path string containing "." and ".."
99 : * with respect to a base path (assumed to already be resolved).
100 : * For example, resolving "../../foo/./bar/../baz.html" w.r.t.
101 : * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to
102 : * ascend above the base results in the NS_ERROR_MALFORMED_URI
103 : * exception. If basePath is null, it treats it as "/".
104 : *
105 : * @param relativePath a relative URI
106 : * @param basePath a base URI
107 : *
108 : * @return a new string, representing canonical uri
109 : */
110 : NS_HIDDEN_(nsresult) net_ResolveRelativePath(const nsACString &relativePath,
111 : const nsACString &basePath,
112 : nsACString &result);
113 :
114 : /**
115 : * Extract URI-Scheme if possible
116 : *
117 : * @param inURI URI spec
118 : * @param startPos start of scheme (may be null)
119 : * @param endPos end of scheme; index of colon (may be null)
120 : * @param scheme scheme copied to this buffer on return (may be null)
121 : */
122 : NS_HIDDEN_(nsresult) net_ExtractURLScheme(const nsACString &inURI,
123 : PRUint32 *startPos,
124 : PRUint32 *endPos,
125 : nsACString *scheme = nsnull);
126 :
127 : /* check that the given scheme conforms to RFC 2396 */
128 : NS_HIDDEN_(bool) net_IsValidScheme(const char *scheme, PRUint32 schemeLen);
129 :
130 0 : inline bool net_IsValidScheme(const nsAFlatCString &scheme)
131 : {
132 0 : return net_IsValidScheme(scheme.get(), scheme.Length());
133 : }
134 :
135 : /**
136 : * Filter out whitespace from a URI string. The input is the |str|
137 : * pointer. |result| is written to if and only if there is whitespace that has
138 : * to be filtered out. The return value is true if and only if |result| is
139 : * written to.
140 : *
141 : * This function strips out all whitespace at the beginning and end of the URL
142 : * and strips out \r, \n, \t from the middle of the URL. This makes it safe to
143 : * call on things like javascript: urls or data: urls, where we may in fact run
144 : * into whitespace that is not properly encoded. Note that stripping does not
145 : * occur in the scheme portion itself.
146 : *
147 : * @param str the pointer to the string to filter. Must be non-null.
148 : * @param result the out param to write to if filtering happens
149 : * @return whether result was written to
150 : */
151 : NS_HIDDEN_(bool) net_FilterURIString(const char *str, nsACString& result);
152 :
153 : #if defined(XP_WIN) || defined(XP_OS2)
154 : /**
155 : * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a
156 : * forward-slash. This function maps any back-slashes to forward-slashes.
157 : *
158 : * @param aURL
159 : * The URL string to normalize (UTF-8 encoded). This can be a
160 : * relative URL segment.
161 : * @param aResultBuf
162 : * The resulting string is appended to this string. If the input URL
163 : * is already normalized, then aResultBuf is unchanged.
164 : *
165 : * @returns false if aURL is already normalized. Otherwise, returns true.
166 : */
167 : NS_HIDDEN_(bool) net_NormalizeFileURL(const nsACString &aURL,
168 : nsCString &aResultBuf);
169 : #endif
170 :
171 : /*****************************************************************************
172 : * generic string routines follow (XXX move to someplace more generic).
173 : */
174 :
175 : /* convert to lower case */
176 : NS_HIDDEN_(void) net_ToLowerCase(char* str, PRUint32 length);
177 : NS_HIDDEN_(void) net_ToLowerCase(char* str);
178 :
179 : /**
180 : * returns pointer to first character of |str| in the given set. if not found,
181 : * then |end| is returned. stops prematurely if a null byte is encountered,
182 : * and returns the address of the null byte.
183 : */
184 : NS_HIDDEN_(char *) net_FindCharInSet(const char *str, const char *end, const char *set);
185 :
186 : /**
187 : * returns pointer to first character of |str| NOT in the given set. if all
188 : * characters are in the given set, then |end| is returned. if '\0' is not
189 : * included in |set|, then stops prematurely if a null byte is encountered,
190 : * and returns the address of the null byte.
191 : */
192 : NS_HIDDEN_(char *) net_FindCharNotInSet(const char *str, const char *end, const char *set);
193 :
194 : /**
195 : * returns pointer to last character of |str| NOT in the given set. if all
196 : * characters are in the given set, then |str - 1| is returned.
197 : */
198 : NS_HIDDEN_(char *) net_RFindCharNotInSet(const char *str, const char *end, const char *set);
199 :
200 : /**
201 : * Parses a content-type header and returns the content type and
202 : * charset (if any). aCharset is not modified if no charset is
203 : * specified in anywhere in aHeaderStr. In that case (no charset
204 : * specified), aHadCharset is set to false. Otherwise, it's set to
205 : * true. Note that aContentCharset can be empty even if aHadCharset
206 : * is true.
207 : */
208 : NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
209 : nsACString &aContentType,
210 : nsACString &aContentCharset,
211 : bool* aHadCharset);
212 : /**
213 : * As above, but also returns the start and end indexes for the charset
214 : * parameter in aHeaderStr. These are indices for the entire parameter, NOT
215 : * just the value. If there is "effectively" no charset parameter (e.g. if an
216 : * earlier type with one is overridden by a later type without one),
217 : * *aHadCharset will be true but *aCharsetStart will be set to -1. Note that
218 : * it's possible to have aContentCharset empty and *aHadCharset true when
219 : * *aCharsetStart is nonnegative; this corresponds to charset="".
220 : */
221 : NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
222 : nsACString &aContentType,
223 : nsACString &aContentCharset,
224 : bool *aHadCharset,
225 : PRInt32 *aCharsetStart,
226 : PRInt32 *aCharsetEnd);
227 :
228 : /* inline versions */
229 :
230 : /* remember the 64-bit platforms ;-) */
231 : #define NET_MAX_ADDRESS (((char*)0)-1)
232 :
233 1358 : inline char *net_FindCharInSet(const char *str, const char *set)
234 : {
235 1358 : return net_FindCharInSet(str, NET_MAX_ADDRESS, set);
236 : }
237 20139 : inline char *net_FindCharNotInSet(const char *str, const char *set)
238 : {
239 20139 : return net_FindCharNotInSet(str, NET_MAX_ADDRESS, set);
240 : }
241 18781 : inline char *net_RFindCharNotInSet(const char *str, const char *set)
242 : {
243 18781 : return net_RFindCharNotInSet(str, str + strlen(str), set);
244 : }
245 :
246 : /**
247 : * This function returns true if the given hostname does not include any
248 : * restricted characters. Otherwise, false is returned.
249 : */
250 : NS_HIDDEN_(bool) net_IsValidHostName(const nsCSubstring &host);
251 :
252 : /**
253 : * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2.
254 : */
255 : NS_HIDDEN_(bool) net_IsValidIPv4Addr(const char *addr, PRInt32 addrLen);
256 :
257 : /**
258 : * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2.
259 : */
260 : NS_HIDDEN_(bool) net_IsValidIPv6Addr(const char *addr, PRInt32 addrLen);
261 :
262 : #endif // !nsURLHelper_h__
|