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