url_util.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This file contains a set of utility functions related to parsing,
  5. // manipulating, and interacting with URLs and hostnames. These functions are
  6. // intended to be of a text-processing nature, and should not attempt to use any
  7. // networking or blocking services.
  8. #ifndef NET_BASE_URL_UTIL_H_
  9. #define NET_BASE_URL_UTIL_H_
  10. #include <string>
  11. #include "base/strings/string_piece.h"
  12. #include "net/base/net_export.h"
  13. #include "url/third_party/mozilla/url_parse.h"
  14. class GURL;
  15. namespace url {
  16. struct CanonHostInfo;
  17. class SchemeHostPort;
  18. }
  19. namespace net {
  20. // Returns a new GURL by appending the given query parameter name and the
  21. // value. Unsafe characters in the name and the value are escaped like
  22. // %XX%XX. The original query component is preserved if it's present.
  23. //
  24. // Examples:
  25. //
  26. // AppendQueryParameter(GURL("http://example.com"), "name", "value").spec()
  27. // => "http://example.com?name=value"
  28. // AppendQueryParameter(GURL("http://example.com?x=y"), "name", "value").spec()
  29. // => "http://example.com?x=y&name=value"
  30. NET_EXPORT GURL AppendQueryParameter(const GURL& url,
  31. const std::string& name,
  32. const std::string& value);
  33. // Returns a new GURL by appending or replacing the given query parameter name
  34. // and the value. If |name| appears more than once, only the first name-value
  35. // pair is replaced. Unsafe characters in the name and the value are escaped
  36. // like %XX%XX. The original query component is preserved if it's present.
  37. //
  38. // Examples:
  39. //
  40. // AppendOrReplaceQueryParameter(
  41. // GURL("http://example.com"), "name", "new").spec()
  42. // => "http://example.com?name=value"
  43. // AppendOrReplaceQueryParameter(
  44. // GURL("http://example.com?x=y&name=old"), "name", "new").spec()
  45. // => "http://example.com?x=y&name=new"
  46. NET_EXPORT GURL AppendOrReplaceQueryParameter(const GURL& url,
  47. const std::string& name,
  48. const std::string& value);
  49. // Iterates over the key-value pairs in the query portion of |url|.
  50. // NOTE: QueryIterator stores reference to |url| and creates base::StringPiece
  51. // instances which refer to the data inside |url| query. Therefore |url| must
  52. // outlive QueryIterator and all base::StringPiece objects returned from GetKey
  53. // and GetValue methods.
  54. class NET_EXPORT QueryIterator {
  55. public:
  56. explicit QueryIterator(const GURL& url);
  57. QueryIterator(const QueryIterator&) = delete;
  58. QueryIterator& operator=(const QueryIterator&) = delete;
  59. ~QueryIterator();
  60. base::StringPiece GetKey() const;
  61. base::StringPiece GetValue() const;
  62. const std::string& GetUnescapedValue();
  63. bool IsAtEnd() const;
  64. void Advance();
  65. private:
  66. const GURL& url_;
  67. url::Component query_;
  68. bool at_end_;
  69. url::Component key_;
  70. url::Component value_;
  71. std::string unescaped_value_;
  72. };
  73. // Looks for |search_key| in the query portion of |url|. Returns true if the
  74. // key is found and sets |out_value| to the unescaped value for the key.
  75. // Returns false if the key is not found.
  76. NET_EXPORT bool GetValueForKeyInQuery(const GURL& url,
  77. const std::string& search_key,
  78. std::string* out_value);
  79. // Splits an input of the form <host>[":"<port>] into its consitituent parts.
  80. // Saves the result into |*host| and |*port|. If the input did not have
  81. // the optional port, sets |*port| to -1.
  82. // Returns true if the parsing was successful, false otherwise.
  83. // The returned host is NOT canonicalized, and may be invalid.
  84. //
  85. // IPv6 literals must be specified in a bracketed form, for instance:
  86. // [::1]:90 and [::1]
  87. //
  88. // The resultant |*host| in both cases will be "::1" (not bracketed).
  89. NET_EXPORT bool ParseHostAndPort(base::StringPiece input,
  90. std::string* host,
  91. int* port);
  92. // Returns a host:port string for the given URL.
  93. NET_EXPORT std::string GetHostAndPort(const GURL& url);
  94. // Returns a host[:port] string for the given URL, where the port is omitted
  95. // if it is the default for the URL's scheme.
  96. NET_EXPORT std::string GetHostAndOptionalPort(const GURL& url);
  97. // Just like above, but takes a SchemeHostPort.
  98. NET_EXPORT std::string GetHostAndOptionalPort(
  99. const url::SchemeHostPort& scheme_host_port);
  100. // Returns the hostname by trimming the ending dot, if one exists.
  101. NET_EXPORT std::string TrimEndingDot(base::StringPiece host);
  102. // Returns either the host from |url|, or, if the host is empty, the full spec.
  103. NET_EXPORT std::string GetHostOrSpecFromURL(const GURL& url);
  104. // Returns the given domain minus its leftmost label, or the empty string if the
  105. // given domain is just a single label. For normal domain names (not IP
  106. // addresses), this represents the "superdomain" of the given domain.
  107. // Note that this does not take into account anything like the Public Suffix
  108. // List, so the superdomain may end up being a bare eTLD. The returned string is
  109. // not guaranteed to be a valid or canonical hostname, or to make any sense at
  110. // all.
  111. //
  112. // Examples:
  113. //
  114. // GetSuperdomain("assets.example.com") -> "example.com"
  115. // GetSuperdomain("example.net") -> "net"
  116. // GetSuperdomain("littlebox") -> ""
  117. // GetSuperdomain("127.0.0.1") -> "0.0.1"
  118. NET_EXPORT std::string GetSuperdomain(base::StringPiece domain);
  119. // Returns whether |subdomain| is a subdomain of (or identical to)
  120. // |superdomain|, if both are hostnames (not IP addresses -- for which this
  121. // function is nonsensical). Does not consider the Public Suffix List.
  122. // Returns true if both input strings are empty.
  123. NET_EXPORT bool IsSubdomainOf(base::StringPiece subdomain,
  124. base::StringPiece superdomain);
  125. // Canonicalizes |host| and returns it. Also fills |host_info| with
  126. // IP address information. |host_info| must not be NULL.
  127. NET_EXPORT std::string CanonicalizeHost(base::StringPiece host,
  128. url::CanonHostInfo* host_info);
  129. // Returns true if |host| is not an IP address and is compliant with a set of
  130. // rules based on RFC 1738 and tweaked to be compatible with the real world.
  131. // The rules are:
  132. // * One or more components separated by '.'
  133. // * Each component contains only alphanumeric characters and '-' or '_'
  134. // * The last component begins with an alphanumeric character
  135. // * Optional trailing dot after last component (means "treat as FQDN")
  136. //
  137. // NOTE: You should only pass in hosts that have been returned from
  138. // CanonicalizeHost(), or you may not get accurate results.
  139. NET_EXPORT bool IsCanonicalizedHostCompliant(const std::string& host);
  140. // Returns true if |hostname| contains a non-registerable or non-assignable
  141. // domain name (eg: a gTLD that has not been assigned by IANA) or an IP address
  142. // that falls in an range reserved for non-publicly routable networks.
  143. NET_EXPORT bool IsHostnameNonUnique(const std::string& hostname);
  144. // Returns true if the host part of |url| is a local host name according to
  145. // HostStringIsLocalhost.
  146. NET_EXPORT bool IsLocalhost(const GURL& url);
  147. // Returns true if |host| is one of the local hostnames
  148. // (e.g. "localhost") or IP addresses (IPv4 127.0.0.0/8 or IPv6 ::1).
  149. // "[::1]" is not detected as a local hostname. Do not use this method to check
  150. // whether the host part of a URL is a local host name; use IsLocalhost instead.
  151. //
  152. // Note that this function does not check for IP addresses other than
  153. // the above, although other IP addresses may point to the local
  154. // machine.
  155. NET_EXPORT bool HostStringIsLocalhost(base::StringPiece host);
  156. // Strip the portions of |url| that aren't core to the network request.
  157. // - user name / password
  158. // - reference section
  159. NET_EXPORT GURL SimplifyUrlForRequest(const GURL& url);
  160. // Changes scheme "ws" to "http" and "wss" to "https". This is useful for origin
  161. // checks and authentication, where WebSocket URLs are treated as if they were
  162. // HTTP. It is an error to call this function with a url with a scheme other
  163. // than "ws" or "wss".
  164. NET_EXPORT GURL ChangeWebSocketSchemeToHttpScheme(const GURL& url);
  165. // Returns whether the given url scheme is of a standard scheme type that can
  166. // have hostnames representing domains (i.e. network hosts).
  167. // See url::SchemeType.
  168. NET_EXPORT bool IsStandardSchemeWithNetworkHost(base::StringPiece scheme);
  169. // Extracts the unescaped username/password from |url|, saving the results
  170. // into |*username| and |*password|.
  171. NET_EXPORT_PRIVATE void GetIdentityFromURL(const GURL& url,
  172. std::u16string* username,
  173. std::u16string* password);
  174. // Returns true if the url's host is a Google server. This should only be used
  175. // for histograms and shouldn't be used to affect behavior.
  176. NET_EXPORT_PRIVATE bool HasGoogleHost(const GURL& url);
  177. // Returns true if |host| is the hostname of a Google server. This should only
  178. // be used for histograms and shouldn't be used to affect behavior.
  179. NET_EXPORT_PRIVATE bool IsGoogleHost(base::StringPiece host);
  180. // This function tests |host| to see if it is of any local hostname form.
  181. // |host| is normalized before being tested.
  182. NET_EXPORT_PRIVATE bool IsLocalHostname(base::StringPiece host);
  183. } // namespace net
  184. #endif // NET_BASE_URL_UTIL_H_