gurl.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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#ifdef WIN32
31#include <windows.h>
32#else
33#include <pthread.h>
34#endif
35
36#include <algorithm>
37
38#include "googleurl/src/gurl.h"
39
40#include "base/logging.h"
41#include "googleurl/src/url_canon_stdstring.h"
42#include "googleurl/src/url_util.h"
43
44namespace {
45
46// External template that can handle initialization of either character type.
47// The input spec is given, and the canonical version will be placed in
48// |*canonical|, along with the parsing of the canonical spec in |*parsed|.
49template<typename STR>
50bool InitCanonical(const STR& input_spec,
51                   std::string* canonical,
52                   url_parse::Parsed* parsed) {
53  // Reserve enough room in the output for the input, plus some extra so that
54  // we have room if we have to escape a few things without reallocating.
55  canonical->reserve(input_spec.size() + 32);
56  url_canon::StdStringCanonOutput output(canonical);
57  bool success = url_util::Canonicalize(
58      input_spec.data(), static_cast<int>(input_spec.length()),
59      NULL, &output, parsed);
60
61  output.Complete();  // Must be done before using string.
62  return success;
63}
64
65static std::string* empty_string = NULL;
66static GURL* empty_gurl = NULL;
67
68#ifdef WIN32
69
70// Returns a static reference to an empty string for returning a reference
71// when there is no underlying string.
72const std::string& EmptyStringForGURL() {
73  // Avoid static object construction/destruction on startup/shutdown.
74  if (!empty_string) {
75    // Create the string. Be careful that we don't break in the case that this
76    // is being called from multiple threads. Statics are not threadsafe.
77    std::string* new_empty_string = new std::string;
78    if (InterlockedCompareExchangePointer(
79        reinterpret_cast<PVOID*>(&empty_string), new_empty_string, NULL)) {
80      // The old value was non-NULL, so no replacement was done. Another
81      // thread did the initialization out from under us.
82      delete new_empty_string;
83    }
84  }
85  return *empty_string;
86}
87
88#else
89
90static pthread_once_t empty_string_once = PTHREAD_ONCE_INIT;
91static pthread_once_t empty_gurl_once = PTHREAD_ONCE_INIT;
92
93void EmptyStringForGURLOnce(void) {
94  empty_string = new std::string;
95}
96
97const std::string& EmptyStringForGURL() {
98  // Avoid static object construction/destruction on startup/shutdown.
99  pthread_once(&empty_string_once, EmptyStringForGURLOnce);
100  return *empty_string;
101}
102
103#endif  // WIN32
104
105} // namespace
106
107GURL::GURL() : is_valid_(false) {
108}
109
110GURL::GURL(const GURL& other)
111    : spec_(other.spec_),
112      is_valid_(other.is_valid_),
113      parsed_(other.parsed_) {
114}
115
116GURL::GURL(const std::string& url_string) {
117  is_valid_ = InitCanonical(url_string, &spec_, &parsed_);
118}
119
120GURL::GURL(const string16& url_string) {
121  is_valid_ = InitCanonical(url_string, &spec_, &parsed_);
122}
123
124GURL::GURL(const char* canonical_spec, size_t canonical_spec_len,
125           const url_parse::Parsed& parsed, bool is_valid)
126    : spec_(canonical_spec, canonical_spec_len),
127      is_valid_(is_valid),
128      parsed_(parsed) {
129#ifndef NDEBUG
130  // For testing purposes, check that the parsed canonical URL is identical to
131  // what we would have produced. Skip checking for invalid URLs have no meaning
132  // and we can't always canonicalize then reproducabely.
133  if (is_valid_) {
134    GURL test_url(spec_);
135
136    DCHECK(test_url.is_valid_ == is_valid_);
137    DCHECK(test_url.spec_ == spec_);
138
139    DCHECK(test_url.parsed_.scheme == parsed_.scheme);
140    DCHECK(test_url.parsed_.username == parsed_.username);
141    DCHECK(test_url.parsed_.password == parsed_.password);
142    DCHECK(test_url.parsed_.host == parsed_.host);
143    DCHECK(test_url.parsed_.port == parsed_.port);
144    DCHECK(test_url.parsed_.path == parsed_.path);
145    DCHECK(test_url.parsed_.query == parsed_.query);
146    DCHECK(test_url.parsed_.ref == parsed_.ref);
147  }
148#endif
149}
150
151const std::string& GURL::spec() const {
152  if (is_valid_ || spec_.empty())
153    return spec_;
154
155  DCHECK(false) << "Trying to get the spec of an invalid URL!";
156  return EmptyStringForGURL();
157}
158
159GURL GURL::Resolve(const std::string& relative) const {
160  return ResolveWithCharsetConverter(relative, NULL);
161}
162GURL GURL::Resolve(const string16& relative) const {
163  return ResolveWithCharsetConverter(relative, NULL);
164}
165
166// Note: code duplicated below (it's inconvenient to use a template here).
167GURL GURL::ResolveWithCharsetConverter(
168    const std::string& relative,
169    url_canon::CharsetConverter* charset_converter) const {
170  // Not allowed for invalid URLs.
171  if (!is_valid_)
172    return GURL();
173
174  GURL result;
175
176  // Reserve enough room in the output for the input, plus some extra so that
177  // we have room if we have to escape a few things without reallocating.
178  result.spec_.reserve(spec_.size() + 32);
179  url_canon::StdStringCanonOutput output(&result.spec_);
180
181  if (!url_util::ResolveRelative(
182          spec_.data(), static_cast<int>(spec_.length()), parsed_,
183          relative.data(), static_cast<int>(relative.length()),
184          charset_converter, &output, &result.parsed_)) {
185    // Error resolving, return an empty URL.
186    return GURL();
187  }
188
189  output.Complete();
190  result.is_valid_ = true;
191  return result;
192}
193
194// Note: code duplicated above (it's inconvenient to use a template here).
195GURL GURL::ResolveWithCharsetConverter(
196    const string16& relative,
197    url_canon::CharsetConverter* charset_converter) const {
198  // Not allowed for invalid URLs.
199  if (!is_valid_)
200    return GURL();
201
202  GURL result;
203
204  // Reserve enough room in the output for the input, plus some extra so that
205  // we have room if we have to escape a few things without reallocating.
206  result.spec_.reserve(spec_.size() + 32);
207  url_canon::StdStringCanonOutput output(&result.spec_);
208
209  if (!url_util::ResolveRelative(
210          spec_.data(), static_cast<int>(spec_.length()), parsed_,
211          relative.data(), static_cast<int>(relative.length()),
212          charset_converter, &output, &result.parsed_)) {
213    // Error resolving, return an empty URL.
214    return GURL();
215  }
216
217  output.Complete();
218  result.is_valid_ = true;
219  return result;
220}
221
222// Note: code duplicated below (it's inconvenient to use a template here).
223GURL GURL::ReplaceComponents(
224    const url_canon::Replacements<char>& replacements) const {
225  GURL result;
226
227  // Not allowed for invalid URLs.
228  if (!is_valid_)
229    return GURL();
230
231  // Reserve enough room in the output for the input, plus some extra so that
232  // we have room if we have to escape a few things without reallocating.
233  result.spec_.reserve(spec_.size() + 32);
234  url_canon::StdStringCanonOutput output(&result.spec_);
235
236  result.is_valid_ = url_util::ReplaceComponents(
237      spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
238      NULL, &output, &result.parsed_);
239
240  output.Complete();
241  return result;
242}
243
244// Note: code duplicated above (it's inconvenient to use a template here).
245GURL GURL::ReplaceComponents(
246    const url_canon::Replacements<char16>& replacements) const {
247  GURL result;
248
249  // Not allowed for invalid URLs.
250  if (!is_valid_)
251    return GURL();
252
253  // Reserve enough room in the output for the input, plus some extra so that
254  // we have room if we have to escape a few things without reallocating.
255  result.spec_.reserve(spec_.size() + 32);
256  url_canon::StdStringCanonOutput output(&result.spec_);
257
258  result.is_valid_ = url_util::ReplaceComponents(
259      spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
260      NULL, &output, &result.parsed_);
261
262  output.Complete();
263  return result;
264}
265
266GURL GURL::GetOrigin() const {
267  // This doesn't make sense for invalid or nonstandard URLs, so return
268  // the empty URL
269  if (!is_valid_ || !IsStandard())
270    return GURL();
271
272  url_canon::Replacements<char> replacements;
273  replacements.ClearUsername();
274  replacements.ClearPassword();
275  replacements.ClearPath();
276  replacements.ClearQuery();
277  replacements.ClearRef();
278
279  return ReplaceComponents(replacements);
280}
281
282GURL GURL::GetWithEmptyPath() const {
283  // This doesn't make sense for invalid or nonstandard URLs, so return
284  // the empty URL.
285  if (!is_valid_ || !IsStandard())
286    return GURL();
287
288  // We could optimize this since we know that the URL is canonical, and we are
289  // appending a canonical path, so avoiding re-parsing.
290  GURL other(*this);
291  if (parsed_.path.len == 0)
292    return other;
293
294  // Clear everything after the path.
295  other.parsed_.query.reset();
296  other.parsed_.ref.reset();
297
298  // Set the path, since the path is longer than one, we can just set the
299  // first character and resize.
300  other.spec_[other.parsed_.path.begin] = '/';
301  other.parsed_.path.len = 1;
302  other.spec_.resize(other.parsed_.path.begin + 1);
303  return other;
304}
305
306bool GURL::IsStandard() const {
307  return url_util::IsStandard(spec_.data(), parsed_.scheme);
308}
309
310bool GURL::SchemeIs(const char* lower_ascii_scheme) const {
311  if (parsed_.scheme.len <= 0)
312    return lower_ascii_scheme == NULL;
313  return url_util::LowerCaseEqualsASCII(spec_.data() + parsed_.scheme.begin,
314                                        spec_.data() + parsed_.scheme.end(),
315                                        lower_ascii_scheme);
316}
317
318int GURL::IntPort() const {
319  if (parsed_.port.is_nonempty())
320    return url_parse::ParsePort(spec_.data(), parsed_.port);
321  return url_parse::PORT_UNSPECIFIED;
322}
323
324int GURL::EffectiveIntPort() const {
325  int int_port = IntPort();
326  if (int_port == url_parse::PORT_UNSPECIFIED && IsStandard())
327    return url_canon::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin,
328                                           parsed_.scheme.len);
329  return int_port;
330}
331
332std::string GURL::ExtractFileName() const {
333  url_parse::Component file_component;
334  url_parse::ExtractFileName(spec_.data(), parsed_.path, &file_component);
335  return ComponentString(file_component);
336}
337
338std::string GURL::PathForRequest() const {
339  DCHECK(parsed_.path.len > 0) << "Canonical path for requests should be non-empty";
340  if (parsed_.ref.len >= 0) {
341    // Clip off the reference when it exists. The reference starts after the #
342    // sign, so we have to subtract one to also remove it.
343    return std::string(spec_, parsed_.path.begin,
344                       parsed_.ref.begin - parsed_.path.begin - 1);
345  }
346
347  // Use everything form the path to the end.
348  return std::string(spec_, parsed_.path.begin);
349}
350
351std::string GURL::HostNoBrackets() const {
352  // If host looks like an IPv6 literal, strip the square brackets.
353  url_parse::Component h(parsed_.host);
354  if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') {
355    h.begin++;
356    h.len -= 2;
357  }
358  return ComponentString(h);
359}
360
361bool GURL::HostIsIPAddress() const {
362  if (!is_valid_ || spec_.empty())
363     return false;
364
365  url_canon::RawCanonOutputT<char, 128> ignored_output;
366  url_canon::CanonHostInfo host_info;
367  url_canon::CanonicalizeIPAddress(spec_.c_str(), parsed_.host,
368                                   &ignored_output, &host_info);
369  return host_info.IsIPAddress();
370}
371
372#ifdef WIN32
373
374const GURL& GURL::EmptyGURL() {
375  // Avoid static object construction/destruction on startup/shutdown.
376  if (!empty_gurl) {
377    // Create the string. Be careful that we don't break in the case that this
378    // is being called from multiple threads.
379    GURL* new_empty_gurl = new GURL;
380    if (InterlockedCompareExchangePointer(
381        reinterpret_cast<PVOID*>(&empty_gurl), new_empty_gurl, NULL)) {
382      // The old value was non-NULL, so no replacement was done. Another
383      // thread did the initialization out from under us.
384      delete new_empty_gurl;
385    }
386  }
387  return *empty_gurl;
388}
389
390#else
391
392void EmptyGURLOnce(void) {
393  empty_gurl = new GURL;
394}
395
396const GURL& GURL::EmptyGURL() {
397  // Avoid static object construction/destruction on startup/shutdown.
398  pthread_once(&empty_gurl_once, EmptyGURLOnce);
399  return *empty_gurl;
400}
401
402#endif  // WIN32
403
404bool GURL::DomainIs(const char* lower_ascii_domain,
405                    int domain_len) const {
406  // Return false if this URL is not valid or domain is empty.
407  if (!is_valid_ || !parsed_.host.is_nonempty() || !domain_len)
408    return false;
409
410  // Check whether the host name is end with a dot. If yes, treat it
411  // the same as no-dot unless the input comparison domain is end
412  // with dot.
413  const char* last_pos = spec_.data() + parsed_.host.end() - 1;
414  int host_len = parsed_.host.len;
415  if ('.' == *last_pos && '.' != lower_ascii_domain[domain_len - 1]) {
416    last_pos--;
417    host_len--;
418  }
419
420  // Return false if host's length is less than domain's length.
421  if (host_len < domain_len)
422    return false;
423
424  // Compare this url whether belong specific domain.
425  const char* start_pos = spec_.data() + parsed_.host.begin +
426                          host_len - domain_len;
427
428  if (!url_util::LowerCaseEqualsASCII(start_pos,
429                                      last_pos + 1,
430                                      lower_ascii_domain,
431                                      lower_ascii_domain + domain_len))
432    return false;
433
434  // Check whether host has right domain start with dot, make sure we got
435  // right domain range. For example www.google.com has domain
436  // "google.com" but www.iamnotgoogle.com does not.
437  if ('.' != lower_ascii_domain[0] && host_len > domain_len &&
438      '.' != *(start_pos - 1))
439    return false;
440
441  return true;
442}
443
444void GURL::Swap(GURL* other) {
445  spec_.swap(other->spec_);
446  std::swap(is_valid_, other->is_valid_);
447  std::swap(parsed_, other->parsed_);
448}
449
450