netwerk/base/src/nsURLHelper.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/base/src/nsURLHelper.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,230 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsURLHelper_h__
    1.10 +#define nsURLHelper_h__
    1.11 +
    1.12 +#include "nsString.h"
    1.13 +
    1.14 +class nsIFile;
    1.15 +class nsIURLParser;
    1.16 +
    1.17 +enum netCoalesceFlags
    1.18 +{
    1.19 +  NET_COALESCE_NORMAL = 0,
    1.20 +
    1.21 +  /**
    1.22 +   * retains /../ that reach above dir root (useful for FTP
    1.23 +   * servers in which the root of the FTP URL is not necessarily
    1.24 +   * the root of the FTP filesystem).
    1.25 +   */
    1.26 +  NET_COALESCE_ALLOW_RELATIVE_ROOT = 1<<0,
    1.27 +
    1.28 +  /**
    1.29 +   * recognizes /%2F and // as markers for the root directory
    1.30 +   * and handles them properly.
    1.31 +   */
    1.32 +  NET_COALESCE_DOUBLE_SLASH_IS_ROOT = 1<<1
    1.33 +};
    1.34 +
    1.35 +//----------------------------------------------------------------------------
    1.36 +// This module contains some private helper functions related to URL parsing.
    1.37 +//----------------------------------------------------------------------------
    1.38 +
    1.39 +/* shutdown frees URL parser */
    1.40 +NS_HIDDEN_(void) net_ShutdownURLHelper();
    1.41 +#ifdef XP_MACOSX
    1.42 +NS_HIDDEN_(void) net_ShutdownURLHelperOSX();
    1.43 +#endif
    1.44 +
    1.45 +/* access URL parsers */
    1.46 +NS_HIDDEN_(nsIURLParser *) net_GetAuthURLParser();
    1.47 +NS_HIDDEN_(nsIURLParser *) net_GetNoAuthURLParser();
    1.48 +NS_HIDDEN_(nsIURLParser *) net_GetStdURLParser();
    1.49 +
    1.50 +/* convert between nsIFile and file:// URL spec 
    1.51 + * net_GetURLSpecFromFile does an extra stat, so callers should
    1.52 + * avoid it if possible in favor of net_GetURLSpecFromActualFile
    1.53 + * and net_GetURLSpecFromDir */
    1.54 +NS_HIDDEN_(nsresult) net_GetURLSpecFromFile(nsIFile *, nsACString &);
    1.55 +NS_HIDDEN_(nsresult) net_GetURLSpecFromDir(nsIFile *, nsACString &);
    1.56 +NS_HIDDEN_(nsresult) net_GetURLSpecFromActualFile(nsIFile *, nsACString &);
    1.57 +NS_HIDDEN_(nsresult) net_GetFileFromURLSpec(const nsACString &, nsIFile **);
    1.58 +
    1.59 +/* extract file path components from file:// URL */
    1.60 +NS_HIDDEN_(nsresult) net_ParseFileURL(const nsACString &inURL,
    1.61 +                                      nsACString &outDirectory,
    1.62 +                                      nsACString &outFileBaseName,
    1.63 +                                      nsACString &outFileExtension);
    1.64 +
    1.65 +/* handle .. in dirs while resolving URLs (path is UTF-8) */
    1.66 +NS_HIDDEN_(void) net_CoalesceDirs(netCoalesceFlags flags, char* path);
    1.67 +
    1.68 +/**
    1.69 + * Resolves a relative path string containing "." and ".."
    1.70 + * with respect to a base path (assumed to already be resolved). 
    1.71 + * For example, resolving "../../foo/./bar/../baz.html" w.r.t.
    1.72 + * "/a/b/c/d/e/" yields "/a/b/c/foo/baz.html". Attempting to 
    1.73 + * ascend above the base results in the NS_ERROR_MALFORMED_URI
    1.74 + * exception. If basePath is null, it treats it as "/".
    1.75 + *
    1.76 + * @param relativePath  a relative URI
    1.77 + * @param basePath      a base URI
    1.78 + *
    1.79 + * @return a new string, representing canonical uri
    1.80 + */
    1.81 +NS_HIDDEN_(nsresult) net_ResolveRelativePath(const nsACString &relativePath,
    1.82 +                                             const nsACString &basePath,
    1.83 +                                             nsACString &result);
    1.84 +
    1.85 +/**
    1.86 + * Extract URI-Scheme if possible
    1.87 + *
    1.88 + * @param inURI     URI spec
    1.89 + * @param startPos  start of scheme (may be null)
    1.90 + * @param endPos    end of scheme; index of colon (may be null)
    1.91 + * @param scheme    scheme copied to this buffer on return (may be null)
    1.92 + */
    1.93 +NS_HIDDEN_(nsresult) net_ExtractURLScheme(const nsACString &inURI,
    1.94 +                                          uint32_t *startPos, 
    1.95 +                                          uint32_t *endPos,
    1.96 +                                          nsACString *scheme = nullptr);
    1.97 +
    1.98 +/* check that the given scheme conforms to RFC 2396 */
    1.99 +NS_HIDDEN_(bool) net_IsValidScheme(const char *scheme, uint32_t schemeLen);
   1.100 +
   1.101 +inline bool net_IsValidScheme(const nsAFlatCString &scheme)
   1.102 +{
   1.103 +    return net_IsValidScheme(scheme.get(), scheme.Length());
   1.104 +}
   1.105 +
   1.106 +/**
   1.107 + * Filter out whitespace from a URI string.  The input is the |str|
   1.108 + * pointer. |result| is written to if and only if there is whitespace that has
   1.109 + * to be filtered out.  The return value is true if and only if |result| is
   1.110 + * written to.
   1.111 + *
   1.112 + * This function strips out all whitespace at the beginning and end of the URL
   1.113 + * and strips out \r, \n, \t from the middle of the URL.  This makes it safe to
   1.114 + * call on things like javascript: urls or data: urls, where we may in fact run
   1.115 + * into whitespace that is not properly encoded.  Note that stripping does not
   1.116 + * occur in the scheme portion itself.
   1.117 + *
   1.118 + * @param str the pointer to the string to filter.  Must be non-null.
   1.119 + * @param result the out param to write to if filtering happens
   1.120 + * @return whether result was written to
   1.121 + */
   1.122 +NS_HIDDEN_(bool) net_FilterURIString(const char *str, nsACString& result);
   1.123 +
   1.124 +#if defined(XP_WIN)
   1.125 +/**
   1.126 + * On Win32 and OS/2 system's a back-slash in a file:// URL is equivalent to a
   1.127 + * forward-slash.  This function maps any back-slashes to forward-slashes.
   1.128 + *
   1.129 + * @param aURL
   1.130 + *        The URL string to normalize (UTF-8 encoded).  This can be a
   1.131 + *        relative URL segment.
   1.132 + * @param aResultBuf 
   1.133 + *        The resulting string is appended to this string.  If the input URL
   1.134 + *        is already normalized, then aResultBuf is unchanged.
   1.135 + *
   1.136 + * @returns false if aURL is already normalized.  Otherwise, returns true.
   1.137 + */
   1.138 +NS_HIDDEN_(bool) net_NormalizeFileURL(const nsACString &aURL,
   1.139 +                                        nsCString &aResultBuf);
   1.140 +#endif
   1.141 +
   1.142 +/*****************************************************************************
   1.143 + * generic string routines follow (XXX move to someplace more generic).
   1.144 + */
   1.145 +
   1.146 +/* convert to lower case */
   1.147 +NS_HIDDEN_(void) net_ToLowerCase(char* str, uint32_t length);
   1.148 +NS_HIDDEN_(void) net_ToLowerCase(char* str);
   1.149 +
   1.150 +/**
   1.151 + * returns pointer to first character of |str| in the given set.  if not found,
   1.152 + * then |end| is returned.  stops prematurely if a null byte is encountered,
   1.153 + * and returns the address of the null byte.
   1.154 + */
   1.155 +NS_HIDDEN_(char *) net_FindCharInSet(const char *str, const char *end, const char *set);
   1.156 +
   1.157 +/**
   1.158 + * returns pointer to first character of |str| NOT in the given set.  if all
   1.159 + * characters are in the given set, then |end| is returned.  if '\0' is not
   1.160 + * included in |set|, then stops prematurely if a null byte is encountered,
   1.161 + * and returns the address of the null byte.
   1.162 + */
   1.163 +NS_HIDDEN_(char *) net_FindCharNotInSet(const char *str, const char *end, const char *set);
   1.164 +
   1.165 +/**
   1.166 + * returns pointer to last character of |str| NOT in the given set.  if all
   1.167 + * characters are in the given set, then |str - 1| is returned.
   1.168 + */
   1.169 +NS_HIDDEN_(char *) net_RFindCharNotInSet(const char *str, const char *end, const char *set);
   1.170 +
   1.171 +/**
   1.172 + * Parses a content-type header and returns the content type and
   1.173 + * charset (if any).  aCharset is not modified if no charset is
   1.174 + * specified in anywhere in aHeaderStr.  In that case (no charset
   1.175 + * specified), aHadCharset is set to false.  Otherwise, it's set to
   1.176 + * true.  Note that aContentCharset can be empty even if aHadCharset
   1.177 + * is true.
   1.178 + */
   1.179 +NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
   1.180 +                                      nsACString       &aContentType,
   1.181 +                                      nsACString       &aContentCharset,
   1.182 +                                      bool*          aHadCharset);
   1.183 +/**
   1.184 + * As above, but also returns the start and end indexes for the charset
   1.185 + * parameter in aHeaderStr.  These are indices for the entire parameter, NOT
   1.186 + * just the value.  If there is "effectively" no charset parameter (e.g. if an
   1.187 + * earlier type with one is overridden by a later type without one),
   1.188 + * *aHadCharset will be true but *aCharsetStart will be set to -1.  Note that
   1.189 + * it's possible to have aContentCharset empty and *aHadCharset true when
   1.190 + * *aCharsetStart is nonnegative; this corresponds to charset="".
   1.191 + */
   1.192 +NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
   1.193 +                                      nsACString       &aContentType,
   1.194 +                                      nsACString       &aContentCharset,
   1.195 +                                      bool             *aHadCharset,
   1.196 +                                      int32_t          *aCharsetStart,
   1.197 +                                      int32_t          *aCharsetEnd);
   1.198 +
   1.199 +/* inline versions */
   1.200 +
   1.201 +/* remember the 64-bit platforms ;-) */
   1.202 +#define NET_MAX_ADDRESS (((char*)0)-1)
   1.203 +
   1.204 +inline char *net_FindCharInSet(const char *str, const char *set)
   1.205 +{
   1.206 +    return net_FindCharInSet(str, NET_MAX_ADDRESS, set);
   1.207 +}
   1.208 +inline char *net_FindCharNotInSet(const char *str, const char *set)
   1.209 +{
   1.210 +    return net_FindCharNotInSet(str, NET_MAX_ADDRESS, set);
   1.211 +}
   1.212 +inline char *net_RFindCharNotInSet(const char *str, const char *set)
   1.213 +{
   1.214 +    return net_RFindCharNotInSet(str, str + strlen(str), set);
   1.215 +}
   1.216 +
   1.217 +/**
   1.218 + * This function returns true if the given hostname does not include any
   1.219 + * restricted characters.  Otherwise, false is returned.
   1.220 + */
   1.221 +NS_HIDDEN_(bool) net_IsValidHostName(const nsCSubstring &host);
   1.222 +
   1.223 +/**
   1.224 + * Checks whether the IPv4 address is valid according to RFC 3986 section 3.2.2.
   1.225 + */
   1.226 +NS_HIDDEN_(bool) net_IsValidIPv4Addr(const char *addr, int32_t addrLen);
   1.227 +
   1.228 +/**
   1.229 + * Checks whether the IPv6 address is valid according to RFC 3986 section 3.2.2.
   1.230 + */
   1.231 +NS_HIDDEN_(bool) net_IsValidIPv6Addr(const char *addr, int32_t addrLen);
   1.232 +
   1.233 +#endif // !nsURLHelper_h__

mercurial