OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 *parts = url_parse::Parsed(); | 392 *parts = url_parse::Parsed(); |
393 | 393 |
394 string trimmed; | 394 string trimmed; |
395 TrimWhitespaceUTF8(text, TRIM_ALL, &trimmed); | 395 TrimWhitespaceUTF8(text, TRIM_ALL, &trimmed); |
396 if (trimmed.empty()) | 396 if (trimmed.empty()) |
397 return string(); // Nothing to segment. | 397 return string(); // Nothing to segment. |
398 | 398 |
399 #if defined(OS_WIN) | 399 #if defined(OS_WIN) |
400 int trimmed_length = static_cast<int>(trimmed.length()); | 400 int trimmed_length = static_cast<int>(trimmed.length()); |
401 if (url_parse::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) || | 401 if (url_parse::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) || |
402 url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, false)) | 402 url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true)) |
403 return "file"; | 403 return "file"; |
404 #elif defined(OS_POSIX) | 404 #elif defined(OS_POSIX) |
405 if (FilePath::IsSeparator(trimmed.data()[0]) || trimmed.data()[0] == '~') | 405 if (FilePath::IsSeparator(trimmed.data()[0]) || trimmed.data()[0] == '~') |
406 return "file"; | 406 return "file"; |
407 #endif | 407 #endif |
408 | 408 |
409 // Otherwise, we need to look at things carefully. | 409 // Otherwise, we need to look at things carefully. |
410 string scheme; | 410 string scheme; |
411 if (!GetValidScheme(text, &parts->scheme, &scheme)) { | 411 if (!GetValidScheme(text, &parts->scheme, &scheme)) { |
412 // Couldn't determine the scheme, so just pick one. | 412 // Couldn't determine the scheme, so just pick one. |
413 parts->scheme.reset(); | 413 parts->scheme.reset(); |
414 scheme.assign(StartsWithASCII(text, "ftp.", false) ? | 414 scheme.assign(StartsWithASCII(text, "ftp.", false) ? |
415 chrome::kFtpScheme : chrome::kHttpScheme); | 415 chrome::kFtpScheme : chrome::kHttpScheme); |
416 } | 416 } |
417 | 417 |
418 // Not segmenting file schemes or nonstandard schemes. | 418 // Not segmenting file schemes or nonstandard schemes. |
419 if ((scheme == chrome::kFileScheme) || | 419 if ((scheme == chrome::kFileScheme) || |
420 !url_util::IsStandard(scheme.c_str(), static_cast<int>(scheme.length()), | 420 !url_util::IsStandard(scheme.c_str(), |
421 url_parse::Component(0, static_cast<int>(scheme.length())))) | 421 url_parse::Component(0, static_cast<int>(scheme.length())))) |
422 return scheme; | 422 return scheme; |
423 | 423 |
424 if (parts->scheme.is_valid()) { | 424 if (parts->scheme.is_valid()) { |
425 // Have the GURL parser do the heavy lifting for us. | 425 // Have the GURL parser do the heavy lifting for us. |
426 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), | 426 url_parse::ParseStandardURL(text.data(), static_cast<int>(text.length()), |
427 parts); | 427 parts); |
428 return scheme; | 428 return scheme; |
429 } | 429 } |
430 | 430 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 | 469 |
470 // Segment the URL. | 470 // Segment the URL. |
471 url_parse::Parsed parts; | 471 url_parse::Parsed parts; |
472 string scheme(SegmentURL(trimmed, &parts)); | 472 string scheme(SegmentURL(trimmed, &parts)); |
473 | 473 |
474 // We handle the file scheme separately. | 474 // We handle the file scheme separately. |
475 if (scheme == "file") | 475 if (scheme == "file") |
476 return (parts.scheme.is_valid() ? text : FixupPath(text)); | 476 return (parts.scheme.is_valid() ? text : FixupPath(text)); |
477 | 477 |
478 // For some schemes whose layouts we understand, we rebuild it. | 478 // For some schemes whose layouts we understand, we rebuild it. |
479 if (url_util::IsStandard(scheme.c_str(), static_cast<int>(scheme.length()), | 479 if (url_util::IsStandard(scheme.c_str(), |
480 url_parse::Component(0, static_cast<int>(scheme.length())))) { | 480 url_parse::Component(0, static_cast<int>(scheme.length())))) { |
481 string url(scheme); | 481 string url(scheme); |
482 url.append("://"); | 482 url.append("://"); |
483 | 483 |
484 // We need to check whether the |username| is valid because it is our | 484 // We need to check whether the |username| is valid because it is our |
485 // responsibility to append the '@' to delineate the user information from | 485 // responsibility to append the '@' to delineate the user information from |
486 // the host portion of the URL. | 486 // the host portion of the URL. |
487 if (parts.username.is_valid()) { | 487 if (parts.username.is_valid()) { |
488 FixupUsername(trimmed, parts.username, &url); | 488 FixupUsername(trimmed, parts.username, &url); |
489 FixupPassword(trimmed, parts.password, &url); | 489 FixupPassword(trimmed, parts.password, &url); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 url_parse::Parsed parts_utf8; | 576 url_parse::Parsed parts_utf8; |
577 string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8); | 577 string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8); |
578 UTF8PartsToWideParts(text_utf8, parts_utf8, parts); | 578 UTF8PartsToWideParts(text_utf8, parts_utf8, parts); |
579 return UTF8ToWide(scheme_utf8); | 579 return UTF8ToWide(scheme_utf8); |
580 } | 580 } |
581 wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir, | 581 wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir, |
582 const wstring& text) { | 582 const wstring& text) { |
583 return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir), | 583 return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir), |
584 FilePath::FromWStringHack(text))); | 584 FilePath::FromWStringHack(text))); |
585 } | 585 } |
OLD | NEW |