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#ifdef WIN32
6#include <windows.h>
7#else
8#include <pthread.h>
9#endif
10
11#include <algorithm>
12#include <ostream>
13
14#include "url/gurl.h"
15
16#include "base/logging.h"
17#include "url/url_canon_stdstring.h"
18#include "url/url_util.h"
19
20namespace {
21
22static std::string* empty_string = NULL;
23static GURL* empty_gurl = NULL;
24
25#ifdef WIN32
26
27// Returns a static reference to an empty string for returning a reference
28// when there is no underlying string.
29const std::string& EmptyStringForGURL() {
30  // Avoid static object construction/destruction on startup/shutdown.
31  if (!empty_string) {
32    // Create the string. Be careful that we don't break in the case that this
33    // is being called from multiple threads. Statics are not threadsafe.
34    std::string* new_empty_string = new std::string;
35    if (InterlockedCompareExchangePointer(
36        reinterpret_cast<PVOID*>(&empty_string), new_empty_string, NULL)) {
37      // The old value was non-NULL, so no replacement was done. Another
38      // thread did the initialization out from under us.
39      delete new_empty_string;
40    }
41  }
42  return *empty_string;
43}
44
45#else
46
47static pthread_once_t empty_string_once = PTHREAD_ONCE_INIT;
48static pthread_once_t empty_gurl_once = PTHREAD_ONCE_INIT;
49
50void EmptyStringForGURLOnce(void) {
51  empty_string = new std::string;
52}
53
54const std::string& EmptyStringForGURL() {
55  // Avoid static object construction/destruction on startup/shutdown.
56  pthread_once(&empty_string_once, EmptyStringForGURLOnce);
57  return *empty_string;
58}
59
60#endif  // WIN32
61
62} // namespace
63
64GURL::GURL() : is_valid_(false) {
65}
66
67GURL::GURL(const GURL& other)
68    : spec_(other.spec_),
69      is_valid_(other.is_valid_),
70      parsed_(other.parsed_) {
71  if (other.inner_url_)
72    inner_url_.reset(new GURL(*other.inner_url_));
73  // Valid filesystem urls should always have an inner_url_.
74  DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
75}
76
77GURL::GURL(const std::string& url_string) {
78  InitCanonical(url_string, true);
79}
80
81GURL::GURL(const base::string16& url_string) {
82  InitCanonical(url_string, true);
83}
84
85GURL::GURL(const std::string& url_string, RetainWhiteSpaceSelector) {
86  InitCanonical(url_string, false);
87}
88
89GURL::GURL(const char* canonical_spec,
90           size_t canonical_spec_len,
91           const url::Parsed& parsed,
92           bool is_valid)
93    : spec_(canonical_spec, canonical_spec_len),
94      is_valid_(is_valid),
95      parsed_(parsed) {
96  InitializeFromCanonicalSpec();
97}
98
99GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid)
100    : is_valid_(is_valid),
101      parsed_(parsed) {
102  spec_.swap(canonical_spec);
103  InitializeFromCanonicalSpec();
104}
105
106template<typename STR>
107void GURL::InitCanonical(const STR& input_spec, bool trim_path_end) {
108  // Reserve enough room in the output for the input, plus some extra so that
109  // we have room if we have to escape a few things without reallocating.
110  spec_.reserve(input_spec.size() + 32);
111  url::StdStringCanonOutput output(&spec_);
112  is_valid_ = url::Canonicalize(
113      input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end,
114      NULL, &output, &parsed_);
115
116  output.Complete();  // Must be done before using string.
117  if (is_valid_ && SchemeIsFileSystem()) {
118    inner_url_.reset(new GURL(spec_.data(), parsed_.Length(),
119                              *parsed_.inner_parsed(), true));
120  }
121}
122
123void GURL::InitializeFromCanonicalSpec() {
124  if (is_valid_ && SchemeIsFileSystem()) {
125    inner_url_.reset(
126        new GURL(spec_.data(), parsed_.Length(),
127                 *parsed_.inner_parsed(), true));
128  }
129
130#ifndef NDEBUG
131  // For testing purposes, check that the parsed canonical URL is identical to
132  // what we would have produced. Skip checking for invalid URLs have no meaning
133  // and we can't always canonicalize then reproducabely.
134  if (is_valid_) {
135    url::Component scheme;
136    // We can't do this check on the inner_url of a filesystem URL, as
137    // canonical_spec actually points to the start of the outer URL, so we'd
138    // end up with infinite recursion in this constructor.
139    if (!url::FindAndCompareScheme(spec_.data(), spec_.length(),
140                                   url::kFileSystemScheme, &scheme) ||
141        scheme.begin == parsed_.scheme.begin) {
142      // We need to retain trailing whitespace on path URLs, as the |parsed_|
143      // spec we originally received may legitimately contain trailing white-
144      // space on the path or  components e.g. if the #ref has been
145      // removed from a "foo:hello #ref" URL (see http://crbug.com/291747).
146      GURL test_url(spec_, RETAIN_TRAILING_PATH_WHITEPACE);
147
148      DCHECK(test_url.is_valid_ == is_valid_);
149      DCHECK(test_url.spec_ == spec_);
150
151      DCHECK(test_url.parsed_.scheme == parsed_.scheme);
152      DCHECK(test_url.parsed_.username == parsed_.username);
153      DCHECK(test_url.parsed_.password == parsed_.password);
154      DCHECK(test_url.parsed_.host == parsed_.host);
155      DCHECK(test_url.parsed_.port == parsed_.port);
156      DCHECK(test_url.parsed_.path == parsed_.path);
157      DCHECK(test_url.parsed_.query == parsed_.query);
158      DCHECK(test_url.parsed_.ref == parsed_.ref);
159    }
160  }
161#endif
162}
163
164GURL::~GURL() {
165}
166
167GURL& GURL::operator=(GURL other) {
168  Swap(&other);
169  return *this;
170}
171
172const std::string& GURL::spec() const {
173  if (is_valid_ || spec_.empty())
174    return spec_;
175
176  DCHECK(false) << "Trying to get the spec of an invalid URL!";
177  return EmptyStringForGURL();
178}
179
180GURL GURL::Resolve(const std::string& relative) const {
181  return ResolveWithCharsetConverter(relative, NULL);
182}
183GURL GURL::Resolve(const base::string16& relative) const {
184  return ResolveWithCharsetConverter(relative, NULL);
185}
186
187// Note: code duplicated below (it's inconvenient to use a template here).
188GURL GURL::ResolveWithCharsetConverter(
189    const std::string& relative,
190    url::CharsetConverter* charset_converter) const {
191  // Not allowed for invalid URLs.
192  if (!is_valid_)
193    return GURL();
194
195  GURL result;
196
197  // Reserve enough room in the output for the input, plus some extra so that
198  // we have room if we have to escape a few things without reallocating.
199  result.spec_.reserve(spec_.size() + 32);
200  url::StdStringCanonOutput output(&result.spec_);
201
202  if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
203                            parsed_, relative.data(),
204                            static_cast<int>(relative.length()),
205                            charset_converter, &output, &result.parsed_)) {
206    // Error resolving, return an empty URL.
207    return GURL();
208  }
209
210  output.Complete();
211  result.is_valid_ = true;
212  if (result.SchemeIsFileSystem()) {
213    result.inner_url_.reset(
214        new GURL(result.spec_.data(), result.parsed_.Length(),
215                 *result.parsed_.inner_parsed(), true));
216  }
217  return result;
218}
219
220// Note: code duplicated above (it's inconvenient to use a template here).
221GURL GURL::ResolveWithCharsetConverter(
222    const base::string16& relative,
223    url::CharsetConverter* charset_converter) const {
224  // Not allowed for invalid URLs.
225  if (!is_valid_)
226    return GURL();
227
228  GURL result;
229
230  // Reserve enough room in the output for the input, plus some extra so that
231  // we have room if we have to escape a few things without reallocating.
232  result.spec_.reserve(spec_.size() + 32);
233  url::StdStringCanonOutput output(&result.spec_);
234
235  if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
236                            parsed_, relative.data(),
237                            static_cast<int>(relative.length()),
238                            charset_converter, &output, &result.parsed_)) {
239    // Error resolving, return an empty URL.
240    return GURL();
241  }
242
243  output.Complete();
244  result.is_valid_ = true;
245  if (result.SchemeIsFileSystem()) {
246    result.inner_url_.reset(
247        new GURL(result.spec_.data(), result.parsed_.Length(),
248                 *result.parsed_.inner_parsed(), true));
249  }
250  return result;
251}
252
253// Note: code duplicated below (it's inconvenient to use a template here).
254GURL GURL::ReplaceComponents(
255    const url::Replacements<char>& replacements) const {
256  GURL result;
257
258  // Not allowed for invalid URLs.
259  if (!is_valid_)
260    return GURL();
261
262  // Reserve enough room in the output for the input, plus some extra so that
263  // we have room if we have to escape a few things without reallocating.
264  result.spec_.reserve(spec_.size() + 32);
265  url::StdStringCanonOutput output(&result.spec_);
266
267  result.is_valid_ = url::ReplaceComponents(
268      spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
269      NULL, &output, &result.parsed_);
270
271  output.Complete();
272  if (result.is_valid_ && result.SchemeIsFileSystem()) {
273    result.inner_url_.reset(new GURL(spec_.data(), result.parsed_.Length(),
274                                     *result.parsed_.inner_parsed(), true));
275  }
276  return result;
277}
278
279// Note: code duplicated above (it's inconvenient to use a template here).
280GURL GURL::ReplaceComponents(
281    const url::Replacements<base::char16>& replacements) const {
282  GURL result;
283
284  // Not allowed for invalid URLs.
285  if (!is_valid_)
286    return GURL();
287
288  // Reserve enough room in the output for the input, plus some extra so that
289  // we have room if we have to escape a few things without reallocating.
290  result.spec_.reserve(spec_.size() + 32);
291  url::StdStringCanonOutput output(&result.spec_);
292
293  result.is_valid_ = url::ReplaceComponents(
294      spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
295      NULL, &output, &result.parsed_);
296
297  output.Complete();
298  if (result.is_valid_ && result.SchemeIsFileSystem()) {
299    result.inner_url_.reset(new GURL(spec_.data(), result.parsed_.Length(),
300                                     *result.parsed_.inner_parsed(), true));
301  }
302  return result;
303}
304
305GURL GURL::GetOrigin() const {
306  // This doesn't make sense for invalid or nonstandard URLs, so return
307  // the empty URL
308  if (!is_valid_ || !IsStandard())
309    return GURL();
310
311  if (SchemeIsFileSystem())
312    return inner_url_->GetOrigin();
313
314  url::Replacements<char> replacements;
315  replacements.ClearUsername();
316  replacements.ClearPassword();
317  replacements.ClearPath();
318  replacements.ClearQuery();
319  replacements.ClearRef();
320
321  return ReplaceComponents(replacements);
322}
323
324GURL GURL::GetAsReferrer() const {
325  if (!is_valid_ ||
326      (!has_ref() && !has_username() && !has_password()))
327    return GURL(*this);
328
329  url::Replacements<char> replacements;
330  replacements.ClearRef();
331  replacements.ClearUsername();
332  replacements.ClearPassword();
333  return ReplaceComponents(replacements);
334}
335
336GURL GURL::GetWithEmptyPath() const {
337  // This doesn't make sense for invalid or nonstandard URLs, so return
338  // the empty URL.
339  if (!is_valid_ || !IsStandard())
340    return GURL();
341
342  // We could optimize this since we know that the URL is canonical, and we are
343  // appending a canonical path, so avoiding re-parsing.
344  GURL other(*this);
345  if (parsed_.path.len == 0)
346    return other;
347
348  // Clear everything after the path.
349  other.parsed_.query.reset();
350  other.parsed_.ref.reset();
351
352  // Set the path, since the path is longer than one, we can just set the
353  // first character and resize.
354  other.spec_[other.parsed_.path.begin] = '/';
355  other.parsed_.path.len = 1;
356  other.spec_.resize(other.parsed_.path.begin + 1);
357  return other;
358}
359
360bool GURL::IsStandard() const {
361  return url::IsStandard(spec_.data(), parsed_.scheme);
362}
363
364bool GURL::SchemeIs(const char* lower_ascii_scheme) const {
365  if (parsed_.scheme.len <= 0)
366    return lower_ascii_scheme == NULL;
367  return url::LowerCaseEqualsASCII(spec_.data() + parsed_.scheme.begin,
368                                   spec_.data() + parsed_.scheme.end(),
369                                   lower_ascii_scheme);
370}
371
372bool GURL::SchemeIsHTTPOrHTTPS() const {
373  return SchemeIs(url::kHttpScheme) || SchemeIs(url::kHttpsScheme);
374}
375
376bool GURL::SchemeIsWSOrWSS() const {
377  return SchemeIs(url::kWsScheme) || SchemeIs(url::kWssScheme);
378}
379
380int GURL::IntPort() const {
381  if (parsed_.port.is_nonempty())
382    return url::ParsePort(spec_.data(), parsed_.port);
383  return url::PORT_UNSPECIFIED;
384}
385
386int GURL::EffectiveIntPort() const {
387  int int_port = IntPort();
388  if (int_port == url::PORT_UNSPECIFIED && IsStandard())
389    return url::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin,
390                                     parsed_.scheme.len);
391  return int_port;
392}
393
394std::string GURL::ExtractFileName() const {
395  url::Component file_component;
396  url::ExtractFileName(spec_.data(), parsed_.path, &file_component);
397  return ComponentString(file_component);
398}
399
400std::string GURL::PathForRequest() const {
401  DCHECK(parsed_.path.len > 0) << "Canonical path for requests should be non-empty";
402  if (parsed_.ref.len >= 0) {
403    // Clip off the reference when it exists. The reference starts after the #
404    // sign, so we have to subtract one to also remove it.
405    return std::string(spec_, parsed_.path.begin,
406                       parsed_.ref.begin - parsed_.path.begin - 1);
407  }
408  // Compute the actual path length, rather than depending on the spec's
409  // terminator.  If we're an inner_url, our spec continues on into our outer
410  // url's path/query/ref.
411  int path_len = parsed_.path.len;
412  if (parsed_.query.is_valid())
413    path_len = parsed_.query.end() - parsed_.path.begin;
414
415  return std::string(spec_, parsed_.path.begin, path_len);
416}
417
418std::string GURL::HostNoBrackets() const {
419  // If host looks like an IPv6 literal, strip the square brackets.
420  url::Component h(parsed_.host);
421  if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') {
422    h.begin++;
423    h.len -= 2;
424  }
425  return ComponentString(h);
426}
427
428std::string GURL::GetContent() const {
429  return is_valid_ ? ComponentString(parsed_.GetContent()) : std::string();
430}
431
432bool GURL::HostIsIPAddress() const {
433  if (!is_valid_ || spec_.empty())
434     return false;
435
436  url::RawCanonOutputT<char, 128> ignored_output;
437  url::CanonHostInfo host_info;
438  url::CanonicalizeIPAddress(spec_.c_str(), parsed_.host, &ignored_output,
439                             &host_info);
440  return host_info.IsIPAddress();
441}
442
443#ifdef WIN32
444
445const GURL& GURL::EmptyGURL() {
446  // Avoid static object construction/destruction on startup/shutdown.
447  if (!empty_gurl) {
448    // Create the string. Be careful that we don't break in the case that this
449    // is being called from multiple threads.
450    GURL* new_empty_gurl = new GURL;
451    if (InterlockedCompareExchangePointer(
452        reinterpret_cast<PVOID*>(&empty_gurl), new_empty_gurl, NULL)) {
453      // The old value was non-NULL, so no replacement was done. Another
454      // thread did the initialization out from under us.
455      delete new_empty_gurl;
456    }
457  }
458  return *empty_gurl;
459}
460
461#else
462
463void EmptyGURLOnce(void) {
464  empty_gurl = new GURL;
465}
466
467const GURL& GURL::EmptyGURL() {
468  // Avoid static object construction/destruction on startup/shutdown.
469  pthread_once(&empty_gurl_once, EmptyGURLOnce);
470  return *empty_gurl;
471}
472
473#endif  // WIN32
474
475bool GURL::DomainIs(const char* lower_ascii_domain,
476                    int domain_len) const {
477  // Return false if this URL is not valid or domain is empty.
478  if (!is_valid_ || !domain_len)
479    return false;
480
481  // FileSystem URLs have empty parsed_.host, so check this first.
482  if (SchemeIsFileSystem() && inner_url_)
483    return inner_url_->DomainIs(lower_ascii_domain, domain_len);
484
485  if (!parsed_.host.is_nonempty())
486    return false;
487
488  // Check whether the host name is end with a dot. If yes, treat it
489  // the same as no-dot unless the input comparison domain is end
490  // with dot.
491  const char* last_pos = spec_.data() + parsed_.host.end() - 1;
492  int host_len = parsed_.host.len;
493  if ('.' == *last_pos && '.' != lower_ascii_domain[domain_len - 1]) {
494    last_pos--;
495    host_len--;
496  }
497
498  // Return false if host's length is less than domain's length.
499  if (host_len < domain_len)
500    return false;
501
502  // Compare this url whether belong specific domain.
503  const char* start_pos = spec_.data() + parsed_.host.begin +
504                          host_len - domain_len;
505
506  if (!url::LowerCaseEqualsASCII(start_pos,
507                                 last_pos + 1,
508                                 lower_ascii_domain,
509                                 lower_ascii_domain + domain_len))
510    return false;
511
512  // Check whether host has right domain start with dot, make sure we got
513  // right domain range. For example www.google.com has domain
514  // "google.com" but www.iamnotgoogle.com does not.
515  if ('.' != lower_ascii_domain[0] && host_len > domain_len &&
516      '.' != *(start_pos - 1))
517    return false;
518
519  return true;
520}
521
522void GURL::Swap(GURL* other) {
523  spec_.swap(other->spec_);
524  std::swap(is_valid_, other->is_valid_);
525  std::swap(parsed_, other->parsed_);
526  inner_url_.swap(other->inner_url_);
527}
528
529std::ostream& operator<<(std::ostream& out, const GURL& url) {
530  return out << url.possibly_invalid_spec();
531}
532