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 // Not segmenting file schemes or nonstandard schemes. | 409 // Proceed with about and chrome schemes, but not file or nonstandard schemes. |
410 if ((scheme == chrome::kFileScheme) || | 410 if ((scheme != chrome::kAboutScheme) && (scheme != chrome::kChromeUIScheme) && |
411 !url_util::IsStandard(scheme.c_str(), | 411 ((scheme == chrome::kFileScheme) || !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("://"); | 430 inserted_text.append(chrome::kStandardSchemeSeparator); |
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 |
481 // For some schemes whose layouts we understand, we rebuild it. | 485 // For some schemes whose layouts we understand, we rebuild it. |
482 if (url_util::IsStandard(scheme.c_str(), | 486 if (chrome_url || url_util::IsStandard(scheme.c_str(), |
483 url_parse::Component(0, static_cast<int>(scheme.length())))) { | 487 url_parse::Component(0, static_cast<int>(scheme.length())))) { |
484 std::string url(scheme); | 488 // Replace the about: scheme with the chrome: scheme. |
485 url.append("://"); | 489 std::string url(chrome_url ? chrome::kChromeUIScheme : scheme); |
| 490 url.append(chrome::kStandardSchemeSeparator); |
486 | 491 |
487 // We need to check whether the |username| is valid because it is our | 492 // We need to check whether the |username| is valid because it is our |
488 // responsibility to append the '@' to delineate the user information from | 493 // responsibility to append the '@' to delineate the user information from |
489 // the host portion of the URL. | 494 // the host portion of the URL. |
490 if (parts.username.is_valid()) { | 495 if (parts.username.is_valid()) { |
491 FixupUsername(trimmed, parts.username, &url); | 496 FixupUsername(trimmed, parts.username, &url); |
492 FixupPassword(trimmed, parts.password, &url); | 497 FixupPassword(trimmed, parts.password, &url); |
493 url.append("@"); | 498 url.append("@"); |
494 } | 499 } |
495 | 500 |
496 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); | 501 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); |
| 502 if (chrome_url && !parts.host.is_valid()) |
| 503 url.append(chrome::kChromeUIDefaultHost); |
497 FixupPort(trimmed, parts.port, &url); | 504 FixupPort(trimmed, parts.port, &url); |
498 FixupPath(trimmed, parts.path, &url); | 505 FixupPath(trimmed, parts.path, &url); |
499 FixupQuery(trimmed, parts.query, &url); | 506 FixupQuery(trimmed, parts.query, &url); |
500 FixupRef(trimmed, parts.ref, &url); | 507 FixupRef(trimmed, parts.ref, &url); |
501 | 508 |
502 return GURL(url); | 509 return GURL(url); |
503 } | 510 } |
504 | 511 |
505 // In the worst-case, we insert a scheme if the URL lacks one. | 512 // In the worst-case, we insert a scheme if the URL lacks one. |
506 if (!parts.scheme.is_valid()) { | 513 if (!parts.scheme.is_valid()) { |
507 std::string fixed_scheme(scheme); | 514 std::string fixed_scheme(scheme); |
508 fixed_scheme.append("://"); | 515 fixed_scheme.append(chrome::kStandardSchemeSeparator); |
509 trimmed.insert(0, fixed_scheme); | 516 trimmed.insert(0, fixed_scheme); |
510 } | 517 } |
511 | 518 |
512 return GURL(trimmed); | 519 return GURL(trimmed); |
513 } | 520 } |
514 | 521 |
515 // The rules are different here than for regular fixup, since we need to handle | 522 // The rules are different here than for regular fixup, since we need to handle |
516 // input like "hello.html" and know to look in the current directory. Regular | 523 // input like "hello.html" and know to look in the current directory. Regular |
517 // fixup will look for cues that it is actually a file path before trying to | 524 // fixup will look for cues that it is actually a file path before trying to |
518 // figure out what file it is. If our logic doesn't work, we will fall back on | 525 // 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... |
586 | 593 |
587 if (part->is_valid()) { | 594 if (part->is_valid()) { |
588 // Offset the location of this component. | 595 // Offset the location of this component. |
589 part->begin += offset; | 596 part->begin += offset; |
590 | 597 |
591 // This part might not have existed in the original text. | 598 // This part might not have existed in the original text. |
592 if (part->begin < 0) | 599 if (part->begin < 0) |
593 part->reset(); | 600 part->reset(); |
594 } | 601 } |
595 } | 602 } |
OLD | NEW |