Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: url/url_canon_relative.cc

Issue 2617173003: [url] Reserve output buffer for various URL parsing paths (Closed)
Patch Set: brettw review Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « url/url_canon.h ('k') | url/url_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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())) + 8);
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
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() does not include the old scheme, so make sure to add it from the
406 // base URL.
407 output->ReserveSizeIfNeeded(
408 replacements.components().Length() +
409 base_parsed.CountCharactersBefore(Parsed::USERNAME, false) + 8);
397 return ReplaceStandardURL(base_url, base_parsed, replacements, 410 return ReplaceStandardURL(base_url, base_parsed, replacements,
398 query_converter, output, out_parsed); 411 query_converter, output, out_parsed);
399 } 412 }
400 413
401 // Resolves a relative URL that happens to be an absolute file path. Examples 414 // Resolves a relative URL that happens to be an absolute file path. Examples
402 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo". 415 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo".
403 template<typename CHAR> 416 template<typename CHAR>
404 bool DoResolveAbsoluteFile(const CHAR* relative_url, 417 bool DoResolveAbsoluteFile(const CHAR* relative_url,
405 const Component& relative_component, 418 const Component& relative_component,
406 CharsetConverter* query_converter, 419 CharsetConverter* query_converter,
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 const Component& relative_component, 568 const Component& relative_component,
556 CharsetConverter* query_converter, 569 CharsetConverter* query_converter,
557 CanonOutput* output, 570 CanonOutput* output,
558 Parsed* out_parsed) { 571 Parsed* out_parsed) {
559 return DoResolveRelativeURL<base::char16>( 572 return DoResolveRelativeURL<base::char16>(
560 base_url, base_parsed, base_is_file, relative_url, 573 base_url, base_parsed, base_is_file, relative_url,
561 relative_component, query_converter, output, out_parsed); 574 relative_component, query_converter, output, out_parsed);
562 } 575 }
563 576
564 } // namespace url 577 } // namespace url
OLDNEW
« no previous file with comments | « url/url_canon.h ('k') | url/url_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698