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