michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsURLHelper_h__ michael@0: #define nsURLHelper_h__ michael@0: michael@0: #include "nsString.h" michael@0: michael@0: class nsIFile; michael@0: class nsIURLParser; michael@0: michael@0: enum netCoalesceFlags michael@0: { michael@0: NET_COALESCE_NORMAL = 0, michael@0: michael@0: /** michael@0: * retains /../ that reach above dir root (useful for FTP michael@0: * servers in which the root of the FTP URL is not necessarily michael@0: * the root of the FTP filesystem). michael@0: */ michael@0: NET_COALESCE_ALLOW_RELATIVE_ROOT = 1<<0, michael@0: michael@0: /** michael@0: * recognizes /%2F and // as markers for the root directory michael@0: * and handles them properly. michael@0: */ michael@0: NET_COALESCE_DOUBLE_SLASH_IS_ROOT = 1<<1 michael@0: }; michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // This module contains some private helper functions related to URL parsing. michael@0: //---------------------------------------------------------------------------- michael@0: michael@0: /* shutdown frees URL parser */ michael@0: NS_HIDDEN_(void) net_ShutdownURLHelper(); michael@0: #ifdef XP_MACOSX michael@0: NS_HIDDEN_(void) net_ShutdownURLHelperOSX(); michael@0: #endif michael@0: michael@0: /* access URL parsers */ michael@0: NS_HIDDEN_(nsIURLParser *) net_GetAuthURLParser(); michael@0: NS_HIDDEN_(nsIURLParser *) net_GetNoAuthURLParser(); michael@0: NS_HIDDEN_(nsIURLParser *) net_GetStdURLParser(); michael@0: michael@0: /* convert between nsIFile and file:// URL spec michael@0: * net_GetURLSpecFromFile does an extra stat, so callers should michael@0: * avoid it if possible in favor of net_GetURLSpecFromActualFile michael@0: * and net_GetURLSpecFromDir */ michael@0: NS_HIDDEN_(nsresult) net_GetURLSpecFromFile(nsIFile *, nsACString &); michael@0: NS_HIDDEN_(nsresult) net_GetURLSpecFromDir(nsIFile *, nsACString &); michael@0: NS_HIDDEN_(nsresult) net_GetURLSpecFromActualFile(nsIFile *, nsACString &); michael@0: NS_HIDDEN_(nsresult) net_GetFileFromURLSpec(const nsACString &, nsIFile **); michael@0: michael@0: /* extract file path components from file:// URL */ michael@0: NS_HIDDEN_(nsresult) net_ParseFileURL(const nsACString &inURL, michael@0: nsACString &outDirectory, michael@0: nsACString &outFileBaseName, michael@0: nsACString &outFileExtension); michael@0: michael@0: /* handle .. in dirs while resolving URLs (path is UTF-8) */ michael@0: NS_HIDDEN_(void) net_CoalesceDirs(netCoalesceFlags flags, char* path); michael@0: michael@0: /** michael@0: * Resolves a relative path string containing "." and ".." michael@0: * with respect to a base path (assumed to already be resolved). michael@0: * For example, resolving "../../foo/./bar/../baz.html" w.r.t. michael@0: * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to michael@0: * ascend above the base results in the NS_ERROR_MALFORMED_URI michael@0: * exception. If basePath is null, it treats it as "/". michael@0: * michael@0: * @param relativePath a relative URI michael@0: * @param basePath a base URI michael@0: * michael@0: * @return a new string, representing canonical uri michael@0: */ michael@0: NS_HIDDEN_(nsresult) net_ResolveRelativePath(const nsACString &relativePath, michael@0: const nsACString &basePath, michael@0: nsACString &result); michael@0: michael@0: /** michael@0: * Extract URI-Scheme if possible michael@0: * michael@0: * @param inURI URI spec michael@0: * @param startPos start of scheme (may be null) michael@0: * @param endPos end of scheme; index of colon (may be null) michael@0: * @param scheme scheme copied to this buffer on return (may be null) michael@0: */ michael@0: NS_HIDDEN_(nsresult) net_ExtractURLScheme(const nsACString &inURI, michael@0: uint32_t *startPos, michael@0: uint32_t *endPos, michael@0: nsACString *scheme = nullptr); michael@0: michael@0: /* check that the given scheme conforms to RFC 2396 */ michael@0: NS_HIDDEN_(bool) net_IsValidScheme(const char *scheme, uint32_t schemeLen); michael@0: michael@0: inline bool net_IsValidScheme(const nsAFlatCString &scheme) michael@0: { michael@0: return net_IsValidScheme(scheme.get(), scheme.Length()); michael@0: } michael@0: michael@0: /** michael@0: * Filter out whitespace from a URI string. The input is the |str| michael@0: * pointer. |result| is written to if and only if there is whitespace that has michael@0: * to be filtered out. The return value is true if and only if |result| is michael@0: * written to. michael@0: * michael@0: * This function strips out all whitespace at the beginning and end of the URL michael@0: * and strips out \r, \n, \t from the middle of the URL. This makes it safe to michael@0: * call on things like javascript: urls or data: urls, where we may in fact run michael@0: * into whitespace that is not properly encoded. Note that stripping does not michael@0: * occur in the scheme portion itself. michael@0: * michael@0: * @param str the pointer to the string to filter. Must be non-null. michael@0: * @param result the out param to write to if filtering happens michael@0: * @return whether result was written to michael@0: */ michael@0: NS_HIDDEN_(bool) net_FilterURIString(const char *str, nsACString& result); michael@0: michael@0: #if defined(XP_WIN) michael@0: /** michael@0: * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a michael@0: * forward-slash. This function maps any back-slashes to forward-slashes. michael@0: * michael@0: * @param aURL michael@0: * The URL string to normalize (UTF-8 encoded). This can be a michael@0: * relative URL segment. michael@0: * @param aResultBuf michael@0: * The resulting string is appended to this string. If the input URL michael@0: * is already normalized, then aResultBuf is unchanged. michael@0: * michael@0: * @returns false if aURL is already normalized. Otherwise, returns true. michael@0: */ michael@0: NS_HIDDEN_(bool) net_NormalizeFileURL(const nsACString &aURL, michael@0: nsCString &aResultBuf); michael@0: #endif michael@0: michael@0: /***************************************************************************** michael@0: * generic string routines follow (XXX move to someplace more generic). michael@0: */ michael@0: michael@0: /* convert to lower case */ michael@0: NS_HIDDEN_(void) net_ToLowerCase(char* str, uint32_t length); michael@0: NS_HIDDEN_(void) net_ToLowerCase(char* str); michael@0: michael@0: /** michael@0: * returns pointer to first character of |str| in the given set. if not found, michael@0: * then |end| is returned. stops prematurely if a null byte is encountered, michael@0: * and returns the address of the null byte. michael@0: */ michael@0: NS_HIDDEN_(char *) net_FindCharInSet(const char *str, const char *end, const char *set); michael@0: michael@0: /** michael@0: * returns pointer to first character of |str| NOT in the given set. if all michael@0: * characters are in the given set, then |end| is returned. if '\0' is not michael@0: * included in |set|, then stops prematurely if a null byte is encountered, michael@0: * and returns the address of the null byte. michael@0: */ michael@0: NS_HIDDEN_(char *) net_FindCharNotInSet(const char *str, const char *end, const char *set); michael@0: michael@0: /** michael@0: * returns pointer to last character of |str| NOT in the given set. if all michael@0: * characters are in the given set, then |str - 1| is returned. michael@0: */ michael@0: NS_HIDDEN_(char *) net_RFindCharNotInSet(const char *str, const char *end, const char *set); michael@0: michael@0: /** michael@0: * Parses a content-type header and returns the content type and michael@0: * charset (if any). aCharset is not modified if no charset is michael@0: * specified in anywhere in aHeaderStr. In that case (no charset michael@0: * specified), aHadCharset is set to false. Otherwise, it's set to michael@0: * true. Note that aContentCharset can be empty even if aHadCharset michael@0: * is true. michael@0: */ michael@0: NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr, michael@0: nsACString &aContentType, michael@0: nsACString &aContentCharset, michael@0: bool* aHadCharset); michael@0: /** michael@0: * As above, but also returns the start and end indexes for the charset michael@0: * parameter in aHeaderStr. These are indices for the entire parameter, NOT michael@0: * just the value. If there is "effectively" no charset parameter (e.g. if an michael@0: * earlier type with one is overridden by a later type without one), michael@0: * *aHadCharset will be true but *aCharsetStart will be set to -1. Note that michael@0: * it's possible to have aContentCharset empty and *aHadCharset true when michael@0: * *aCharsetStart is nonnegative; this corresponds to charset="". michael@0: */ michael@0: NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr, michael@0: nsACString &aContentType, michael@0: nsACString &aContentCharset, michael@0: bool *aHadCharset, michael@0: int32_t *aCharsetStart, michael@0: int32_t *aCharsetEnd); michael@0: michael@0: /* inline versions */ michael@0: michael@0: /* remember the 64-bit platforms ;-) */ michael@0: #define NET_MAX_ADDRESS (((char*)0)-1) michael@0: michael@0: inline char *net_FindCharInSet(const char *str, const char *set) michael@0: { michael@0: return net_FindCharInSet(str, NET_MAX_ADDRESS, set); michael@0: } michael@0: inline char *net_FindCharNotInSet(const char *str, const char *set) michael@0: { michael@0: return net_FindCharNotInSet(str, NET_MAX_ADDRESS, set); michael@0: } michael@0: inline char *net_RFindCharNotInSet(const char *str, const char *set) michael@0: { michael@0: return net_RFindCharNotInSet(str, str + strlen(str), set); michael@0: } michael@0: michael@0: /** michael@0: * This function returns true if the given hostname does not include any michael@0: * restricted characters. Otherwise, false is returned. michael@0: */ michael@0: NS_HIDDEN_(bool) net_IsValidHostName(const nsCSubstring &host); michael@0: michael@0: /** michael@0: * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2. michael@0: */ michael@0: NS_HIDDEN_(bool) net_IsValidIPv4Addr(const char *addr, int32_t addrLen); michael@0: michael@0: /** michael@0: * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2. michael@0: */ michael@0: NS_HIDDEN_(bool) net_IsValidIPv6Addr(const char *addr, int32_t addrLen); michael@0: michael@0: #endif // !nsURLHelper_h__