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