Line data Source code
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 :
6 : #ifndef nsURLHelper_h__
7 : #define nsURLHelper_h__
8 :
9 : #include "nsString.h"
10 :
11 : class nsIFile;
12 : class nsIURLParser;
13 :
14 : enum netCoalesceFlags
15 : {
16 : NET_COALESCE_NORMAL = 0,
17 :
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,
24 :
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 : };
31 :
32 : //----------------------------------------------------------------------------
33 : // This module contains some private helper functions related to URL parsing.
34 : //----------------------------------------------------------------------------
35 :
36 : /* shutdown frees URL parser */
37 : void net_ShutdownURLHelper();
38 : #ifdef XP_MACOSX
39 : void net_ShutdownURLHelperOSX();
40 : #endif
41 :
42 : /* access URL parsers */
43 : nsIURLParser * net_GetAuthURLParser();
44 : nsIURLParser * net_GetNoAuthURLParser();
45 : nsIURLParser * net_GetStdURLParser();
46 :
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 : nsresult net_GetURLSpecFromFile(nsIFile *, nsACString &);
52 : nsresult net_GetURLSpecFromDir(nsIFile *, nsACString &);
53 : nsresult net_GetURLSpecFromActualFile(nsIFile *, nsACString &);
54 : nsresult net_GetFileFromURLSpec(const nsACString &, nsIFile **);
55 :
56 : /* extract file path components from file:// URL */
57 : nsresult net_ParseFileURL(const nsACString &inURL,
58 : nsACString &outDirectory,
59 : nsACString &outFileBaseName,
60 : nsACString &outFileExtension);
61 :
62 : /* handle .. in dirs while resolving URLs (path is UTF-8) */
63 : void net_CoalesceDirs(netCoalesceFlags flags, char* path);
64 :
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 : nsresult net_ResolveRelativePath(const nsACString &relativePath,
79 : const nsACString &basePath,
80 : nsACString &result);
81 :
82 : /**
83 : * Check if a URL is absolute
84 : *
85 : * @param inURL URL spec
86 : * @return true if the given spec represents an absolute URL
87 : */
88 : bool net_IsAbsoluteURL(const nsACString& inURL);
89 :
90 : /**
91 : * Extract URI-Scheme if possible
92 : *
93 : * @param inURI URI spec
94 : * @param scheme scheme copied to this buffer on return (may be null)
95 : */
96 : nsresult net_ExtractURLScheme(const nsACString &inURI,
97 : nsACString &scheme);
98 :
99 : /* check that the given scheme conforms to RFC 2396 */
100 : bool net_IsValidScheme(const char *scheme, uint32_t schemeLen);
101 :
102 0 : inline bool net_IsValidScheme(const nsCString& scheme)
103 : {
104 0 : return net_IsValidScheme(scheme.get(), scheme.Length());
105 : }
106 :
107 : /**
108 : * This function strips out all C0 controls and space at the beginning and end
109 : * of the URL and filters out \r, \n, \t from the middle of the URL. This makes
110 : * it safe to call on things like javascript: urls or data: urls, where we may
111 : * in fact run into whitespace that is not properly encoded.
112 : *
113 : * @param input the URL spec we want to filter
114 : * @param result the out param to write to if filtering happens
115 : */
116 : void net_FilterURIString(const nsACString& input, nsACString& result);
117 :
118 : #if defined(XP_WIN)
119 : /**
120 : * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a
121 : * forward-slash. This function maps any back-slashes to forward-slashes.
122 : *
123 : * @param aURL
124 : * The URL string to normalize (UTF-8 encoded). This can be a
125 : * relative URL segment.
126 : * @param aResultBuf
127 : * The resulting string is appended to this string. If the input URL
128 : * is already normalized, then aResultBuf is unchanged.
129 : *
130 : * @returns false if aURL is already normalized. Otherwise, returns true.
131 : */
132 : bool net_NormalizeFileURL(const nsACString &aURL,
133 : nsCString &aResultBuf);
134 : #endif
135 :
136 : /*****************************************************************************
137 : * generic string routines follow (XXX move to someplace more generic).
138 : */
139 :
140 : /* convert to lower case */
141 : void net_ToLowerCase(char* str, uint32_t length);
142 : void net_ToLowerCase(char* str);
143 :
144 : /**
145 : * returns pointer to first character of |str| in the given set. if not found,
146 : * then |end| is returned. stops prematurely if a null byte is encountered,
147 : * and returns the address of the null byte.
148 : */
149 : char * net_FindCharInSet(const char *str, const char *end, const char *set);
150 :
151 : /**
152 : * returns pointer to first character of |str| NOT in the given set. if all
153 : * characters are in the given set, then |end| is returned. if '\0' is not
154 : * included in |set|, then stops prematurely if a null byte is encountered,
155 : * and returns the address of the null byte.
156 : */
157 : char * net_FindCharNotInSet(const char *str, const char *end, const char *set);
158 :
159 : /**
160 : * returns pointer to last character of |str| NOT in the given set. if all
161 : * characters are in the given set, then |str - 1| is returned.
162 : */
163 : char * net_RFindCharNotInSet(const char *str, const char *end, const char *set);
164 :
165 : /**
166 : * Parses a content-type header and returns the content type and
167 : * charset (if any). aCharset is not modified if no charset is
168 : * specified in anywhere in aHeaderStr. In that case (no charset
169 : * specified), aHadCharset is set to false. Otherwise, it's set to
170 : * true. Note that aContentCharset can be empty even if aHadCharset
171 : * is true.
172 : *
173 : * This parsing is suitable for HTTP request. Use net_ParseContentType
174 : * for parsing this header in HTTP responses.
175 : */
176 : void net_ParseRequestContentType(const nsACString &aHeaderStr,
177 : nsACString &aContentType,
178 : nsACString &aContentCharset,
179 : bool* aHadCharset);
180 :
181 : /**
182 : * Parses a content-type header and returns the content type and
183 : * charset (if any). aCharset is not modified if no charset is
184 : * specified in anywhere in aHeaderStr. In that case (no charset
185 : * specified), aHadCharset is set to false. Otherwise, it's set to
186 : * true. Note that aContentCharset can be empty even if aHadCharset
187 : * is true.
188 : */
189 : void net_ParseContentType(const nsACString &aHeaderStr,
190 : nsACString &aContentType,
191 : nsACString &aContentCharset,
192 : bool* aHadCharset);
193 : /**
194 : * As above, but also returns the start and end indexes for the charset
195 : * parameter in aHeaderStr. These are indices for the entire parameter, NOT
196 : * just the value. If there is "effectively" no charset parameter (e.g. if an
197 : * earlier type with one is overridden by a later type without one),
198 : * *aHadCharset will be true but *aCharsetStart will be set to -1. Note that
199 : * it's possible to have aContentCharset empty and *aHadCharset true when
200 : * *aCharsetStart is nonnegative; this corresponds to charset="".
201 : */
202 : void net_ParseContentType(const nsACString &aHeaderStr,
203 : nsACString &aContentType,
204 : nsACString &aContentCharset,
205 : bool *aHadCharset,
206 : int32_t *aCharsetStart,
207 : int32_t *aCharsetEnd);
208 :
209 : /* inline versions */
210 :
211 : /* remember the 64-bit platforms ;-) */
212 : #define NET_MAX_ADDRESS (((char*)0)-1)
213 :
214 4 : inline char *net_FindCharInSet(const char *str, const char *set)
215 : {
216 4 : return net_FindCharInSet(str, NET_MAX_ADDRESS, set);
217 : }
218 4 : inline char *net_FindCharNotInSet(const char *str, const char *set)
219 : {
220 4 : return net_FindCharNotInSet(str, NET_MAX_ADDRESS, set);
221 : }
222 : inline char *net_RFindCharNotInSet(const char *str, const char *set)
223 : {
224 : return net_RFindCharNotInSet(str, str + strlen(str), set);
225 : }
226 :
227 : /**
228 : * This function returns true if the given hostname does not include any
229 : * restricted characters. Otherwise, false is returned.
230 : */
231 : bool net_IsValidHostName(const nsACString& host);
232 :
233 : /**
234 : * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2.
235 : */
236 : bool net_IsValidIPv4Addr(const char *addr, int32_t addrLen);
237 :
238 : /**
239 : * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2.
240 : */
241 : bool net_IsValidIPv6Addr(const char *addr, int32_t addrLen);
242 :
243 :
244 : /**
245 : * Returns the max length of a URL. The default is 1048576 (1 MB).
246 : * Can be changed by pref "network.standard-url.max-length"
247 : */
248 : int32_t net_GetURLMaxLength();
249 :
250 : #endif // !nsURLHelper_h__
|