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/. */
6 #ifndef nsURLHelper_h__
7 #define nsURLHelper_h__
11 #include "nsASCIIMask.h"
16 enum netCoalesceFlags
{
17 NET_COALESCE_NORMAL
= 0,
20 * retains /../ that reach above dir root (useful for FTP
21 * servers in which the root of the FTP URL is not necessarily
22 * the root of the FTP filesystem).
24 NET_COALESCE_ALLOW_RELATIVE_ROOT
= 1 << 0,
27 * recognizes /%2F and // as markers for the root directory
28 * and handles them properly.
30 NET_COALESCE_DOUBLE_SLASH_IS_ROOT
= 1 << 1
33 //----------------------------------------------------------------------------
34 // This module contains some private helper functions related to URL parsing.
35 //----------------------------------------------------------------------------
37 /* shutdown frees URL parser */
38 void net_ShutdownURLHelper();
40 void net_ShutdownURLHelperOSX();
43 /* access URL parsers */
44 nsIURLParser
* net_GetAuthURLParser();
45 nsIURLParser
* net_GetNoAuthURLParser();
46 nsIURLParser
* net_GetStdURLParser();
48 /* convert between nsIFile and file:// URL spec
49 * net_GetURLSpecFromFile does an extra stat, so callers should
50 * avoid it if possible in favor of net_GetURLSpecFromActualFile
51 * and net_GetURLSpecFromDir */
52 nsresult
net_GetURLSpecFromFile(nsIFile
*, nsACString
&);
53 nsresult
net_GetURLSpecFromDir(nsIFile
*, nsACString
&);
54 nsresult
net_GetURLSpecFromActualFile(nsIFile
*, nsACString
&);
55 nsresult
net_GetFileFromURLSpec(const nsACString
&, nsIFile
**);
57 /* extract file path components from file:// URL */
58 nsresult
net_ParseFileURL(const nsACString
& inURL
, nsACString
& outDirectory
,
59 nsACString
& outFileBaseName
,
60 nsACString
& outFileExtension
);
62 /* handle .. in dirs while resolving URLs (path is UTF-8) */
63 void net_CoalesceDirs(netCoalesceFlags flags
, char* path
);
66 * Check if a URL is absolute
68 * @param inURL URL spec
69 * @return true if the given spec represents an absolute URL
71 bool net_IsAbsoluteURL(const nsACString
& uri
);
74 * Extract URI-Scheme if possible
76 * @param inURI URI spec
77 * @param scheme scheme copied to this buffer on return. Is lowercase.
79 nsresult
net_ExtractURLScheme(const nsACString
& inURI
, nsACString
& scheme
);
81 /* check that the given scheme conforms to RFC 2396 */
82 bool net_IsValidScheme(const nsACString
& scheme
);
85 * This function strips out all C0 controls and space at the beginning and end
86 * of the URL and filters out \r, \n, \t from the middle of the URL. This makes
87 * it safe to call on things like javascript: urls or data: urls, where we may
88 * in fact run into whitespace that is not properly encoded.
90 * @param input the URL spec we want to filter
91 * @param result the out param to write to if filtering happens
93 void net_FilterURIString(const nsACString
& input
, nsACString
& result
);
96 * This function performs character stripping just like net_FilterURIString,
97 * with the added benefit of also performing percent escaping of dissallowed
98 * characters, all in one pass. Saving one pass is very important when operating
99 * on really large strings.
101 * @param aInput the URL spec we want to filter
102 * @param aFlags the flags which control which characters we escape
103 * @param aFilterMask a mask of characters that should excluded from the result
104 * @param aResult the out param to write to if filtering happens
106 nsresult
net_FilterAndEscapeURI(const nsACString
& aInput
, uint32_t aFlags
,
107 const ASCIIMaskArray
& aFilterMask
,
108 nsACString
& aResult
);
112 * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a
113 * forward-slash. This function maps any back-slashes to forward-slashes.
116 * The URL string to normalize (UTF-8 encoded). This can be a
117 * relative URL segment.
119 * The resulting string is appended to this string. If the input URL
120 * is already normalized, then aResultBuf is unchanged.
122 * @returns false if aURL is already normalized. Otherwise, returns true.
124 bool net_NormalizeFileURL(const nsACString
& aURL
, nsCString
& aResultBuf
);
127 /*****************************************************************************
128 * generic string routines follow (XXX move to someplace more generic).
131 /* convert to lower case */
132 void net_ToLowerCase(char* str
, uint32_t length
);
133 void net_ToLowerCase(char* str
);
136 * returns pointer to first character of |str| in the given set. if not found,
137 * then |end| is returned. stops prematurely if a null byte is encountered,
138 * and returns the address of the null byte.
140 char* net_FindCharInSet(const char* iter
, const char* stop
, const char* set
);
143 * returns pointer to first character of |str| NOT in the given set. if all
144 * characters are in the given set, then |end| is returned. if '\0' is not
145 * included in |set|, then stops prematurely if a null byte is encountered,
146 * and returns the address of the null byte.
148 char* net_FindCharNotInSet(const char* iter
, const char* stop
, const char* set
);
151 * returns pointer to last character of |str| NOT in the given set. if all
152 * characters are in the given set, then |str - 1| is returned.
154 char* net_RFindCharNotInSet(const char* stop
, const char* iter
,
158 * Parses a content-type header and returns the content type and
159 * charset (if any). aCharset is not modified if no charset is
160 * specified in anywhere in aHeaderStr. In that case (no charset
161 * specified), aHadCharset is set to false. Otherwise, it's set to
162 * true. Note that aContentCharset can be empty even if aHadCharset
165 * This parsing is suitable for HTTP request. Use net_ParseContentType
166 * for parsing this header in HTTP responses.
168 void net_ParseRequestContentType(const nsACString
& aHeaderStr
,
169 nsACString
& aContentType
,
170 nsACString
& aContentCharset
,
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
181 void net_ParseContentType(const nsACString
& aHeaderStr
,
182 nsACString
& aContentType
, nsACString
& aContentCharset
,
185 * As above, but also returns the start and end indexes for the charset
186 * parameter in aHeaderStr. These are indices for the entire parameter, NOT
187 * just the value. If there is "effectively" no charset parameter (e.g. if an
188 * earlier type with one is overridden by a later type without one),
189 * *aHadCharset will be true but *aCharsetStart will be set to -1. Note that
190 * it's possible to have aContentCharset empty and *aHadCharset true when
191 * *aCharsetStart is nonnegative; this corresponds to charset="".
193 void net_ParseContentType(const nsACString
& aHeaderStr
,
194 nsACString
& aContentType
, nsACString
& aContentCharset
,
195 bool* aHadCharset
, int32_t* aCharsetStart
,
196 int32_t* aCharsetEnd
);
198 /* inline versions */
200 /* remember the 64-bit platforms ;-) */
201 #define NET_MAX_ADDRESS ((char*)UINTPTR_MAX)
203 inline char* net_FindCharInSet(const char* str
, const char* set
) {
204 return net_FindCharInSet(str
, NET_MAX_ADDRESS
, set
);
206 inline char* net_FindCharNotInSet(const char* str
, const char* set
) {
207 return net_FindCharNotInSet(str
, NET_MAX_ADDRESS
, set
);
209 inline char* net_RFindCharNotInSet(const char* str
, const char* set
) {
210 return net_RFindCharNotInSet(str
, str
+ strlen(str
), set
);
214 * This function returns true if the given hostname does not include any
215 * restricted characters. Otherwise, false is returned.
217 bool net_IsValidHostName(const nsACString
& host
);
220 * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2.
222 bool net_IsValidIPv4Addr(const nsACString
& aAddr
);
225 * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2.
227 bool net_IsValidIPv6Addr(const nsACString
& aAddr
);
231 * A class for handling form-urlencoded query strings.
233 * Manages an ordered list of name-value pairs, and allows conversion from and
234 * to the string representation.
236 * In addition, there are static functions for handling one-shot use cases.
238 class URLParams final
{
241 * \brief Parses a query string and calls a parameter handler for each
242 * name/value pair. The parameter handler can stop processing early by
245 * \param aInput the query string to parse
246 * \param aParamHandler the parameter handler as desribed above
247 * \tparam ParamHandler a function type compatible with signature
248 * bool(nsString, nsString)
250 * \return false if the parameter handler returned false for any parameter,
253 template <typename ParamHandler
>
254 static bool Parse(const nsACString
& aInput
, ParamHandler aParamHandler
) {
255 const char* start
= aInput
.BeginReading();
256 const char* const end
= aInput
.EndReading();
258 while (start
!= end
) {
259 nsAutoString decodedName
;
260 nsAutoString decodedValue
;
262 if (!ParseNextInternal(start
, end
, &decodedName
, &decodedValue
)) {
266 if (!aParamHandler(std::move(decodedName
), std::move(decodedValue
))) {
274 * \brief Parses a query string and returns the value of a single parameter
277 * If there are multiple parameters with the same name, the value of the first
280 * \param aInput the query string to parse
281 * \param aName the name of the parameter to extract
282 * \param[out] aValue will be assigned the parameter value, set to void if
283 * there is no match \return true iff there was a parameter with with name
286 static bool Extract(const nsACString
& aInput
, const nsAString
& aName
,
290 * \brief Resets the state of this instance and parses a new query string.
292 * \param aInput the query string to parse
294 void ParseInput(const nsACString
& aInput
);
297 * Serializes the current state to a query string.
299 * \param[out] aValue will be assigned the result of the serialization
300 * \param aEncode If this is true, the serialization will encode the string.
302 void Serialize(nsAString
& aValue
, bool aEncode
) const;
304 void Get(const nsAString
& aName
, nsString
& aRetval
);
306 void GetAll(const nsAString
& aName
, nsTArray
<nsString
>& aRetval
);
309 * \brief Sets the value of a given parameter.
311 * If one or more parameters of the name exist, the value of the first is
312 * replaced, and all further parameters of the name are deleted. Otherwise,
313 * the behaviour is the same as \ref Append.
315 void Set(const nsAString
& aName
, const nsAString
& aValue
);
317 void Append(const nsAString
& aName
, const nsAString
& aValue
);
319 bool Has(const nsAString
& aName
);
321 bool Has(const nsAString
& aName
, const nsAString
& aValue
);
324 * \brief Deletes all parameters with the given name.
326 void Delete(const nsAString
& aName
);
328 void Delete(const nsAString
& aName
, const nsAString
& aValue
);
330 void DeleteAll() { mParams
.Clear(); }
332 uint32_t Length() const { return mParams
.Length(); }
334 const nsAString
& GetKeyAtIndex(uint32_t aIndex
) const {
335 MOZ_ASSERT(aIndex
< mParams
.Length());
336 return mParams
[aIndex
].mKey
;
339 const nsAString
& GetValueAtIndex(uint32_t aIndex
) const {
340 MOZ_ASSERT(aIndex
< mParams
.Length());
341 return mParams
[aIndex
].mValue
;
345 * \brief Performs a stable sort of the parameters, maintaining the order of
346 * multiple parameters with the same name.
351 static void DecodeString(const nsACString
& aInput
, nsAString
& aOutput
);
352 static void ConvertString(const nsACString
& aInput
, nsAString
& aOutput
);
353 static bool ParseNextInternal(const char*& aStart
, const char* aEnd
,
354 nsAString
* aOutDecodedName
,
355 nsAString
* aOutDecodedValue
);
362 nsTArray
<Param
> mParams
;
364 } // namespace mozilla
366 #endif // !nsURLHelper_h__