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