OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/net/url_fixer_upper.h" | 5 #include "chrome/browser/net/url_fixer_upper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 399 |
400 // Otherwise, we need to look at things carefully. | 400 // Otherwise, we need to look at things carefully. |
401 std::string scheme; | 401 std::string scheme; |
402 if (!GetValidScheme(text, &parts->scheme, &scheme)) { | 402 if (!GetValidScheme(text, &parts->scheme, &scheme)) { |
403 // Couldn't determine the scheme, so just pick one. | 403 // Couldn't determine the scheme, so just pick one. |
404 parts->scheme.reset(); | 404 parts->scheme.reset(); |
405 scheme.assign(StartsWithASCII(text, "ftp.", false) ? | 405 scheme.assign(StartsWithASCII(text, "ftp.", false) ? |
406 chrome::kFtpScheme : chrome::kHttpScheme); | 406 chrome::kFtpScheme : chrome::kHttpScheme); |
407 } | 407 } |
408 | 408 |
409 // Proceed with about and chrome schemes, but not file or nonstandard schemes. | 409 // Not segmenting file schemes or nonstandard schemes. |
410 if ((scheme != chrome::kAboutScheme) && (scheme != chrome::kChromeUIScheme) && | 410 if ((scheme == chrome::kFileScheme) || |
411 ((scheme == chrome::kFileScheme) || !url_util::IsStandard(scheme.c_str(), | 411 !url_util::IsStandard(scheme.c_str(), |
412 url_parse::Component(0, static_cast<int>(scheme.length()))))) | 412 url_parse::Component(0, static_cast<int>(scheme.length())))) |
413 return scheme; | 413 return scheme; |
414 | 414 |
415 if (parts->scheme.is_valid()) { | 415 if (parts->scheme.is_valid()) { |
416 // Have the GURL parser do the heavy lifting for us. | 416 // Have the GURL parser do the heavy lifting for us. |
417 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), | 417 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), |
418 parts); | 418 parts); |
419 return scheme; | 419 return scheme; |
420 } | 420 } |
421 | 421 |
422 // We need to add a scheme in order for ParseStandardURL to be happy. | 422 // We need to add a scheme in order for ParseStandardURL to be happy. |
423 // Find the first non-whitespace character. | 423 // Find the first non-whitespace character. |
424 std::string::const_iterator first_nonwhite = text.begin(); | 424 std::string::const_iterator first_nonwhite = text.begin(); |
425 while ((first_nonwhite != text.end()) && IsWhitespace(*first_nonwhite)) | 425 while ((first_nonwhite != text.end()) && IsWhitespace(*first_nonwhite)) |
426 ++first_nonwhite; | 426 ++first_nonwhite; |
427 | 427 |
428 // Construct the text to parse by inserting the scheme. | 428 // Construct the text to parse by inserting the scheme. |
429 std::string inserted_text(scheme); | 429 std::string inserted_text(scheme); |
430 inserted_text.append(chrome::kStandardSchemeSeparator); | 430 inserted_text.append("://"); |
431 std::string text_to_parse(text.begin(), first_nonwhite); | 431 std::string text_to_parse(text.begin(), first_nonwhite); |
432 text_to_parse.append(inserted_text); | 432 text_to_parse.append(inserted_text); |
433 text_to_parse.append(first_nonwhite, text.end()); | 433 text_to_parse.append(first_nonwhite, text.end()); |
434 | 434 |
435 // Have the GURL parser do the heavy lifting for us. | 435 // Have the GURL parser do the heavy lifting for us. |
436 url_parse::ParseStandardURL(text_to_parse.data(), | 436 url_parse::ParseStandardURL(text_to_parse.data(), |
437 static_cast<int>(text_to_parse.length()), | 437 static_cast<int>(text_to_parse.length()), |
438 parts); | 438 parts); |
439 | 439 |
440 // Offset the results of the parse to match the original text. | 440 // Offset the results of the parse to match the original text. |
(...skipping 30 matching lines...) Expand all Loading... |
471 return GURL(chrome::kViewSourceScheme + std::string(":") + | 471 return GURL(chrome::kViewSourceScheme + std::string(":") + |
472 FixupURL(trimmed.substr(scheme.length() + 1), | 472 FixupURL(trimmed.substr(scheme.length() + 1), |
473 desired_tld).possibly_invalid_spec()); | 473 desired_tld).possibly_invalid_spec()); |
474 } | 474 } |
475 } | 475 } |
476 | 476 |
477 // We handle the file scheme separately. | 477 // We handle the file scheme separately. |
478 if (scheme == chrome::kFileScheme) | 478 if (scheme == chrome::kFileScheme) |
479 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); | 479 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); |
480 | 480 |
481 // Parse and rebuild about: and chrome: URLs, except about:blank. | |
482 bool chrome_url = !LowerCaseEqualsASCII(trimmed, chrome::kAboutBlankURL) && | |
483 ((scheme == chrome::kAboutScheme) || (scheme == chrome::kChromeUIScheme)); | |
484 | |
485 // For some schemes whose layouts we understand, we rebuild it. | 481 // For some schemes whose layouts we understand, we rebuild it. |
486 if (chrome_url || url_util::IsStandard(scheme.c_str(), | 482 if (url_util::IsStandard(scheme.c_str(), |
487 url_parse::Component(0, static_cast<int>(scheme.length())))) { | 483 url_parse::Component(0, static_cast<int>(scheme.length())))) { |
488 // Replace the about: scheme with the chrome: scheme. | 484 std::string url(scheme); |
489 std::string url(chrome_url ? chrome::kChromeUIScheme : scheme); | 485 url.append("://"); |
490 url.append(chrome::kStandardSchemeSeparator); | |
491 | 486 |
492 // We need to check whether the |username| is valid because it is our | 487 // We need to check whether the |username| is valid because it is our |
493 // responsibility to append the '@' to delineate the user information from | 488 // responsibility to append the '@' to delineate the user information from |
494 // the host portion of the URL. | 489 // the host portion of the URL. |
495 if (parts.username.is_valid()) { | 490 if (parts.username.is_valid()) { |
496 FixupUsername(trimmed, parts.username, &url); | 491 FixupUsername(trimmed, parts.username, &url); |
497 FixupPassword(trimmed, parts.password, &url); | 492 FixupPassword(trimmed, parts.password, &url); |
498 url.append("@"); | 493 url.append("@"); |
499 } | 494 } |
500 | 495 |
501 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); | 496 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); |
502 if (chrome_url && !parts.host.is_valid()) | |
503 url.append(chrome::kChromeUIDefaultHost); | |
504 FixupPort(trimmed, parts.port, &url); | 497 FixupPort(trimmed, parts.port, &url); |
505 FixupPath(trimmed, parts.path, &url); | 498 FixupPath(trimmed, parts.path, &url); |
506 FixupQuery(trimmed, parts.query, &url); | 499 FixupQuery(trimmed, parts.query, &url); |
507 FixupRef(trimmed, parts.ref, &url); | 500 FixupRef(trimmed, parts.ref, &url); |
508 | 501 |
509 return GURL(url); | 502 return GURL(url); |
510 } | 503 } |
511 | 504 |
512 // In the worst-case, we insert a scheme if the URL lacks one. | 505 // In the worst-case, we insert a scheme if the URL lacks one. |
513 if (!parts.scheme.is_valid()) { | 506 if (!parts.scheme.is_valid()) { |
514 std::string fixed_scheme(scheme); | 507 std::string fixed_scheme(scheme); |
515 fixed_scheme.append(chrome::kStandardSchemeSeparator); | 508 fixed_scheme.append("://"); |
516 trimmed.insert(0, fixed_scheme); | 509 trimmed.insert(0, fixed_scheme); |
517 } | 510 } |
518 | 511 |
519 return GURL(trimmed); | 512 return GURL(trimmed); |
520 } | 513 } |
521 | 514 |
522 // The rules are different here than for regular fixup, since we need to handle | 515 // The rules are different here than for regular fixup, since we need to handle |
523 // input like "hello.html" and know to look in the current directory. Regular | 516 // input like "hello.html" and know to look in the current directory. Regular |
524 // fixup will look for cues that it is actually a file path before trying to | 517 // fixup will look for cues that it is actually a file path before trying to |
525 // figure out what file it is. If our logic doesn't work, we will fall back on | 518 // figure out what file it is. If our logic doesn't work, we will fall back on |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 | 586 |
594 if (part->is_valid()) { | 587 if (part->is_valid()) { |
595 // Offset the location of this component. | 588 // Offset the location of this component. |
596 part->begin += offset; | 589 part->begin += offset; |
597 | 590 |
598 // This part might not have existed in the original text. | 591 // This part might not have existed in the original text. |
599 if (part->begin < 0) | 592 if (part->begin < 0) |
600 part->reset(); | 593 part->reset(); |
601 } | 594 } |
602 } | 595 } |
OLD | NEW |