Source code

Revision control

Other Tools

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