Home | History | Annotate | Download | only in src
      1 // Copyright 2007, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 #ifndef GOOGLEURL_SRC_GURL_H__
     31 #define GOOGLEURL_SRC_GURL_H__
     32 
     33 #include <iosfwd>
     34 #include <string>
     35 
     36 #include "base/string16.h"
     37 #include "googleurl/src/url_canon.h"
     38 #include "googleurl/src/url_canon_stdstring.h"
     39 #include "googleurl/src/url_common.h"
     40 #include "googleurl/src/url_parse.h"
     41 
     42 class GURL {
     43  public:
     44   typedef url_canon::StdStringReplacements<std::string> Replacements;
     45   typedef url_canon::StdStringReplacements<string16> ReplacementsW;
     46 
     47   // Creates an empty, invalid URL.
     48   GURL_API GURL();
     49 
     50   // Copy construction is relatively inexpensive, with most of the time going
     51   // to reallocating the string. It does not re-parse.
     52   GURL_API GURL(const GURL& other);
     53 
     54   // The narrow version requires the input be UTF-8. Invalid UTF-8 input will
     55   // result in an invalid URL.
     56   //
     57   // The wide version should also take an encoding parameter so we know how to
     58   // encode the query parameters. It is probably sufficient for the narrow
     59   // version to assume the query parameter encoding should be the same as the
     60   // input encoding.
     61   GURL_API explicit GURL(const std::string& url_string
     62                          /*, output_param_encoding*/);
     63   GURL_API explicit GURL(const string16& url_string
     64                          /*, output_param_encoding*/);
     65 
     66   // Constructor for URLs that have already been parsed and canonicalized. This
     67   // is used for conversions from KURL, for example. The caller must supply all
     68   // information associated with the URL, which must be correct and consistent.
     69   GURL_API GURL(const char* canonical_spec, size_t canonical_spec_len,
     70                 const url_parse::Parsed& parsed, bool is_valid);
     71 
     72   GURL_API GURL& operator=(const GURL& other);
     73 
     74   // Returns true when this object represents a valid parsed URL. When not
     75   // valid, other functions will still succeed, but you will not get canonical
     76   // data out in the format you may be expecting. Instead, we keep something
     77   // "reasonable looking" so that the user can see how it's busted if
     78   // displayed to them.
     79   bool is_valid() const {
     80     return is_valid_;
     81   }
     82 
     83   // Returns true if the URL is zero-length. Note that empty URLs are also
     84   // invalid, and is_valid() will return false for them. This is provided
     85   // because some users may want to treat the empty case differently.
     86   bool is_empty() const {
     87     return spec_.empty();
     88   }
     89 
     90   // Returns the raw spec, i.e., the full text of the URL, in canonical UTF-8,
     91   // if the URL is valid. If the URL is not valid, this will assert and return
     92   // the empty string (for safety in release builds, to keep them from being
     93   // misused which might be a security problem).
     94   //
     95   // The URL will be ASCII except the reference fragment, which may be UTF-8.
     96   // It is guaranteed to be valid UTF-8.
     97   //
     98   // The exception is for empty() URLs (which are !is_valid()) but this will
     99   // return the empty string without asserting.
    100   //
    101   // Used invalid_spec() below to get the unusable spec of an invalid URL. This
    102   // separation is designed to prevent errors that may cause security problems
    103   // that could result from the mistaken use of an invalid URL.
    104   GURL_API const std::string& spec() const;
    105 
    106   // Returns the potentially invalid spec for a the URL. This spec MUST NOT be
    107   // modified or sent over the network. It is designed to be displayed in error
    108   // messages to the user, as the apperance of the spec may explain the error.
    109   // If the spec is valid, the valid spec will be returned.
    110   //
    111   // The returned string is guaranteed to be valid UTF-8.
    112   const std::string& possibly_invalid_spec() const {
    113     return spec_;
    114   }
    115 
    116   // Getter for the raw parsed structure. This allows callers to locate parts
    117   // of the URL within the spec themselves. Most callers should consider using
    118   // the individual component getters below.
    119   //
    120   // The returned parsed structure will reference into the raw spec, which may
    121   // or may not be valid. If you are using this to index into the spec, BE
    122   // SURE YOU ARE USING possibly_invalid_spec() to get the spec, and that you
    123   // don't do anything "important" with invalid specs.
    124   const url_parse::Parsed& parsed_for_possibly_invalid_spec() const {
    125     return parsed_;
    126   }
    127 
    128   // Defiant equality operator!
    129   bool operator==(const GURL& other) const {
    130     return spec_ == other.spec_;
    131   }
    132   bool operator!=(const GURL& other) const {
    133     return spec_ != other.spec_;
    134   }
    135 
    136   // Allows GURL to used as a key in STL (for example, a std::set or std::map).
    137   bool operator<(const GURL& other) const {
    138     return spec_ < other.spec_;
    139   }
    140 
    141   // Resolves a URL that's possibly relative to this object's URL, and returns
    142   // it. Absolute URLs are also handled according to the rules of URLs on web
    143   // pages.
    144   //
    145   // It may be impossible to resolve the URLs properly. If the input is not
    146   // "standard" (SchemeIsStandard() == false) and the input looks relative, we
    147   // can't resolve it. In these cases, the result will be an empty, invalid
    148   // GURL.
    149   //
    150   // The result may also be a nonempty, invalid URL if the input has some kind
    151   // of encoding error. In these cases, we will try to construct a "good" URL
    152   // that may have meaning to the user, but it will be marked invalid.
    153   //
    154   // It is an error to resolve a URL relative to an invalid URL. The result
    155   // will be the empty URL.
    156   GURL_API GURL Resolve(const std::string& relative) const;
    157   GURL_API GURL Resolve(const string16& relative) const;
    158 
    159   // Like Resolve() above but takes a character set encoder which will be used
    160   // for any query text specified in the input. The charset converter parameter
    161   // may be NULL, in which case it will be treated as UTF-8.
    162   //
    163   // TODO(brettw): These should be replaced with versions that take something
    164   // more friendly than a raw CharsetConverter (maybe like an ICU character set
    165   // name).
    166   GURL_API GURL ResolveWithCharsetConverter(
    167       const std::string& relative,
    168       url_canon::CharsetConverter* charset_converter) const;
    169   GURL_API GURL ResolveWithCharsetConverter(
    170       const string16& relative,
    171       url_canon::CharsetConverter* charset_converter) const;
    172 
    173   // Creates a new GURL by replacing the current URL's components with the
    174   // supplied versions. See the Replacements class in url_canon.h for more.
    175   //
    176   // These are not particularly quick, so avoid doing mutations when possible.
    177   // Prefer the 8-bit version when possible.
    178   //
    179   // It is an error to replace components of an invalid URL. The result will
    180   // be the empty URL.
    181   //
    182   // Note that we use the more general url_canon::Replacements type to give
    183   // callers extra flexibility rather than our override.
    184   GURL_API GURL ReplaceComponents(
    185       const url_canon::Replacements<char>& replacements) const;
    186   GURL_API GURL ReplaceComponents(
    187       const url_canon::Replacements<char16>& replacements) const;
    188 
    189   // A helper function that is equivalent to replacing the path with a slash
    190   // and clearing out everything after that. We sometimes need to know just the
    191   // scheme and the authority. If this URL is not a standard URL (it doesn't
    192   // have the regular authority and path sections), then the result will be
    193   // an empty, invalid GURL. Note that this *does* work for file: URLs, which
    194   // some callers may want to filter out before calling this.
    195   //
    196   // It is an error to get an empty path on an invalid URL. The result
    197   // will be the empty URL.
    198   GURL_API GURL GetWithEmptyPath() const;
    199 
    200   // A helper function to return a GURL containing just the scheme, host,
    201   // and port from a URL. Equivalent to clearing any username and password,
    202   // replacing the path with a slash, and clearing everything after that. If
    203   // this URL is not a standard URL, then the result will be an empty,
    204   // invalid GURL. If the URL has neither username nor password, this
    205   // degenerates to GetWithEmptyPath().
    206   //
    207   // It is an error to get the origin of an invalid URL. The result
    208   // will be the empty URL.
    209   GURL_API GURL GetOrigin() const;
    210 
    211   // Returns true if the scheme for the current URL is a known "standard"
    212   // scheme. Standard schemes have an authority and a path section. This
    213   // includes file:, which some callers may want to filter out explicitly by
    214   // calling SchemeIsFile.
    215   GURL_API bool IsStandard() const;
    216 
    217   // Returns true if the given parameter (should be lower-case ASCII to match
    218   // the canonicalized scheme) is the scheme for this URL. This call is more
    219   // efficient than getting the scheme and comparing it because no copies or
    220   // object constructions are done.
    221   GURL_API bool SchemeIs(const char* lower_ascii_scheme) const;
    222 
    223   // We often need to know if this is a file URL. File URLs are "standard", but
    224   // are often treated separately by some programs.
    225   bool SchemeIsFile() const {
    226     return SchemeIs("file");
    227   }
    228 
    229   // If the scheme indicates a secure connection
    230   bool SchemeIsSecure() const {
    231     return SchemeIs("https");
    232   }
    233 
    234   // Returns true if the hostname is an IP address. Note: this function isn't
    235   // as cheap as a simple getter because it re-parses the hostname to verify.
    236   // This currently identifies only IPv4 addresses (bug 822685).
    237   GURL_API bool HostIsIPAddress() const;
    238 
    239   // Getters for various components of the URL. The returned string will be
    240   // empty if the component is empty or is not present.
    241   std::string scheme() const {  // Not including the colon. See also SchemeIs.
    242     return ComponentString(parsed_.scheme);
    243   }
    244   std::string username() const {
    245     return ComponentString(parsed_.username);
    246   }
    247   std::string password() const {
    248     return ComponentString(parsed_.password);
    249   }
    250   // Note that this may be a hostname, an IPv4 address, or an IPv6 literal
    251   // surrounded by square brackets, like "[2001:db8::1]".  To exclude these
    252   // brackets, use HostNoBrackets() below.
    253   std::string host() const {
    254     return ComponentString(parsed_.host);
    255   }
    256   std::string port() const {  // Returns -1 if "default"
    257     return ComponentString(parsed_.port);
    258   }
    259   std::string path() const {  // Including first slash following host
    260     return ComponentString(parsed_.path);
    261   }
    262   std::string query() const {  // Stuff following '?'
    263     return ComponentString(parsed_.query);
    264   }
    265   std::string ref() const {  // Stuff following '#'
    266     return ComponentString(parsed_.ref);
    267   }
    268 
    269   // Existance querying. These functions will return true if the corresponding
    270   // URL component exists in this URL. Note that existance is different than
    271   // being nonempty. http://www.google.com/? has a query that just happens to
    272   // be empty, and has_query() will return true.
    273   bool has_scheme() const {
    274     return parsed_.scheme.len >= 0;
    275   }
    276   bool has_username() const {
    277     return parsed_.username.len >= 0;
    278   }
    279   bool has_password() const {
    280     return parsed_.password.len >= 0;
    281   }
    282   bool has_host() const {
    283     // Note that hosts are special, absense of host means length 0.
    284     return parsed_.host.len > 0;
    285   }
    286   bool has_port() const {
    287     return parsed_.port.len >= 0;
    288   }
    289   bool has_path() const {
    290     // Note that http://www.google.com/" has a path, the path is "/". This can
    291     // return false only for invalid or nonstandard URLs.
    292     return parsed_.path.len >= 0;
    293   }
    294   bool has_query() const {
    295     return parsed_.query.len >= 0;
    296   }
    297   bool has_ref() const {
    298     return parsed_.ref.len >= 0;
    299   }
    300 
    301   // Returns a parsed version of the port. Can also be any of the special
    302   // values defined in Parsed for ExtractPort.
    303   GURL_API int IntPort() const;
    304 
    305   // Returns the port number of the url, or the default port number.
    306   // If the scheme has no concept of port (or unknown default) returns
    307   // PORT_UNSPECIFIED.
    308   GURL_API int EffectiveIntPort() const;
    309 
    310   // Extracts the filename portion of the path and returns it. The filename
    311   // is everything after the last slash in the path. This may be empty.
    312   GURL_API std::string ExtractFileName() const;
    313 
    314   // Returns the path that should be sent to the server. This is the path,
    315   // parameter, and query portions of the URL. It is guaranteed to be ASCII.
    316   GURL_API std::string PathForRequest() const;
    317 
    318   // Returns the host, excluding the square brackets surrounding IPv6 address
    319   // literals.  This can be useful for passing to getaddrinfo().
    320   GURL_API std::string HostNoBrackets() const;
    321 
    322   // Returns true if this URL's host matches or is in the same domain as
    323   // the given input string. For example if this URL was "www.google.com",
    324   // this would match "com", "google.com", and "www.google.com
    325   // (input domain should be lower-case ASCII to match the canonicalized
    326   // scheme). This call is more efficient than getting the host and check
    327   // whether host has the specific domain or not because no copies or
    328   // object constructions are done.
    329   //
    330   // If function DomainIs has parameter domain_len, which means the parameter
    331   // lower_ascii_domain does not gurantee to terminate with NULL character.
    332   GURL_API bool DomainIs(const char* lower_ascii_domain, int domain_len) const;
    333 
    334   // If function DomainIs only has parameter lower_ascii_domain, which means
    335   // domain string should be terminate with NULL character.
    336   bool DomainIs(const char* lower_ascii_domain) const {
    337     return DomainIs(lower_ascii_domain,
    338                     static_cast<int>(strlen(lower_ascii_domain)));
    339   }
    340 
    341   // Swaps the contents of this GURL object with the argument without doing
    342   // any memory allocations.
    343   GURL_API void Swap(GURL* other);
    344 
    345   // Returns a reference to a singleton empty GURL. This object is for callers
    346   // who return references but don't have anything to return in some cases.
    347   // This function may be called from any thread.
    348   GURL_API static const GURL& EmptyGURL();
    349 
    350  private:
    351   // Returns the substring of the input identified by the given component.
    352   std::string ComponentString(const url_parse::Component& comp) const {
    353     if (comp.len <= 0)
    354       return std::string();
    355     return std::string(spec_, comp.begin, comp.len);
    356   }
    357 
    358   // The actual text of the URL, in canonical ASCII form.
    359   std::string spec_;
    360 
    361   // Set when the given URL is valid. Otherwise, we may still have a spec and
    362   // components, but they may not identify valid resources (for example, an
    363   // invalid port number, invalid characters in the scheme, etc.).
    364   bool is_valid_;
    365 
    366   // Identified components of the canonical spec.
    367   url_parse::Parsed parsed_;
    368 
    369   // TODO bug 684583: Add encoding for query params.
    370 };
    371 
    372 // Stream operator so GURL can be used in assertion statements.
    373 GURL_API std::ostream& operator<<(std::ostream& out, const GURL& url);
    374 
    375 #endif  // GOOGLEURL_SRC_GURL_H__
    376