| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 #if defined(OS_MACOSX) | 139 #if defined(OS_MACOSX) |
| 140 static const char kHome[] = "/Users/"; | 140 static const char kHome[] = "/Users/"; |
| 141 #else | 141 #else |
| 142 static const char kHome[] = "/home/"; | 142 static const char kHome[] = "/home/"; |
| 143 #endif | 143 #endif |
| 144 return kHome + text.substr(1); | 144 return kHome + text.substr(1); |
| 145 } | 145 } |
| 146 #endif | 146 #endif |
| 147 | 147 |
| 148 // Tries to create a file: URL from |text| if it looks like a filename, even if | 148 // Tries to create a file: URL from |text| if it looks like a filename, even if |
| 149 // it doesn't resolve as a valid path or to an existing file. Returns true | 149 // it doesn't resolve as a valid path or to an existing file. Returns a |
| 150 // with a (possibly invalid) file: URL in |fixed_up_url| for input beginning | 150 // (possibly invalid) file: URL in |fixed_up_url| for input beginning |
| 151 // with a drive specifier or "\\". Returns false in other cases (including | 151 // with a drive specifier or "\\". Returns the unchanged input in other cases |
| 152 // file: URLs: these don't look like filenames), leaving fixed_up_url | 152 // (including file: URLs: these don't look like filenames). |
| 153 // unchanged. | |
| 154 static string FixupPath(const string& text) { | 153 static string FixupPath(const string& text) { |
| 155 DCHECK(!text.empty()); | 154 DCHECK(!text.empty()); |
| 156 | 155 |
| 157 FilePath::StringType filename; | 156 FilePath::StringType filename; |
| 158 #if defined(OS_WIN) | 157 #if defined(OS_WIN) |
| 159 FilePath input_path(UTF8ToWide(text)); | 158 FilePath input_path(UTF8ToWide(text)); |
| 160 PrepareStringForFileOps(input_path, &filename); | 159 PrepareStringForFileOps(input_path, &filename); |
| 161 | 160 |
| 162 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|". | 161 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|". |
| 163 if (filename.length() > 1 && filename[1] == '|') | 162 if (filename.length() > 1 && filename[1] == '|') |
| 164 filename[1] = ':'; | 163 filename[1] = ':'; |
| 165 #elif defined(OS_POSIX) | 164 #elif defined(OS_POSIX) |
| 166 FilePath input_path(text); | 165 FilePath input_path(text); |
| 167 PrepareStringForFileOps(input_path, &filename); | 166 PrepareStringForFileOps(input_path, &filename); |
| 168 if (filename.length() > 0 && filename[0] == '~') | 167 if (filename.length() > 0 && filename[0] == '~') |
| 169 filename = FixupHomedir(filename); | 168 filename = FixupHomedir(filename); |
| 170 #endif | 169 #endif |
| 171 | 170 |
| 172 // Here, we know the input looks like a file. | 171 // Here, we know the input looks like a file. |
| 173 GURL file_url = net::FilePathToFileURL(FilePath(filename)); | 172 GURL file_url = net::FilePathToFileURL(FilePath(filename)); |
| 174 if (file_url.is_valid()) { | 173 if (file_url.is_valid()) { |
| 175 return WideToUTF8(net::FormatUrl(file_url, std::wstring(), true, | 174 return WideToUTF8(net::FormatUrl(file_url, std::wstring(), true, |
| 176 UnescapeRule::NORMAL, NULL, NULL)); | 175 UnescapeRule::NORMAL, NULL, NULL, NULL)); |
| 177 } | 176 } |
| 178 | 177 |
| 179 // Invalid file URL, just return the input. | 178 // Invalid file URL, just return the input. |
| 180 return text; | 179 return text; |
| 181 } | 180 } |
| 182 | 181 |
| 183 // Checks |domain| to see if a valid TLD is already present. If not, appends | 182 // Checks |domain| to see if a valid TLD is already present. If not, appends |
| 184 // |desired_tld| to the domain, and prepends "www." unless it's already present. | 183 // |desired_tld| to the domain, and prepends "www." unless it's already present. |
| 185 // Then modifies |fixed_up_url| to reflect the changes. | |
| 186 static void AddDesiredTLD(const string& desired_tld, | 184 static void AddDesiredTLD(const string& desired_tld, |
| 187 string* domain) { | 185 string* domain) { |
| 188 if (desired_tld.empty() || domain->empty()) | 186 if (desired_tld.empty() || domain->empty()) |
| 189 return; | 187 return; |
| 190 | 188 |
| 191 // Check the TLD. If the return value is positive, we already have a TLD, so | 189 // Check the TLD. If the return value is positive, we already have a TLD, so |
| 192 // abort; if the return value is string::npos, there's no valid host (e.g. if | 190 // abort; if the return value is string::npos, there's no valid host (e.g. if |
| 193 // the user pasted in garbage for which HistoryURLProvider is trying to | 191 // the user pasted in garbage for which HistoryURLProvider is trying to |
| 194 // suggest an exact match), so adding a TLD makes no sense. The only useful | 192 // suggest an exact match), so adding a TLD makes no sense. The only useful |
| 195 // case is where the return value is 0 (there's a valid host with no known | 193 // case is where the return value is 0 (there's a valid host with no known |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 if (last_nondot < domain.length()) | 259 if (last_nondot < domain.length()) |
| 262 domain.erase(last_nondot); | 260 domain.erase(last_nondot); |
| 263 } | 261 } |
| 264 | 262 |
| 265 // Add any user-specified TLD, if applicable. | 263 // Add any user-specified TLD, if applicable. |
| 266 AddDesiredTLD(desired_tld, &domain); | 264 AddDesiredTLD(desired_tld, &domain); |
| 267 | 265 |
| 268 url->append(domain); | 266 url->append(domain); |
| 269 } | 267 } |
| 270 | 268 |
| 271 // Looks for a port number, including initial colon, at port_start. If | |
| 272 // something invalid (which cannot be fixed up) is found, like ":foo" or | |
| 273 // ":7:7", returns false. Otherwise, removes any extra colons | |
| 274 // ("::1337" -> ":1337", ":/" -> "/") and returns true. | |
| 275 static void FixupPort(const string& text, | 269 static void FixupPort(const string& text, |
| 276 const url_parse::Component& part, | 270 const url_parse::Component& part, |
| 277 string* url) { | 271 string* url) { |
| 278 if (!part.is_valid()) | 272 if (!part.is_valid()) |
| 279 return; | 273 return; |
| 280 | 274 |
| 281 // Look for non-digit in port and strip if found. | 275 // We don't fix up the port at the moment. |
| 282 string port(text, part.begin, part.len); | |
| 283 for (string::iterator i = port.begin(); i != port.end();) { | |
| 284 if (IsAsciiDigit(*i)) | |
| 285 ++i; | |
| 286 else | |
| 287 i = port.erase(i); | |
| 288 } | |
| 289 | |
| 290 if (port.empty()) | |
| 291 return; // Nothing to append. | |
| 292 | |
| 293 url->append(":"); | 276 url->append(":"); |
| 294 url->append(port); | 277 url->append(text, part.begin, part.len); |
| 295 } | 278 } |
| 296 | 279 |
| 297 static inline void FixupPath(const string& text, | 280 static inline void FixupPath(const string& text, |
| 298 const url_parse::Component& part, | 281 const url_parse::Component& part, |
| 299 string* url) { | 282 string* url) { |
| 300 if (!part.is_valid() || part.len == 0) { | 283 if (!part.is_valid() || part.len == 0) { |
| 301 // We should always have a path. | 284 // We should always have a path. |
| 302 url->append("/"); | 285 url->append("/"); |
| 303 return; | 286 return; |
| 304 } | 287 } |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 | 549 |
| 567 // Put back the current directory if we saved it. | 550 // Put back the current directory if we saved it. |
| 568 if (!base_dir.empty()) { | 551 if (!base_dir.empty()) { |
| 569 file_util::SetCurrentDirectory(old_cur_directory); | 552 file_util::SetCurrentDirectory(old_cur_directory); |
| 570 } | 553 } |
| 571 | 554 |
| 572 if (is_file) { | 555 if (is_file) { |
| 573 GURL file_url = net::FilePathToFileURL(full_path); | 556 GURL file_url = net::FilePathToFileURL(full_path); |
| 574 if (file_url.is_valid()) | 557 if (file_url.is_valid()) |
| 575 return WideToUTF8(net::FormatUrl(file_url, std::wstring(), | 558 return WideToUTF8(net::FormatUrl(file_url, std::wstring(), |
| 576 true, UnescapeRule::NORMAL, NULL, NULL)); | 559 true, UnescapeRule::NORMAL, NULL, NULL, NULL)); |
| 577 // Invalid files fall through to regular processing. | 560 // Invalid files fall through to regular processing. |
| 578 } | 561 } |
| 579 | 562 |
| 580 // Fall back on regular fixup for this input. | 563 // Fall back on regular fixup for this input. |
| 581 #if defined(OS_WIN) | 564 #if defined(OS_WIN) |
| 582 string text_utf8 = WideToUTF8(text.value()); | 565 string text_utf8 = WideToUTF8(text.value()); |
| 583 #elif defined(OS_POSIX) | 566 #elif defined(OS_POSIX) |
| 584 string text_utf8 = text.value(); | 567 string text_utf8 = text.value(); |
| 585 #endif | 568 #endif |
| 586 return FixupURL(text_utf8, ""); | 569 return FixupURL(text_utf8, ""); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 597 } | 580 } |
| 598 wstring URLFixerUpper::FixupURL(const wstring& text, | 581 wstring URLFixerUpper::FixupURL(const wstring& text, |
| 599 const wstring& desired_tld) { | 582 const wstring& desired_tld) { |
| 600 return UTF8ToWide(FixupURL(WideToUTF8(text), WideToUTF8(desired_tld))); | 583 return UTF8ToWide(FixupURL(WideToUTF8(text), WideToUTF8(desired_tld))); |
| 601 } | 584 } |
| 602 wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir, | 585 wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir, |
| 603 const wstring& text) { | 586 const wstring& text) { |
| 604 return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir), | 587 return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir), |
| 605 FilePath::FromWStringHack(text))); | 588 FilePath::FromWStringHack(text))); |
| 606 } | 589 } |
| OLD | NEW |