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 #include "url/gurl.h" | 5 #include "url/gurl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <ostream> | 10 #include <ostream> |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 | 102 |
103 GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid) | 103 GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid) |
104 : spec_(std::move(canonical_spec)), is_valid_(is_valid), parsed_(parsed) { | 104 : spec_(std::move(canonical_spec)), is_valid_(is_valid), parsed_(parsed) { |
105 InitializeFromCanonicalSpec(); | 105 InitializeFromCanonicalSpec(); |
106 } | 106 } |
107 | 107 |
108 template<typename STR> | 108 template<typename STR> |
109 void GURL::InitCanonical(base::BasicStringPiece<STR> input_spec, | 109 void GURL::InitCanonical(base::BasicStringPiece<STR> input_spec, |
110 bool trim_path_end) { | 110 bool trim_path_end) { |
111 // Reserve enough room in the output for the input, plus some extra so that | |
112 // we have room if we have to escape a few things without reallocating. | |
113 spec_.reserve(input_spec.size() + 32); | |
114 url::StdStringCanonOutput output(&spec_); | 111 url::StdStringCanonOutput output(&spec_); |
115 is_valid_ = url::Canonicalize( | 112 is_valid_ = url::Canonicalize( |
116 input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end, | 113 input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end, |
117 NULL, &output, &parsed_); | 114 NULL, &output, &parsed_); |
118 | 115 |
119 output.Complete(); // Must be done before using string. | 116 output.Complete(); // Must be done before using string. |
120 if (is_valid_ && SchemeIsFileSystem()) { | 117 if (is_valid_ && SchemeIsFileSystem()) { |
121 inner_url_.reset(new GURL(spec_.data(), parsed_.Length(), | 118 inner_url_.reset(new GURL(spec_.data(), parsed_.Length(), |
122 *parsed_.inner_parsed(), true)); | 119 *parsed_.inner_parsed(), true)); |
123 } | 120 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 return spec_ > other.spec_; | 188 return spec_ > other.spec_; |
192 } | 189 } |
193 | 190 |
194 // Note: code duplicated below (it's inconvenient to use a template here). | 191 // Note: code duplicated below (it's inconvenient to use a template here). |
195 GURL GURL::Resolve(const std::string& relative) const { | 192 GURL GURL::Resolve(const std::string& relative) const { |
196 // Not allowed for invalid URLs. | 193 // Not allowed for invalid URLs. |
197 if (!is_valid_) | 194 if (!is_valid_) |
198 return GURL(); | 195 return GURL(); |
199 | 196 |
200 GURL result; | 197 GURL result; |
201 | |
202 // Reserve enough room in the output for the input, plus some extra so that | |
203 // we have room if we have to escape a few things without reallocating. | |
204 result.spec_.reserve(spec_.size() + 32); | |
205 url::StdStringCanonOutput output(&result.spec_); | 198 url::StdStringCanonOutput output(&result.spec_); |
206 | |
207 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()), | 199 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()), |
208 parsed_, relative.data(), | 200 parsed_, relative.data(), |
209 static_cast<int>(relative.length()), | 201 static_cast<int>(relative.length()), |
210 nullptr, &output, &result.parsed_)) { | 202 nullptr, &output, &result.parsed_)) { |
211 // Error resolving, return an empty URL. | 203 // Error resolving, return an empty URL. |
212 return GURL(); | 204 return GURL(); |
213 } | 205 } |
214 | 206 |
215 output.Complete(); | 207 output.Complete(); |
216 result.is_valid_ = true; | 208 result.is_valid_ = true; |
217 if (result.SchemeIsFileSystem()) { | 209 if (result.SchemeIsFileSystem()) { |
218 result.inner_url_.reset( | 210 result.inner_url_.reset( |
219 new GURL(result.spec_.data(), result.parsed_.Length(), | 211 new GURL(result.spec_.data(), result.parsed_.Length(), |
220 *result.parsed_.inner_parsed(), true)); | 212 *result.parsed_.inner_parsed(), true)); |
221 } | 213 } |
222 return result; | 214 return result; |
223 } | 215 } |
224 | 216 |
225 // Note: code duplicated above (it's inconvenient to use a template here). | 217 // Note: code duplicated above (it's inconvenient to use a template here). |
226 GURL GURL::Resolve(const base::string16& relative) const { | 218 GURL GURL::Resolve(const base::string16& relative) const { |
227 // Not allowed for invalid URLs. | 219 // Not allowed for invalid URLs. |
228 if (!is_valid_) | 220 if (!is_valid_) |
229 return GURL(); | 221 return GURL(); |
230 | 222 |
231 GURL result; | 223 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::StdStringCanonOutput output(&result.spec_); | 224 url::StdStringCanonOutput output(&result.spec_); |
237 | |
238 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()), | 225 if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()), |
239 parsed_, relative.data(), | 226 parsed_, relative.data(), |
240 static_cast<int>(relative.length()), | 227 static_cast<int>(relative.length()), |
241 nullptr, &output, &result.parsed_)) { | 228 nullptr, &output, &result.parsed_)) { |
242 // Error resolving, return an empty URL. | 229 // Error resolving, return an empty URL. |
243 return GURL(); | 230 return GURL(); |
244 } | 231 } |
245 | 232 |
246 output.Complete(); | 233 output.Complete(); |
247 result.is_valid_ = true; | 234 result.is_valid_ = true; |
248 if (result.SchemeIsFileSystem()) { | 235 if (result.SchemeIsFileSystem()) { |
249 result.inner_url_.reset( | 236 result.inner_url_.reset( |
250 new GURL(result.spec_.data(), result.parsed_.Length(), | 237 new GURL(result.spec_.data(), result.parsed_.Length(), |
251 *result.parsed_.inner_parsed(), true)); | 238 *result.parsed_.inner_parsed(), true)); |
252 } | 239 } |
253 return result; | 240 return result; |
254 } | 241 } |
255 | 242 |
256 // Note: code duplicated below (it's inconvenient to use a template here). | 243 // Note: code duplicated below (it's inconvenient to use a template here). |
257 GURL GURL::ReplaceComponents( | 244 GURL GURL::ReplaceComponents( |
258 const url::Replacements<char>& replacements) const { | 245 const url::Replacements<char>& replacements) const { |
259 GURL result; | 246 GURL result; |
260 | 247 |
261 // Not allowed for invalid URLs. | 248 // Not allowed for invalid URLs. |
262 if (!is_valid_) | 249 if (!is_valid_) |
263 return GURL(); | 250 return GURL(); |
264 | 251 |
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::StdStringCanonOutput output(&result.spec_); | 252 url::StdStringCanonOutput output(&result.spec_); |
269 | |
270 result.is_valid_ = url::ReplaceComponents( | 253 result.is_valid_ = url::ReplaceComponents( |
271 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, | 254 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, |
272 NULL, &output, &result.parsed_); | 255 NULL, &output, &result.parsed_); |
273 | 256 |
274 output.Complete(); | 257 output.Complete(); |
275 if (result.is_valid_ && result.SchemeIsFileSystem()) { | 258 if (result.is_valid_ && result.SchemeIsFileSystem()) { |
276 result.inner_url_.reset(new GURL(result.spec_.data(), | 259 result.inner_url_.reset(new GURL(result.spec_.data(), |
277 result.parsed_.Length(), | 260 result.parsed_.Length(), |
278 *result.parsed_.inner_parsed(), true)); | 261 *result.parsed_.inner_parsed(), true)); |
279 } | 262 } |
280 return result; | 263 return result; |
281 } | 264 } |
282 | 265 |
283 // Note: code duplicated above (it's inconvenient to use a template here). | 266 // Note: code duplicated above (it's inconvenient to use a template here). |
284 GURL GURL::ReplaceComponents( | 267 GURL GURL::ReplaceComponents( |
285 const url::Replacements<base::char16>& replacements) const { | 268 const url::Replacements<base::char16>& replacements) const { |
286 GURL result; | 269 GURL result; |
287 | 270 |
288 // Not allowed for invalid URLs. | 271 // Not allowed for invalid URLs. |
289 if (!is_valid_) | 272 if (!is_valid_) |
290 return GURL(); | 273 return GURL(); |
291 | 274 |
292 // Reserve enough room in the output for the input, plus some extra so that | |
293 // we have room if we have to escape a few things without reallocating. | |
294 result.spec_.reserve(spec_.size() + 32); | |
295 url::StdStringCanonOutput output(&result.spec_); | 275 url::StdStringCanonOutput output(&result.spec_); |
296 | |
297 result.is_valid_ = url::ReplaceComponents( | 276 result.is_valid_ = url::ReplaceComponents( |
298 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, | 277 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, |
299 NULL, &output, &result.parsed_); | 278 NULL, &output, &result.parsed_); |
300 | 279 |
301 output.Complete(); | 280 output.Complete(); |
302 if (result.is_valid_ && result.SchemeIsFileSystem()) { | 281 if (result.is_valid_ && result.SchemeIsFileSystem()) { |
303 result.inner_url_.reset(new GURL(result.spec_.data(), | 282 result.inner_url_.reset(new GURL(result.spec_.data(), |
304 result.parsed_.Length(), | 283 result.parsed_.Length(), |
305 *result.parsed_.inner_parsed(), true)); | 284 *result.parsed_.inner_parsed(), true)); |
306 } | 285 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 487 } |
509 | 488 |
510 bool operator==(const GURL& x, const base::StringPiece& spec) { | 489 bool operator==(const GURL& x, const base::StringPiece& spec) { |
511 DCHECK_EQ(GURL(spec).possibly_invalid_spec(), spec); | 490 DCHECK_EQ(GURL(spec).possibly_invalid_spec(), spec); |
512 return x.possibly_invalid_spec() == spec; | 491 return x.possibly_invalid_spec() == spec; |
513 } | 492 } |
514 | 493 |
515 bool operator!=(const GURL& x, const base::StringPiece& spec) { | 494 bool operator!=(const GURL& x, const base::StringPiece& spec) { |
516 return !(x == spec); | 495 return !(x == spec); |
517 } | 496 } |
OLD | NEW |