OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifdef WIN32 | 5 #ifdef WIN32 |
6 #include <windows.h> | 6 #include <windows.h> |
7 #else | 7 #else |
8 #include <pthread.h> | 8 #include <pthread.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <ostream> | 12 #include <ostream> |
13 | 13 |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "url/url_canon_stdstring.h" | 17 #include "url/url_canon_stdstring.h" |
18 #include "url/url_util.h" | 18 #include "url/url_util.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // External template that can handle initialization of either character type. | 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 | 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|. | 24 // |*canonical|, along with the parsing of the canonical spec in |*parsed|. |
25 template<typename STR> | 25 template<typename STR> |
26 bool InitCanonical(const STR& input_spec, | 26 bool InitCanonical(const STR& input_spec, |
27 std::string* canonical, | 27 std::string* canonical, |
28 url_parse::Parsed* parsed) { | 28 url_parse::Parsed* parsed, |
| 29 bool trim_tail = true) { |
29 // Reserve enough room in the output for the input, plus some extra so that | 30 // 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 // we have room if we have to escape a few things without reallocating. |
31 canonical->reserve(input_spec.size() + 32); | 32 canonical->reserve(input_spec.size() + 32); |
32 url_canon::StdStringCanonOutput output(canonical); | 33 url_canon::StdStringCanonOutput output(canonical); |
33 bool success = url_util::Canonicalize( | 34 bool success = url_util::Canonicalize( |
34 input_spec.data(), static_cast<int>(input_spec.length()), | 35 input_spec.data(), static_cast<int>(input_spec.length()), |
35 NULL, &output, parsed); | 36 NULL, &output, parsed, trim_tail); |
36 | 37 |
37 output.Complete(); // Must be done before using string. | 38 output.Complete(); // Must be done before using string. |
38 return success; | 39 return success; |
39 } | 40 } |
40 | 41 |
41 static std::string* empty_string = NULL; | 42 static std::string* empty_string = NULL; |
42 static GURL* empty_gurl = NULL; | 43 static GURL* empty_gurl = NULL; |
43 | 44 |
44 #ifdef WIN32 | 45 #ifdef WIN32 |
45 | 46 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // what we would have produced. Skip checking for invalid URLs have no meaning | 140 // what we would have produced. Skip checking for invalid URLs have no meaning |
140 // and we can't always canonicalize then reproducabely. | 141 // and we can't always canonicalize then reproducabely. |
141 if (is_valid_) { | 142 if (is_valid_) { |
142 url_parse::Component scheme; | 143 url_parse::Component scheme; |
143 if (!url_util::FindAndCompareScheme(spec_.data(), spec_.length(), | 144 if (!url_util::FindAndCompareScheme(spec_.data(), spec_.length(), |
144 "filesystem", &scheme) || | 145 "filesystem", &scheme) || |
145 scheme.begin == parsed_.scheme.begin) { | 146 scheme.begin == parsed_.scheme.begin) { |
146 // We can't do this check on the inner_url of a filesystem URL, as | 147 // 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 // canonical_spec actually points to the start of the outer URL, so we'd |
148 // end up with infinite recursion in this constructor. | 149 // end up with infinite recursion in this constructor. |
149 GURL test_url(spec_); | 150 std::string test_spec; |
| 151 url_parse::Parsed test_parsed; |
| 152 bool test_is_valid = InitCanonical(spec_, &test_spec, |
| 153 &test_parsed, false); |
150 | 154 |
151 DCHECK(test_url.is_valid_ == is_valid_); | 155 DCHECK_EQ(test_is_valid, is_valid_); |
152 DCHECK(test_url.spec_ == spec_); | |
153 | 156 |
154 DCHECK(test_url.parsed_.scheme == parsed_.scheme); | 157 DCHECK_EQ(test_spec, spec_); |
155 DCHECK(test_url.parsed_.username == parsed_.username); | 158 DCHECK(test_parsed.scheme == parsed_.scheme); |
156 DCHECK(test_url.parsed_.password == parsed_.password); | 159 DCHECK(test_parsed.username == parsed_.username); |
157 DCHECK(test_url.parsed_.host == parsed_.host); | 160 DCHECK(test_parsed.password == parsed_.password); |
158 DCHECK(test_url.parsed_.port == parsed_.port); | 161 DCHECK(test_parsed.host == parsed_.host); |
159 DCHECK(test_url.parsed_.path == parsed_.path); | 162 DCHECK(test_parsed.port == parsed_.port); |
160 DCHECK(test_url.parsed_.query == parsed_.query); | 163 DCHECK(test_parsed.path == parsed_.path); |
161 DCHECK(test_url.parsed_.ref == parsed_.ref); | 164 DCHECK(test_parsed.query == parsed_.query); |
| 165 DCHECK(test_parsed.ref == parsed_.ref); |
162 } | 166 } |
163 } | 167 } |
164 #endif | 168 #endif |
165 } | 169 } |
166 | 170 |
167 GURL::~GURL() { | 171 GURL::~GURL() { |
168 delete inner_url_; | 172 delete inner_url_; |
169 } | 173 } |
170 | 174 |
171 GURL& GURL::operator=(const GURL& other) { | 175 GURL& GURL::operator=(const GURL& other) { |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 void GURL::Swap(GURL* other) { | 516 void GURL::Swap(GURL* other) { |
513 spec_.swap(other->spec_); | 517 spec_.swap(other->spec_); |
514 std::swap(is_valid_, other->is_valid_); | 518 std::swap(is_valid_, other->is_valid_); |
515 std::swap(parsed_, other->parsed_); | 519 std::swap(parsed_, other->parsed_); |
516 std::swap(inner_url_, other->inner_url_); | 520 std::swap(inner_url_, other->inner_url_); |
517 } | 521 } |
518 | 522 |
519 std::ostream& operator<<(std::ostream& out, const GURL& url) { | 523 std::ostream& operator<<(std::ostream& out, const GURL& url) { |
520 return out << url.possibly_invalid_spec(); | 524 return out << url.possibly_invalid_spec(); |
521 } | 525 } |
OLD | NEW |