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 // Canonicalizer functions for working with and resolving relative URLs. | 5 // Canonicalizer functions for working with and resolving relative URLs. |
6 | 6 |
7 #include <algorithm> | |
8 | |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "url/url_canon.h" | 10 #include "url/url_canon.h" |
9 #include "url/url_canon_internal.h" | 11 #include "url/url_canon_internal.h" |
10 #include "url/url_constants.h" | 12 #include "url/url_constants.h" |
11 #include "url/url_file.h" | 13 #include "url/url_file.h" |
12 #include "url/url_parse_internal.h" | 14 #include "url/url_parse_internal.h" |
13 #include "url/url_util_internal.h" | 15 #include "url/url_util_internal.h" |
14 | 16 |
15 namespace url { | 17 namespace url { |
16 | 18 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 output->push_back(base_url[base_path_begin + 2]); | 259 output->push_back(base_url[base_path_begin + 2]); |
258 return base_path_begin + 3; | 260 return base_path_begin + 3; |
259 } | 261 } |
260 | 262 |
261 return base_path_begin; | 263 return base_path_begin; |
262 } | 264 } |
263 | 265 |
264 #endif // WIN32 | 266 #endif // WIN32 |
265 | 267 |
266 // A subroutine of DoResolveRelativeURL, this resolves the URL knowning that | 268 // A subroutine of DoResolveRelativeURL, this resolves the URL knowning that |
267 // the input is a relative path or less (qyuery or ref). | 269 // the input is a relative path or less (query or ref). |
268 template<typename CHAR> | 270 template<typename CHAR> |
269 bool DoResolveRelativePath(const char* base_url, | 271 bool DoResolveRelativePath(const char* base_url, |
270 const Parsed& base_parsed, | 272 const Parsed& base_parsed, |
271 bool base_is_file, | 273 bool base_is_file, |
272 const CHAR* relative_url, | 274 const CHAR* relative_url, |
273 const Component& relative_component, | 275 const Component& relative_component, |
274 CharsetConverter* query_converter, | 276 CharsetConverter* query_converter, |
275 CanonOutput* output, | 277 CanonOutput* output, |
276 Parsed* out_parsed) { | 278 Parsed* out_parsed) { |
277 bool success = true; | 279 bool success = true; |
278 | 280 |
279 // We know the authority section didn't change, copy it to the output. We | 281 // We know the authority section didn't change, copy it to the output. We |
280 // also know we have a path so can copy up to there. | 282 // also know we have a path so can copy up to there. |
281 Component path, query, ref; | 283 Component path, query, ref; |
282 ParsePathInternal(relative_url, relative_component, &path, &query, &ref); | 284 ParsePathInternal(relative_url, relative_component, &path, &query, &ref); |
283 // Canonical URLs always have a path, so we can use that offset. | 285 |
286 // Canonical URLs always have a path, so we can use that offset. Reserve | |
287 // enough room for the base URL, the new path, and some extra bytes for | |
288 // possible escaped characters. | |
289 output->ReserveSizeIfNeeded( | |
290 base_parsed.path.begin + | |
291 std::max(path.end(), std::max(query.end(), ref.end())) + 32); | |
brettw
2017/01/18 21:12:27
You copied "32" from the old code. But when we sta
Charlie Harrison
2017/01/18 21:30:38
SGTM
| |
284 output->Append(base_url, base_parsed.path.begin); | 292 output->Append(base_url, base_parsed.path.begin); |
285 | 293 |
286 if (path.len > 0) { | 294 if (path.len > 0) { |
287 // The path is replaced or modified. | 295 // The path is replaced or modified. |
288 int true_path_begin = output->length(); | 296 int true_path_begin = output->length(); |
289 | 297 |
290 // For file: URLs on Windows, we don't want to treat the drive letter and | 298 // For file: URLs on Windows, we don't want to treat the drive letter and |
291 // colon as part of the path for relative file resolution when the | 299 // colon as part of the path for relative file resolution when the |
292 // incoming URL does not provide a drive spec. We save the true path | 300 // incoming URL does not provide a drive spec. We save the true path |
293 // beginning so we can fix it up after we are done. | 301 // beginning so we can fix it up after we are done. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 // parts of the old URL with the new one. | 395 // parts of the old URL with the new one. |
388 Replacements<CHAR> replacements; | 396 Replacements<CHAR> replacements; |
389 replacements.SetUsername(relative_url, relative_parsed.username); | 397 replacements.SetUsername(relative_url, relative_parsed.username); |
390 replacements.SetPassword(relative_url, relative_parsed.password); | 398 replacements.SetPassword(relative_url, relative_parsed.password); |
391 replacements.SetHost(relative_url, relative_parsed.host); | 399 replacements.SetHost(relative_url, relative_parsed.host); |
392 replacements.SetPort(relative_url, relative_parsed.port); | 400 replacements.SetPort(relative_url, relative_parsed.port); |
393 replacements.SetPath(relative_url, relative_parsed.path); | 401 replacements.SetPath(relative_url, relative_parsed.path); |
394 replacements.SetQuery(relative_url, relative_parsed.query); | 402 replacements.SetQuery(relative_url, relative_parsed.query); |
395 replacements.SetRef(relative_url, relative_parsed.ref); | 403 replacements.SetRef(relative_url, relative_parsed.ref); |
396 | 404 |
405 // Length() will include the scheme size, because the underlying Parsed | |
406 // structure starts from |relative_component.begin|. | |
407 output->ReserveSizeIfNeeded(replacements.components().Length() + 32); | |
brettw
2017/01/18 21:12:27
I don't follow this comment. In this case the inpu
Charlie Harrison
2017/01/18 21:30:38
Just read through the code and I think you're righ
| |
397 return ReplaceStandardURL(base_url, base_parsed, replacements, | 408 return ReplaceStandardURL(base_url, base_parsed, replacements, |
398 query_converter, output, out_parsed); | 409 query_converter, output, out_parsed); |
399 } | 410 } |
400 | 411 |
401 // Resolves a relative URL that happens to be an absolute file path. Examples | 412 // Resolves a relative URL that happens to be an absolute file path. Examples |
402 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo". | 413 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo". |
403 template<typename CHAR> | 414 template<typename CHAR> |
404 bool DoResolveAbsoluteFile(const CHAR* relative_url, | 415 bool DoResolveAbsoluteFile(const CHAR* relative_url, |
405 const Component& relative_component, | 416 const Component& relative_component, |
406 CharsetConverter* query_converter, | 417 CharsetConverter* query_converter, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 const Component& relative_component, | 566 const Component& relative_component, |
556 CharsetConverter* query_converter, | 567 CharsetConverter* query_converter, |
557 CanonOutput* output, | 568 CanonOutput* output, |
558 Parsed* out_parsed) { | 569 Parsed* out_parsed) { |
559 return DoResolveRelativeURL<base::char16>( | 570 return DoResolveRelativeURL<base::char16>( |
560 base_url, base_parsed, base_is_file, relative_url, | 571 base_url, base_parsed, base_is_file, relative_url, |
561 relative_component, query_converter, output, out_parsed); | 572 relative_component, query_converter, output, out_parsed); |
562 } | 573 } |
563 | 574 |
564 } // namespace url | 575 } // namespace url |
OLD | NEW |