diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-18 22:44:49 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-18 22:44:49 +0000 |
commit | 76e7da2f3993ed156cc633d84f0c0d8ce3475e47 (patch) | |
tree | 0bf83bcc78d1c22ce608289415d1bcd39761694c | |
parent | 2f1412eee183dfb9e04777827aae4a8766fc9f45 (diff) | |
download | chromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.zip chromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.tar.gz chromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.tar.bz2 |
Make FixupURL() return a GURL instead of a string (i.e. do fixup + canonicalization). Nearly every caller was already doing this.
This in turn allows us to do better fixup/canonicalization of view-source: URLs. We now convert "view-source:google.com" into "view-source:http://google.com/".
With a few changes scattered through the omnibox code, this also means we can do HTTP-stripping on view-source: URLs, and support the user typing in things like the case above. This also fixes some weirdness where if you tried to type something starting with "view-source:", the What You Typed match in the dropdown would show only a scheme, or a scheme plus "http:", in some cases.
BUG=46612
TEST="view-source:google.com" should work.
Review URL: http://codereview.chromium.org/2817011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50290 0039d316-1c4b-4281-b951-d872f2087c98
27 files changed, 210 insertions, 197 deletions
diff --git a/chrome/browser/autocomplete/autocomplete.cc b/chrome/browser/autocomplete/autocomplete.cc index d92677e..cdaa56f 100644 --- a/chrome/browser/autocomplete/autocomplete.cc +++ b/chrome/browser/autocomplete/autocomplete.cc @@ -572,23 +572,13 @@ void AutocompleteProvider::SetProfile(Profile* profile) { } // static -size_t AutocompleteProvider::TrimHttpPrefix(std::wstring* url) { +bool AutocompleteProvider::HasHTTPScheme(const std::wstring& input) { + std::string utf8_input(WideToUTF8(input)); url_parse::Component scheme; - if (!url_util::FindAndCompareScheme(WideToUTF8(*url), chrome::kHttpScheme, - &scheme)) - return 0; // Not "http". - - // Erase scheme plus up to two slashes. - size_t prefix_len = scheme.end() + 1; // "http:" - const size_t after_slashes = std::min(url->length(), - static_cast<size_t>(scheme.end() + 3)); - while ((prefix_len < after_slashes) && ((*url)[prefix_len] == L'/')) - ++prefix_len; - if (prefix_len == url->length()) - url->clear(); - else - url->erase(url->begin(), url->begin() + prefix_len); - return prefix_len; + if (url_util::FindAndCompareScheme(utf8_input, chrome::kViewSourceScheme, + &scheme)) + utf8_input.erase(0, scheme.end() + 1); + return url_util::FindAndCompareScheme(utf8_input, chrome::kHttpScheme, NULL); } void AutocompleteProvider::UpdateStarredStateOfMatches() { diff --git a/chrome/browser/autocomplete/autocomplete.h b/chrome/browser/autocomplete/autocomplete.h index 4f85cf4..2338846 100644 --- a/chrome/browser/autocomplete/autocomplete.h +++ b/chrome/browser/autocomplete/autocomplete.h @@ -560,9 +560,8 @@ class AutocompleteProvider virtual ~AutocompleteProvider(); - // Trims "http:" and up to two subsequent slashes from |url|. Returns the - // number of characters that were trimmed. - static size_t TrimHttpPrefix(std::wstring* url); + // Returns whether |input| begins "http:" or "view-source:http:". + static bool HasHTTPScheme(const std::wstring& input); // Updates the starred state of each of the matches in matches_ from the // profile's bookmark bar model. diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc index 07f584c..6b14c17 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.cc +++ b/chrome/browser/autocomplete/autocomplete_edit.cc @@ -163,7 +163,7 @@ bool AutocompleteEditModel::GetURLForText(const std::wstring& text, if (type != AutocompleteInput::URL) return false; - *url = GURL(URLFixerUpper::FixupURL(WideToUTF8(text), std::string())); + *url = URLFixerUpper::FixupURL(WideToUTF8(text), std::string()); return true; } diff --git a/chrome/browser/autocomplete/autocomplete_unittest.cc b/chrome/browser/autocomplete/autocomplete_unittest.cc index 8042724..6a66e98 100644 --- a/chrome/browser/autocomplete/autocomplete_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -348,6 +348,7 @@ TEST(AutocompleteInput, ParseForEmphasizeComponent) { { L"view-source:http://www.foo.com/", Component(12, 4), Component(19, 11) }, { L"view-source:https://example.com/", Component(12, 5), Component(20, 11) }, + { L"view-source:www.foo.com", kInvalidComponent, Component(12, 11) }, { L"view-source:", Component(0, 11), kInvalidComponent }, { L"view-source:garbage", kInvalidComponent, Component(12, 7) }, { L"view-source:http://http://foo", Component(12, 4), Component(19, 4) }, diff --git a/chrome/browser/autocomplete/history_contents_provider.cc b/chrome/browser/autocomplete/history_contents_provider.cc index deb35d2..c05fbdc 100644 --- a/chrome/browser/autocomplete/history_contents_provider.cc +++ b/chrome/browser/autocomplete/history_contents_provider.cc @@ -75,8 +75,7 @@ void HistoryContentsProvider::Start(const AutocompleteInput& input, // Change input type so matches will be marked up properly. input_type_ = input.type(); - trim_http_ = !url_util::FindAndCompareScheme(WideToUTF8(input.text()), - chrome::kHttpScheme, NULL); + trim_http_ = !HasHTTPScheme(input.text()); // Decide what to do about any previous query/results. if (!minimal_changes) { diff --git a/chrome/browser/autocomplete/history_url_provider.cc b/chrome/browser/autocomplete/history_url_provider.cc index 703b909..1952a65 100644 --- a/chrome/browser/autocomplete/history_url_provider.cc +++ b/chrome/browser/autocomplete/history_url_provider.cc @@ -365,7 +365,7 @@ std::wstring HistoryURLProvider::FixupUserInput( const std::wstring& input_text = input.text(); // Fixup and canonicalize user input. const GURL canonical_gurl(URLFixerUpper::FixupURL(WideToUTF8(input_text), - std::string())); + std::string())); std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); if (canonical_gurl_str.empty()) { // This probably won't happen, but there are no guarantees. @@ -428,6 +428,23 @@ std::wstring HistoryURLProvider::FixupUserInput( } // static +size_t HistoryURLProvider::TrimHttpPrefix(std::wstring* url) { + // Find any "http:". + if (!HasHTTPScheme(*url)) + return 0; + size_t scheme_pos = url->find(ASCIIToWide(chrome::kHttpScheme) + L":"); + DCHECK(scheme_pos != std::wstring::npos); + + // Erase scheme plus up to two slashes. + size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1; + const size_t after_slashes = std::min(url->length(), prefix_end + 2); + while ((prefix_end < after_slashes) && ((*url)[prefix_end] == L'/')) + ++prefix_end; + url->erase(scheme_pos, prefix_end - scheme_pos); + return (scheme_pos == 0) ? prefix_end : 0; +} + +// static bool HistoryURLProvider::IsHostOnly(const GURL& url) { DCHECK(url.is_valid()); return (!url.has_path() || (url.path() == "/")) && !url.has_query() && @@ -627,8 +644,7 @@ void HistoryURLProvider::RunAutocompletePasses( // Create a match for exactly what the user typed. This will only be used as // a fallback in case we can't get the history service or URL DB; otherwise, // we'll run this again in DoAutocomplete() and use that result instead. - const bool trim_http = !url_util::FindAndCompareScheme( - WideToUTF8(input.text()), chrome::kHttpScheme, NULL); + const bool trim_http = !HasHTTPScheme(input.text()); // Don't do this for queries -- while we can sometimes mark up a match for // this, it's not what the user wants, and just adds noise. if ((input.type() != AutocompleteInput::QUERY) && diff --git a/chrome/browser/autocomplete/history_url_provider.h b/chrome/browser/autocomplete/history_url_provider.h index da4fc61..e98d777 100644 --- a/chrome/browser/autocomplete/history_url_provider.h +++ b/chrome/browser/autocomplete/history_url_provider.h @@ -261,6 +261,12 @@ class HistoryURLProvider : public AutocompleteProvider { // output that surprises the user ("Search Google for xn--6ca.com"). static std::wstring FixupUserInput(const AutocompleteInput& input); + // Trims "http:" and up to two subsequent slashes from |url|. Returns the + // number of characters that were trimmed. + // NOTE: For a view-source: URL, this will trim from after "view-source:" and + // return 0. + static size_t TrimHttpPrefix(std::wstring* url); + // Returns true if |url| is just a host (e.g. "http://www.google.com/") and // not some other subpage (e.g. "http://www.google.com/foo.html"). static bool IsHostOnly(const GURL& url); diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc index ba550a6..3da49df 100644 --- a/chrome/browser/autocomplete/search_provider.cc +++ b/chrome/browser/autocomplete/search_provider.cc @@ -445,9 +445,8 @@ bool SearchProvider::ParseSuggestResults(Value* root_val, site_val->IsType(Value::TYPE_STRING) && site_val->GetAsString(&site_name)) { // We can't blindly trust the URL coming from the server to be valid. - GURL result_url = - GURL(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str), - std::string())); + GURL result_url(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str), + std::string())); if (result_url.is_valid()) navigation_results.push_back(NavigationResult(result_url, site_name)); } @@ -737,9 +736,8 @@ AutocompleteMatch SearchProvider::NavigationToMatch( AutocompleteMatch match(this, relevance, false, AutocompleteMatch::NAVSUGGEST); match.destination_url = navigation.url; - const bool trim_http = !url_util::FindAndCompareScheme( - WideToUTF8(input_text), chrome::kHttpScheme, NULL); - match.contents = StringForURLDisplay(navigation.url, true, trim_http); + match.contents = + StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text)); AutocompleteMatch::ClassifyMatchInString(input_text, match.contents, ACMatchClassification::URL, &match.contents_class); diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 53f9cf8..d3e481c 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -1438,8 +1438,8 @@ void Browser::ViewSource() { TabContents* current_tab = GetSelectedTabContents(); NavigationEntry* entry = current_tab->controller().GetLastCommittedEntry(); if (entry) { - GURL url("view-source:" + entry->url().spec()); - OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); + OpenURL(GURL(chrome::kViewSourceScheme + std::string(":") + + entry->url().spec()), GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); } } @@ -3828,17 +3828,15 @@ GURL Browser::GetHomePage() const { if (command_line.HasSwitch(switches::kHomePage)) { FilePath browser_directory; PathService::Get(base::DIR_CURRENT, &browser_directory); - std::string new_homepage = URLFixerUpper::FixupRelativeFile( - browser_directory, - command_line.GetSwitchValuePath(switches::kHomePage)); - GURL home_page = GURL(new_homepage); + GURL home_page(URLFixerUpper::FixupRelativeFile(browser_directory, + command_line.GetSwitchValuePath(switches::kHomePage))); if (home_page.is_valid()) return home_page; } if (profile_->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)) return GURL(chrome::kChromeUINewTabURL); - GURL home_page = GURL(URLFixerUpper::FixupURL( + GURL home_page(URLFixerUpper::FixupURL( WideToUTF8(profile_->GetPrefs()->GetString(prefs::kHomePage)), std::string())); if (!home_page.is_valid()) diff --git a/chrome/browser/browser_init.cc b/chrome/browser/browser_init.cc index fe3638d..a38224f 100644 --- a/chrome/browser/browser_init.cc +++ b/chrome/browser/browser_init.cc @@ -830,9 +830,7 @@ std::vector<GURL> BrowserInit::LaunchWithProfile::GetURLsFromCommandLine( profile->GetTemplateURLModel()->GetDefaultSearchProvider(); if (!default_provider || !default_provider->url()) { // No search provider available. Just treat this as regular URL. - urls.push_back( - GURL(WideToUTF8(URLFixerUpper::FixupRelativeFile(cur_dir_, - value)))); + urls.push_back(URLFixerUpper::FixupRelativeFile(cur_dir_, value)); continue; } const TemplateURLRef* search_url = default_provider->url(); @@ -842,8 +840,7 @@ std::vector<GURL> BrowserInit::LaunchWithProfile::GetURLsFromCommandLine( TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())))); } else { // This will create a file URL or a regular URL. - GURL url = GURL(WideToUTF8( - URLFixerUpper::FixupRelativeFile(cur_dir_, value))); + GURL url(URLFixerUpper::FixupRelativeFile(cur_dir_, value)); // Exclude dangerous schemes. if (url.is_valid()) { ChildProcessSecurityPolicy *policy = diff --git a/chrome/browser/cocoa/custom_home_pages_model.mm b/chrome/browser/cocoa/custom_home_pages_model.mm index f6f5d71..084cf11 100644 --- a/chrome/browser/cocoa/custom_home_pages_model.mm +++ b/chrome/browser/cocoa/custom_home_pages_model.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -106,9 +106,9 @@ NSString* const kHomepageEntryChangedNotification = url = [NSString stringWithString:@""]; // Make sure the url is valid before setting it by fixing it up. - std::string urlToFix(base::SysNSStringToUTF8(url)); - urlToFix = URLFixerUpper::FixupURL(urlToFix, ""); - url_.reset([base::SysUTF8ToNSString(urlToFix) retain]); + std::string fixedUrl(URLFixerUpper::FixupURL( + base::SysNSStringToUTF8(url), std::string()).possibly_invalid_spec()); + url_.reset([base::SysUTF8ToNSString(fixedUrl) retain]); // Broadcast that an individual item has changed. [[NSNotificationCenter defaultCenter] diff --git a/chrome/browser/cocoa/preferences_window_controller.mm b/chrome/browser/cocoa/preferences_window_controller.mm index 45573f4..6099b0b 100644 --- a/chrome/browser/cocoa/preferences_window_controller.mm +++ b/chrome/browser/cocoa/preferences_window_controller.mm @@ -60,7 +60,8 @@ namespace { std::string GetNewTabUIURLString() { - return URLFixerUpper::FixupURL(chrome::kChromeUINewTabURL, std::string()); + return URLFixerUpper::FixupURL(chrome::kChromeUINewTabURL, + std::string()).possibly_invalid_spec(); } // Helper to remove all but the last view from the view hierarchy. @@ -1034,7 +1035,7 @@ enum { kHomepageNewTabPage, kHomepageURL }; // to something valid ("http://google.com"). std::string unfixedURL = urlString ? base::SysNSStringToUTF8(urlString) : chrome::kChromeUINewTabURL; - [self setHomepage:GURL(URLFixerUpper::FixupURL(unfixedURL, std::string()))]; + [self setHomepage:URLFixerUpper::FixupURL(unfixedURL, std::string())]; } // Returns whether the home button should be checked based on the preference. diff --git a/chrome/browser/gtk/bookmark_editor_gtk.cc b/chrome/browser/gtk/bookmark_editor_gtk.cc index d2de0ae..318ba91 100644 --- a/chrome/browser/gtk/bookmark_editor_gtk.cc +++ b/chrome/browser/gtk/bookmark_editor_gtk.cc @@ -278,9 +278,8 @@ void BookmarkEditorGtk::Reset() { GURL BookmarkEditorGtk::GetInputURL() const { if (!url_entry_) return GURL(); // Happens when we're editing a folder. - - return GURL(URLFixerUpper::FixupURL( - gtk_entry_get_text(GTK_ENTRY(url_entry_)), "")); + return URLFixerUpper::FixupURL(gtk_entry_get_text(GTK_ENTRY(url_entry_)), + std::string()); } std::wstring BookmarkEditorGtk::GetInputTitle() const { diff --git a/chrome/browser/gtk/options/general_page_gtk.cc b/chrome/browser/gtk/options/general_page_gtk.cc index d36fbac..0df5b20 100644 --- a/chrome/browser/gtk/options/general_page_gtk.cc +++ b/chrome/browser/gtk/options/general_page_gtk.cc @@ -650,9 +650,8 @@ void GeneralPageGtk::SetHomepage(const GURL& homepage) { } void GeneralPageGtk::SetHomepageFromEntry() { - GURL url(URLFixerUpper::FixupURL( - gtk_entry_get_text(GTK_ENTRY(homepage_use_url_entry_)), "")); - SetHomepage(url); + SetHomepage(URLFixerUpper::FixupURL( + gtk_entry_get_text(GTK_ENTRY(homepage_use_url_entry_)), std::string())); } void GeneralPageGtk::EnableCustomHomepagesControls(bool enable) { diff --git a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc index dd51798..515eb47 100644 --- a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc +++ b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc @@ -180,9 +180,8 @@ UrlPickerDialogGtk::~UrlPickerDialogGtk() { } void UrlPickerDialogGtk::AddURL() { - GURL url(URLFixerUpper::FixupURL( - gtk_entry_get_text(GTK_ENTRY(url_entry_)), "")); - callback_->Run(url); + callback_->Run(URLFixerUpper::FixupURL( + gtk_entry_get_text(GTK_ENTRY(url_entry_)), std::string())); } void UrlPickerDialogGtk::EnableControls() { @@ -263,8 +262,7 @@ void UrlPickerDialogGtk::OnHistorySelectionChanged( void UrlPickerDialogGtk::OnHistoryRowActivated(GtkWidget* tree_view, GtkTreePath* path, GtkTreeViewColumn* column) { - GURL url(URLFixerUpper::FixupURL(GetURLForPath(path), "")); - callback_->Run(url); + callback_->Run(URLFixerUpper::FixupURL(GetURLForPath(path), std::string())); gtk_widget_destroy(dialog_); } diff --git a/chrome/browser/net/url_fixer_upper.cc b/chrome/browser/net/url_fixer_upper.cc index 9d974db..30cc67a 100644 --- a/chrome/browser/net/url_fixer_upper.cc +++ b/chrome/browser/net/url_fixer_upper.cc @@ -11,7 +11,6 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/common/url_constants.h" -#include "googleurl/src/gurl.h" #include "googleurl/src/url_file.h" #include "googleurl/src/url_parse.h" #include "googleurl/src/url_util.h" @@ -462,20 +461,32 @@ std::string URLFixerUpper::SegmentURL(const std::string& text, return scheme; } -std::string URLFixerUpper::FixupURL(const std::string& text, - const std::string& desired_tld) { +GURL URLFixerUpper::FixupURL(const std::string& text, + const std::string& desired_tld) { std::string trimmed; TrimWhitespaceUTF8(text, TRIM_ALL, &trimmed); if (trimmed.empty()) - return std::string(); // Nothing here. + return GURL(); // Nothing here. // Segment the URL. url_parse::Parsed parts; std::string scheme(SegmentURL(trimmed, &parts)); + // For view-source: URLs, we strip "view-source:", do fixup, and stick it back + // on. This allows us to handle things like "view-source:google.com". + if (scheme == chrome::kViewSourceScheme) { + // Reject "view-source:view-source:..." to avoid deep recursion. + std::string view_source(chrome::kViewSourceScheme + std::string(":")); + if (!StartsWithASCII(text, view_source + view_source, false)) { + return GURL(chrome::kViewSourceScheme + std::string(":") + + FixupURL(trimmed.substr(scheme.length() + 1), + desired_tld).possibly_invalid_spec()); + } + } + // We handle the file scheme separately. - if (scheme == "file") - return (parts.scheme.is_valid() ? text : FixupPath(text)); + if (scheme == chrome::kFileScheme) + return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); // For some schemes whose layouts we understand, we rebuild it. if (url_util::IsStandard(scheme.c_str(), @@ -498,7 +509,7 @@ std::string URLFixerUpper::FixupURL(const std::string& text, FixupQuery(trimmed, parts.query, &url); FixupRef(trimmed, parts.ref, &url); - return url; + return GURL(url); } // In the worst-case, we insert a scheme if the URL lacks one. @@ -508,7 +519,7 @@ std::string URLFixerUpper::FixupURL(const std::string& text, trimmed.insert(0, fixed_scheme); } - return trimmed; + return GURL(trimmed); } // The rules are different here than for regular fixup, since we need to handle @@ -516,8 +527,8 @@ std::string URLFixerUpper::FixupURL(const std::string& text, // fixup will look for cues that it is actually a file path before trying to // figure out what file it is. If our logic doesn't work, we will fall back on // regular fixup. -std::string URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, - const FilePath& text) { +GURL URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, + const FilePath& text) { FilePath old_cur_directory; if (!base_dir.empty()) { // Save the old current directory before we move to the new one. @@ -550,16 +561,15 @@ std::string URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, } // Put back the current directory if we saved it. - if (!base_dir.empty()) { + if (!base_dir.empty()) file_util::SetCurrentDirectory(old_cur_directory); - } if (is_file) { GURL file_url = net::FilePathToFileURL(full_path); if (file_url.is_valid()) - return WideToUTF8(net::FormatUrl(file_url, std::wstring(), + return GURL(WideToUTF8(net::FormatUrl(file_url, std::wstring(), net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, NULL, - NULL, NULL)); + NULL, NULL))); // Invalid files fall through to regular processing. } @@ -569,7 +579,7 @@ std::string URLFixerUpper::FixupRelativeFile(const FilePath& base_dir, #elif defined(OS_POSIX) std::string text_utf8 = text.value(); #endif - return FixupURL(text_utf8, ""); + return FixupURL(text_utf8, std::string()); } // Deprecated functions. To be removed when all callers are updated. @@ -581,8 +591,8 @@ std::wstring URLFixerUpper::SegmentURL(const std::wstring& text, UTF8PartsToWideParts(text_utf8, parts_utf8, parts); return UTF8ToWide(scheme_utf8); } -std::wstring URLFixerUpper::FixupRelativeFile(const std::wstring& base_dir, - const std::wstring& text) { - return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir), - FilePath::FromWStringHack(text))); +GURL URLFixerUpper::FixupRelativeFile(const std::wstring& base_dir, + const std::wstring& text) { + return FixupRelativeFile(FilePath::FromWStringHack(base_dir), + FilePath::FromWStringHack(text)); } diff --git a/chrome/browser/net/url_fixer_upper.h b/chrome/browser/net/url_fixer_upper.h index 29e6432..97bc6c0 100644 --- a/chrome/browser/net/url_fixer_upper.h +++ b/chrome/browser/net/url_fixer_upper.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,6 +7,8 @@ #include <string> +#include "googleurl/src/gurl.h" + namespace url_parse { struct Parsed; } @@ -22,6 +24,8 @@ namespace URLFixerUpper { // Segments the given text string into parts of a URL. This is most useful // for schemes such as http, https, and ftp where |SegmentURL| will find many // segments. Currently does not segment "file" schemes. + // Returns the canonicalized scheme, or the empty string when |text| is only + // whitespace. std::string SegmentURL(const std::string& text, url_parse::Parsed* parts); // Deprecated temporary compatibility function. std::wstring SegmentURL(const std::wstring& text, url_parse::Parsed* parts); @@ -32,7 +36,8 @@ namespace URLFixerUpper { // "file:" URL. // // The result will be a "more" valid URL than the input. It may still not - // be valid, convert to a GURL for that. + // be valid, so check the return value's validity or use + // possibly_invalid_spec(). // // If |desired_tld| is non-empty, it represents the TLD the user wishes to // append in the case of an incomplete domain. We check that this is not a @@ -40,8 +45,7 @@ namespace URLFixerUpper { // |desired_tld| to the domain and prepend "www." (unless it, or a scheme, // are already present.) This TLD should not have a leading '.' (use "com" // instead of ".com"). - std::string FixupURL(const std::string& text, - const std::string& desired_tld); + GURL FixupURL(const std::string& text, const std::string& desired_tld); // Converts |text| to a fixed-up URL, allowing it to be a relative path on // the local filesystem. Begin searching in |base_dir|; if empty, use the @@ -52,11 +56,10 @@ namespace URLFixerUpper { // For "regular" input, even if it is possibly a file with a full path, you // should use FixupURL() directly. This function should only be used when // relative path handling is desired, as for command line processing. - std::string FixupRelativeFile(const FilePath& base_dir, - const FilePath& text); + GURL FixupRelativeFile(const FilePath& base_dir, const FilePath& text); // Deprecated temporary compatibility function. - std::wstring FixupRelativeFile(const std::wstring& base_dir, - const std::wstring& text); + GURL FixupRelativeFile(const std::wstring& base_dir, + const std::wstring& text); // For paths like ~, we use $HOME for the current user's home // directory. For tests, we allow our idea of $HOME to be overriden diff --git a/chrome/browser/net/url_fixer_upper_unittest.cc b/chrome/browser/net/url_fixer_upper_unittest.cc index be647ad..67b5cae 100644 --- a/chrome/browser/net/url_fixer_upper_unittest.cc +++ b/chrome/browser/net/url_fixer_upper_unittest.cc @@ -208,7 +208,7 @@ struct fixup_case { } fixup_cases[] = { {"www.google.com", "", "http://www.google.com/"}, {" www.google.com ", "", "http://www.google.com/"}, - {" foo.com/asdf bar", "", "http://foo.com/asdf bar"}, + {" foo.com/asdf bar", "", "http://foo.com/asdf%20%20bar"}, {"..www.google.com..", "", "http://www.google.com./"}, {"http://......", "", "http://....../"}, {"http://host.com:ninety-two/", "", "http://host.com:ninety-two/"}, @@ -224,52 +224,51 @@ struct fixup_case { {"www.google.com#", "", "http://www.google.com/#"}, {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"}, {"user@www.google.com", "", "http://user@www.google.com/"}, - {"\xE6\xB0\xB4.com" , "", "http://\xE6\xB0\xB4.com/"}, + {"\xE6\xB0\xB4.com" , "", "http://xn--1rw.com/"}, // It would be better if this next case got treated as http, but I don't see // a clean way to guess this isn't the new-and-exciting "user" scheme. {"user:passwd@www.google.com:8080/", "", "user:passwd@www.google.com:8080/"}, // {"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"}, {"ftp.google.com", "", "ftp://ftp.google.com/"}, {" ftp.google.com", "", "ftp://ftp.google.com/"}, - {"FTP.GooGle.com", "", "ftp://FTP.GooGle.com/"}, + {"FTP.GooGle.com", "", "ftp://ftp.google.com/"}, {"ftpblah.google.com", "", "http://ftpblah.google.com/"}, {"ftp", "", "http://ftp/"}, {"google.ftp.com", "", "http://google.ftp.com/"}, // URLs which end with 0x85 (NEL in ISO-8859). { "http://google.com/search?q=\xd0\x85", "", - "http://google.com/search?q=\xd0\x85" + "http://google.com/search?q=%D0%85" }, { "http://google.com/search?q=\xec\x97\x85", "", - "http://google.com/search?q=\xec\x97\x85" + "http://google.com/search?q=%EC%97%85" }, { "http://google.com/search?q=\xf0\x90\x80\x85", "", - "http://google.com/search?q=\xf0\x90\x80\x85" + "http://google.com/search?q=%F0%90%80%85" }, // URLs which end with 0xA0 (non-break space in ISO-8859). { "http://google.com/search?q=\xd0\xa0", "", - "http://google.com/search?q=\xd0\xa0" + "http://google.com/search?q=%D0%A0" }, { "http://google.com/search?q=\xec\x97\xa0", "", - "http://google.com/search?q=\xec\x97\xa0" + "http://google.com/search?q=%EC%97%A0" }, { "http://google.com/search?q=\xf0\x90\x80\xa0", "", - "http://google.com/search?q=\xf0\x90\x80\xa0" + "http://google.com/search?q=%F0%90%80%A0" }, // URLs containing IPv6 literals. {"[2001:db8::2]", "", "http://[2001:db8::2]/"}, - {"[::]:80", "", "http://[::]:80/"}, - {"[::]:80/path", "", "http://[::]:80/path"}, + {"[::]:80", "", "http://[::]/"}, + {"[::]:80/path", "", "http://[::]/path"}, + {"[::]:180/path", "", "http://[::]:180/path"}, // TODO(pmarks): Maybe we should parse bare IPv6 literals someday. {"::1", "", "::1"}, }; TEST(URLFixerUpperTest, FixupURL) { - std::string output; - for (size_t i = 0; i < arraysize(fixup_cases); ++i) { fixup_case value = fixup_cases[i]; - output = URLFixerUpper::FixupURL(value.input, value.desired_tld); - EXPECT_EQ(value.output, output); + EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input, + value.desired_tld).possibly_invalid_spec()); } // Check the TLD-appending functionality @@ -295,8 +294,8 @@ TEST(URLFixerUpperTest, FixupURL) { }; for (size_t i = 0; i < arraysize(tld_cases); ++i) { fixup_case value = tld_cases[i]; - output = URLFixerUpper::FixupURL(value.input, value.desired_tld); - EXPECT_EQ(value.output, output); + EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input, + value.desired_tld).possibly_invalid_spec()); } } @@ -314,14 +313,14 @@ TEST(URLFixerUpperTest, FixupFile) { &original)); // reference path - std::string golden = net::FilePathToFileURL(original).spec(); + GURL golden(net::FilePathToFileURL(original)); // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) #if defined(OS_WIN) - std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()), - ""); + GURL fixedup(URLFixerUpper::FixupURL(WideToUTF8(original.value()), + std::string())); #elif defined(OS_POSIX) - std::string fixedup = URLFixerUpper::FixupURL(original.value(), ""); + GURL fixedup(URLFixerUpper::FixupURL(original.value(), std::string())); #endif EXPECT_EQ(golden, fixedup); @@ -331,11 +330,9 @@ TEST(URLFixerUpperTest, FixupFile) { std::string cur(WideToUTF8(original.value())); EXPECT_EQ(':', cur[1]); cur[1] = '|'; - fixedup = URLFixerUpper::FixupURL(cur, ""); - EXPECT_EQ(golden, fixedup); + EXPECT_EQ(golden, URLFixerUpper::FixupURL(cur, std::string())); fixup_case file_cases[] = { - // File URLs go through GURL, which tries to escape intelligently. {"c:\\This%20is a non-existent file.txt", "", "file:///C:/This%2520is%20a%20non-existent%20file.txt"}, @@ -345,27 +342,25 @@ TEST(URLFixerUpperTest, FixupFile) { {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", "file://somenonexistenthost/foo/bar.txt"}, // We do this strictly, like IE8, which only accepts this form using - // backslashes and not forward ones. Its a bit weird that the host/path is - // "more canonicalized" in the UNC case above, and in the http case it - // isn't lowercased, etc. That level of canonicalization will happen when - // it's actually turned into a GURL, so we don't care about it here. Turning - // "//foo" into "http" matches Firefox and IE, silly though it may seem - // (it falls out of adding "http" as the default protocol if you haven't - // entered one). + // backslashes and not forward ones. Turning "//foo" into "http" matches + // Firefox and IE, silly though it may seem (it falls out of adding "http" + // as the default protocol if you haven't entered one). {"//SomeNonexistentHost\\foo/bar.txt", "", - "http://SomeNonexistentHost\\foo/bar.txt"}, + "http://somenonexistenthost/foo/bar.txt"}, {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, + // Much of the work here comes from GURL's canonicalization stage. + {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, + {"file:c:", "", "file:///C:/"}, + {"file:c:WINDOWS", "", "file:///C:/WINDOWS"}, + {"file:c|Program Files", "", "file:///C:/Program%20Files"}, + {"file:/file", "", "file://file/"}, + {"file:////////c:\\foo", "", "file:///C:/foo"}, + {"file://server/folder/file", "", "file://server/folder/file"}, + // These are fixups we don't do, but could consider: // - // {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, - // {"file:c:", "", "file:///c:/"}, - // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"}, - // {"file:c|Program Files", "", "file:///c:/Program Files"}, // {"file:///foo:/bar", "", "file://foo/bar"}, - // {"file:/file", "", "file://file/"}, - // {"file:////////c:\\foo", "", "file:///c:/foo"}, - // {"file://server/folder/file", "", "file://server/folder/file"}, // {"file:/\\/server\\folder/file", "", "file://server/folder/file"}, }; #elif defined(OS_POSIX) @@ -398,9 +393,8 @@ TEST(URLFixerUpperTest, FixupFile) { }; #endif for (size_t i = 0; i < arraysize(file_cases); i++) { - fixedup = URLFixerUpper::FixupURL(file_cases[i].input, - file_cases[i].desired_tld); - EXPECT_EQ(file_cases[i].output, fixedup); + EXPECT_EQ(file_cases[i].output, URLFixerUpper::FixupURL(file_cases[i].input, + file_cases[i].desired_tld).possibly_invalid_spec()); } EXPECT_TRUE(file_util::Delete(original, false)); @@ -414,7 +408,6 @@ TEST(URLFixerUpperTest, FixupRelativeFile) { ASSERT_TRUE(file_util::AbsolutePath(&full_path)); // make sure we pass through good URLs - std::string fixedup; for (size_t i = 0; i < arraysize(fixup_cases); ++i) { fixup_case value = fixup_cases[i]; #if defined(OS_WIN) @@ -422,21 +415,22 @@ TEST(URLFixerUpperTest, FixupRelativeFile) { #elif defined(OS_POSIX) FilePath input(value.input); #endif - fixedup = URLFixerUpper::FixupRelativeFile(dir, input); - EXPECT_EQ(value.output, fixedup); + EXPECT_EQ(value.output, + URLFixerUpper::FixupRelativeFile(dir, input).possibly_invalid_spec()); } // make sure the existing file got fixed-up to a file URL, and that there // are no backslashes - fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); - EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); + EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, + file_part).possibly_invalid_spec(), full_path)); EXPECT_TRUE(file_util::Delete(full_path, false)); // create a filename we know doesn't exist and make sure it doesn't get // fixed up to a file URL FilePath nonexistent_file( FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); - fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); + std::string fixedup(URLFixerUpper::FixupRelativeFile(dir, + nonexistent_file).possibly_invalid_spec()); EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); @@ -452,23 +446,23 @@ TEST(URLFixerUpperTest, FixupRelativeFile) { // test file in the subdir FilePath relative_file = sub_dir.Append(sub_file); - fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); - EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); + EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, + relative_file).possibly_invalid_spec(), full_path)); // test file in the subdir with different slashes and escaping. FilePath::StringType relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/") + sub_file.value(); ReplaceSubstringsAfterOffset(&relative_file_str, 0, FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); - fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); - EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); + EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, + FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); // test relative directories and duplicate slashes // (should resolve to the same file as above) relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); - fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); - EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); + EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, + FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); // done with the subdir EXPECT_TRUE(file_util::Delete(full_path, false)); diff --git a/chrome/browser/tab_contents/navigation_controller_unittest.cc b/chrome/browser/tab_contents/navigation_controller_unittest.cc index b60db13..0a8c677 100644 --- a/chrome/browser/tab_contents/navigation_controller_unittest.cc +++ b/chrome/browser/tab_contents/navigation_controller_unittest.cc @@ -1624,7 +1624,7 @@ TEST_F(NavigationControllerTest, SameSubframe) { TEST_F(NavigationControllerTest, ViewSourceRedirect) { const char kUrl[] = "view-source:http://redirect.to/google.com"; const char kResult[] = "http://google.com"; - const char kExpected[] = "view-source:http://google.com"; + const char kExpected[] = "view-source:google.com"; const GURL url(kUrl); const GURL result_url(kResult); diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index df97268..ef4e6d7 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -1106,8 +1106,8 @@ void RenderViewContextMenu::ExecuteCommand(int id) { break; case IDC_VIEW_SOURCE: - OpenURL(GURL("view-source:" + params_.page_url.spec()), - NEW_FOREGROUND_TAB, PageTransition::LINK); + OpenURL(GURL(chrome::kViewSourceScheme + std::string(":") + + params_.page_url.spec()), NEW_FOREGROUND_TAB, PageTransition::LINK); break; case IDC_CONTENT_CONTEXT_INSPECTELEMENT: @@ -1165,8 +1165,8 @@ void RenderViewContextMenu::ExecuteCommand(int id) { break; case IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE: - OpenURL(GURL("view-source:" + params_.frame_url.spec()), - NEW_FOREGROUND_TAB, PageTransition::LINK); + OpenURL(GURL(chrome::kViewSourceScheme + std::string(":") + + params_.frame_url.spec()), NEW_FOREGROUND_TAB, PageTransition::LINK); break; case IDC_CONTENT_CONTEXT_VIEWFRAMEINFO: { diff --git a/chrome/browser/tab_contents/view_source_uitest.cc b/chrome/browser/tab_contents/view_source_uitest.cc index 9a19ac4..3056543e 100644 --- a/chrome/browser/tab_contents/view_source_uitest.cc +++ b/chrome/browser/tab_contents/view_source_uitest.cc @@ -1,8 +1,9 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/app/chrome_dll_resource.h" +#include "chrome/common/url_constants.h" #include "chrome/test/automation/browser_proxy.h" #include "chrome/test/automation/tab_proxy.h" #include "chrome/test/ui/ui_test.h" @@ -42,15 +43,15 @@ TEST_F(ViewSourceTest, DoesBrowserRenderInViewSource) { std::string cookie = "viewsource_cookie"; std::string cookie_data = "foo"; - // First we navigate to our view-source test page - GURL url = server->TestServerPage(test_html_); - url = GURL("view-source:" + url.spec()); + // First we navigate to our view-source test page. + GURL url(chrome::kViewSourceScheme + std::string(":") + + server->TestServerPage(test_html_).spec()); scoped_refptr<TabProxy> tab(GetActiveTab()); ASSERT_TRUE(tab.get()); ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); - // Try to retrieve the cookie that the page sets - // It should not be there (because we are in view-source mode + // Try to retrieve the cookie that the page sets. It should not be there + // (because we are in view-source mode). std::string cookie_found; ASSERT_TRUE(tab->GetCookieByName(url, cookie, &cookie_found)); EXPECT_NE(cookie_data, cookie_found); @@ -65,42 +66,41 @@ TEST_F(ViewSourceTest, DoesBrowserConsumeViewSourcePrefix) { HTTPTestServer::CreateServer(kDocRoot, NULL); ASSERT_TRUE(NULL != server.get()); - // First we navigate to google.html - GURL url = server->TestServerPage(test_html_); + // First we navigate to google.html. + GURL url(server->TestServerPage(test_html_)); NavigateToURL(url); - // Then we navigate to the SAME url but with the view-source: prefix - GURL url_viewsource = GURL("view-source:" + url.spec()); + // Then we navigate to the same url but with the "view-source:" prefix. + GURL url_viewsource(chrome::kViewSourceScheme + std::string(":") + + url.spec()); NavigateToURL(url_viewsource); - // The URL should still be prefixed with view-source: + // The URL should still be prefixed with "view-source:". EXPECT_EQ(url_viewsource.spec(), GetActiveTabURL().spec()); } -// Make sure that when looking at the actual page, we can select -// "View Source" from the Page menu. +// Make sure that when looking at the actual page, we can select "View Source" +// from the Page menu. TEST_F(ViewSourceTest, ViewSourceInPageMenuEnabledOnANormalPage) { scoped_refptr<HTTPTestServer> server = HTTPTestServer::CreateServer(kDocRoot, NULL); ASSERT_TRUE(NULL != server.get()); - // First we navigate to google.html - GURL url = server->TestServerPage(test_html_); + GURL url(server->TestServerPage(test_html_)); NavigateToURL(url); EXPECT_TRUE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE)); } -// Make sure that when looking at the page source, we can't select -// "View Source" from the Page menu. +// Make sure that when looking at the page source, we can't select "View Source" +// from the Page menu. TEST_F(ViewSourceTest, ViewSourceInPageMenuDisabledWhileViewingSource) { scoped_refptr<HTTPTestServer> server = HTTPTestServer::CreateServer(kDocRoot, NULL); ASSERT_TRUE(NULL != server.get()); - // First we navigate to google.html - GURL url = server->TestServerPage(test_html_); - GURL url_viewsource = GURL("view-source:" + url.spec()); + GURL url_viewsource(chrome::kViewSourceScheme + std::string(":") + + server->TestServerPage(test_html_).spec()); NavigateToURL(url_viewsource); EXPECT_FALSE(IsPageMenuCommandEnabled(IDC_VIEW_SOURCE)); diff --git a/chrome/browser/views/bookmark_editor_view.cc b/chrome/browser/views/bookmark_editor_view.cc index 11df76f..6fc5c55 100644 --- a/chrome/browser/views/bookmark_editor_view.cc +++ b/chrome/browser/views/bookmark_editor_view.cc @@ -421,8 +421,7 @@ void BookmarkEditorView::Reset() { } } GURL BookmarkEditorView::GetInputURL() const { - return GURL(URLFixerUpper::FixupURL(UTF16ToUTF8(url_tf_.text()), - std::string())); + return URLFixerUpper::FixupURL(UTF16ToUTF8(url_tf_.text()), std::string()); } std::wstring BookmarkEditorView::GetInputTitle() const { diff --git a/chrome/browser/views/options/general_page_view.cc b/chrome/browser/views/options/general_page_view.cc index 7ca7255..c41d1c0 100755 --- a/chrome/browser/views/options/general_page_view.cc +++ b/chrome/browser/views/options/general_page_view.cc @@ -287,8 +287,8 @@ void GeneralPageView::ContentsChanged(views::Textfield* sender, // through the fixer upper to allow input like "google.com" to be converted // to something valid ("http://google.com"). If the field contains an // empty or null-host URL, a blank homepage is synced to prefs. - SetHomepage(GURL(URLFixerUpper::FixupURL( - UTF16ToUTF8(homepage_use_url_textfield_->text()), std::string()))); + SetHomepage(URLFixerUpper::FixupURL( + UTF16ToUTF8(homepage_use_url_textfield_->text()), std::string())); } } diff --git a/chrome/browser/views/url_picker.cc b/chrome/browser/views/url_picker.cc index 116075f..fa5418b 100644 --- a/chrome/browser/views/url_picker.cc +++ b/chrome/browser/views/url_picker.cc @@ -244,6 +244,6 @@ void UrlPicker::OnDoubleClick() { } GURL UrlPicker::GetInputURL() const { - return GURL(URLFixerUpper::FixupURL(UTF16ToUTF8(url_field_->text()), - std::string())); + return URLFixerUpper::FixupURL(UTF16ToUTF8(url_field_->text()), + std::string()); } diff --git a/chrome_frame/chrome_active_document.cc b/chrome_frame/chrome_active_document.cc index 2b8e222..faab456 100644 --- a/chrome_frame/chrome_active_document.cc +++ b/chrome_frame/chrome_active_document.cc @@ -782,9 +782,8 @@ void ChromeActiveDocument::OnFindInPage() { void ChromeActiveDocument::OnViewSource() { DCHECK(navigation_info_.url.is_valid()); - std::string url_to_open = "view-source:"; - url_to_open += navigation_info_.url.spec(); - HostNavigate(GURL(url_to_open), GURL(), NEW_WINDOW); + HostNavigate(GURL(chrome::kViewSourceScheme + std::string(":") + + navigation_info_.url.spec()), GURL(), NEW_WINDOW); } void ChromeActiveDocument::OnDetermineSecurityZone(const GUID* cmd_group_guid, diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 946b7ff7..f60afcc 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -747,8 +747,6 @@ std::wstring FormatViewSourceUrl(const GURL& url, const wchar_t* const kWideViewSource = L"view-source:"; const size_t kViewSourceLengthPlus1 = 12; - // 'view-source' requires http, so don't strip it. - format_types &= ~net::kFormatUrlOmitHTTP; GURL real_url(url.possibly_invalid_spec().substr(kViewSourceLengthPlus1)); size_t temp_offset = (*offset_for_adjustment == std::wstring::npos) ? std::wstring::npos : (*offset_for_adjustment - kViewSourceLengthPlus1); @@ -1452,8 +1450,8 @@ std::wstring FormatUrl(const GURL& url, // Special handling for view-source:. Don't use chrome::kViewSourceScheme // because this library shouldn't depend on chrome. const char* const kViewSource = "view-source"; + // Reject "view-source:view-source:..." to avoid deep recursion. const char* const kViewSourceTwice = "view-source:view-source:"; - // Rejects view-source:view-source:... to avoid deep recursive call. if (url.SchemeIs(kViewSource) && !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { return FormatViewSourceUrl(url, languages, format_types, diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc index 4e760fe..461b6d3 100644 --- a/net/base/net_util_unittest.cc +++ b/net/base/net_util_unittest.cc @@ -1411,23 +1411,6 @@ TEST(NetUtilTest, FormatUrl) { */ // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". - // -------- view-source: -------- - {"view-source", - "view-source:http://xn--qcka1pmc.jp/", L"ja", default_format_type, - UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", - 12 + 7}, - - {"view-source of view-source", - "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", - default_format_type, UnescapeRule::NORMAL, - L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, - - // view-source should not omit http. - {"view-source omit http", - "view-source:http://a.b/c", L"en", net::kFormatUrlOmitAll, - UnescapeRule::NORMAL, L"view-source:http://a.b/c", - 19}, - // -------- omit http: -------- {"omit http with user name", "http://user@example.com/foo", L"", net::kFormatUrlOmitAll, @@ -1448,7 +1431,7 @@ TEST(NetUtilTest, FormatUrl) { UnescapeRule::NORMAL, L"http://ftp.google.com/", 7}, - // -------- omit trailing lash on bare hostname -------- + // -------- omit trailing slash on bare hostname -------- {"omit slash when it's the entire path", "http://www.google.com/", L"en", net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, @@ -1471,6 +1454,32 @@ TEST(NetUtilTest, FormatUrl) { {"omit slash for file URLs", "file:///", L"en", net::kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, L"file:///", 7}, + + // -------- view-source: -------- + {"view-source", + "view-source:http://xn--qcka1pmc.jp/", L"ja", default_format_type, + UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", + 19}, + + {"view-source of view-source", + "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", + default_format_type, UnescapeRule::NORMAL, + L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, + + // view-source should omit http and trailing slash where non-view-source + // would. + {"view-source omit http", + "view-source:http://a.b/c", L"en", net::kFormatUrlOmitAll, + UnescapeRule::NORMAL, L"view-source:a.b/c", + 12}, + {"view-source omit http starts with ftp.", + "view-source:http://ftp.b/c", L"en", net::kFormatUrlOmitAll, + UnescapeRule::NORMAL, L"view-source:http://ftp.b/c", + 19}, + {"view-source omit slash when it's the entire path", + "view-source:http://a.b/", L"en", net::kFormatUrlOmitAll, + UnescapeRule::NORMAL, L"view-source:a.b", + 12}, }; for (size_t i = 0; i < arraysize(tests); ++i) { |