blob: c34ac90bb9890f34359e2019a729f2041e5502eb [file] [log] [blame]
[email protected]f1f86392012-04-03 13:51:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]9b5b1d602014-06-12 14:29:025#include "components/url_fixer/url_fixer.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]91e81ae2009-05-08 22:14:387#include <algorithm>
8
skuhne66e02552014-08-29 00:13:569#include "base/files/file_path.h"
thestig819adcc82014-09-10 22:24:5310#include "base/files/file_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/logging.h"
skuhne66e02552014-08-29 00:13:5612#if defined(OS_POSIX)
13#include "base/path_service.h"
14#endif
[email protected]f9b294362013-06-10 20:22:3115#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1816#include "base/strings/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2917#include "net/base/escape.h"
[email protected]d96cf752014-04-09 04:05:2818#include "net/base/filename_util.h"
initial.commit09911bf2008-07-26 23:55:2919#include "net/base/net_util.h"
[email protected]be28b5f42012-07-20 11:31:2520#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
tfarina018de6e2015-05-26 17:41:2021#include "url/third_party/mozilla/url_parse.h"
[email protected]761fa4702013-07-02 15:25:1522#include "url/url_file.h"
[email protected]761fa4702013-07-02 15:25:1523#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2924
[email protected]9b5b1d602014-06-12 14:29:0225const char* url_fixer::home_directory_override = NULL;
[email protected]762c5542009-10-21 16:45:3826
[email protected]a63801082009-04-08 04:28:2527namespace {
28
[email protected]9b5b1d602014-06-12 14:29:0229// Hardcode these constants to avoid dependences on //chrome and //content.
30const char kChromeUIScheme[] = "chrome";
31const char kChromeUIDefaultHost[] = "version";
32const char kViewSourceScheme[] = "view-source";
33
[email protected]f20dead2013-03-02 03:01:4834// TODO(estade): Remove these ugly, ugly functions. They are only used in
[email protected]b45334502014-04-30 19:44:0535// SegmentURL. A url::Parsed object keeps track of a bunch of indices into
[email protected]f20dead2013-03-02 03:01:4836// a url string, and these need to be updated when the URL is converted from
[email protected]a63801082009-04-08 04:28:2537// UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it
38// in the correct string format to begin with.
[email protected]b45334502014-04-30 19:44:0539url::Component UTF8ComponentToUTF16Component(
[email protected]a2fedb1e2011-01-25 15:23:3640 const std::string& text_utf8,
[email protected]b45334502014-04-30 19:44:0541 const url::Component& component_utf8) {
[email protected]f20dead2013-03-02 03:01:4842 if (component_utf8.len == -1)
[email protected]b45334502014-04-30 19:44:0543 return url::Component();
[email protected]a2fedb1e2011-01-25 15:23:3644
45 std::string before_component_string =
46 text_utf8.substr(0, component_utf8.begin);
[email protected]9b5b1d602014-06-12 14:29:0247 std::string component_string =
48 text_utf8.substr(component_utf8.begin, component_utf8.len);
[email protected]428fac12013-12-05 21:38:4949 base::string16 before_component_string_16 =
[email protected]036a5f32013-12-25 00:26:1150 base::UTF8ToUTF16(before_component_string);
51 base::string16 component_string_16 = base::UTF8ToUTF16(component_string);
[email protected]b45334502014-04-30 19:44:0552 url::Component component_16(before_component_string_16.length(),
53 component_string_16.length());
[email protected]a2fedb1e2011-01-25 15:23:3654 return component_16;
55}
56
57void UTF8PartsToUTF16Parts(const std::string& text_utf8,
[email protected]b45334502014-04-30 19:44:0558 const url::Parsed& parts_utf8,
59 url::Parsed* parts) {
[email protected]527965412014-05-07 14:38:2660 if (base::IsStringASCII(text_utf8)) {
[email protected]a2fedb1e2011-01-25 15:23:3661 *parts = parts_utf8;
62 return;
63 }
64
[email protected]9b5b1d602014-06-12 14:29:0265 parts->scheme = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.scheme);
66 parts->username =
[email protected]a2fedb1e2011-01-25 15:23:3667 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.username);
68 parts->password =
69 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.password);
[email protected]9b5b1d602014-06-12 14:29:0270 parts->host = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.host);
71 parts->port = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.port);
72 parts->path = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.path);
73 parts->query = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.query);
74 parts->ref = UTF8ComponentToUTF16Component(text_utf8, parts_utf8.ref);
[email protected]a2fedb1e2011-01-25 15:23:3675}
[email protected]a63801082009-04-08 04:28:2576
[email protected]8af69c6c2014-03-03 19:05:3177base::TrimPositions TrimWhitespaceUTF8(const std::string& input,
78 base::TrimPositions positions,
79 std::string* output) {
[email protected]f20dead2013-03-02 03:01:4880 // This implementation is not so fast since it converts the text encoding
[email protected]d0767cb542009-10-08 17:38:3081 // twice. Please feel free to file a bug if this function hurts the
82 // performance of Chrome.
[email protected]527965412014-05-07 14:38:2683 DCHECK(base::IsStringUTF8(input));
[email protected]036a5f32013-12-25 00:26:1184 base::string16 input16 = base::UTF8ToUTF16(input);
[email protected]428fac12013-12-05 21:38:4985 base::string16 output16;
[email protected]8af69c6c2014-03-03 19:05:3186 base::TrimPositions result =
87 base::TrimWhitespace(input16, positions, &output16);
[email protected]036a5f32013-12-25 00:26:1188 *output = base::UTF16ToUTF8(output16);
[email protected]d0767cb542009-10-08 17:38:3089 return result;
90}
91
initial.commit09911bf2008-07-26 23:55:2992// does some basic fixes for input that we want to test for file-ness
[email protected]ae8e3672013-03-20 09:00:0893void PrepareStringForFileOps(const base::FilePath& text,
94 base::FilePath::StringType* output) {
[email protected]b1c33f82009-01-23 01:51:2395#if defined(OS_WIN)
[email protected]8af69c6c2014-03-03 19:05:3196 base::TrimWhitespace(text.value(), base::TRIM_ALL, output);
initial.commit09911bf2008-07-26 23:55:2997 replace(output->begin(), output->end(), '/', '\\');
[email protected]94161ccf2009-08-19 09:22:5698#else
[email protected]8af69c6c2014-03-03 19:05:3199 TrimWhitespaceUTF8(text.value(), base::TRIM_ALL, output);
[email protected]b1c33f82009-01-23 01:51:23100#endif
initial.commit09911bf2008-07-26 23:55:29101}
102
103// Tries to create a full path from |text|. If the result is valid and the
104// file exists, returns true and sets |full_path| to the result. Otherwise,
105// returns false and leaves |full_path| unchanged.
[email protected]ae8e3672013-03-20 09:00:08106bool ValidPathForFile(const base::FilePath::StringType& text,
107 base::FilePath* full_path) {
[email protected]15476932013-04-12 05:17:15108 base::FilePath file_path = base::MakeAbsoluteFilePath(base::FilePath(text));
109 if (file_path.empty())
[email protected]6c56c992009-03-19 04:06:37110 return false;
[email protected]f20dead2013-03-02 03:01:48111
[email protected]7567484142013-07-11 17:36:07112 if (!base::PathExists(file_path))
initial.commit09911bf2008-07-26 23:55:29113 return false;
[email protected]f20dead2013-03-02 03:01:48114
[email protected]b1c33f82009-01-23 01:51:23115 *full_path = file_path;
initial.commit09911bf2008-07-26 23:55:29116 return true;
117}
118
[email protected]762c5542009-10-21 16:45:38119#if defined(OS_POSIX)
120// Given a path that starts with ~, return a path that starts with an
121// expanded-out /user/foobar directory.
[email protected]ae8e3672013-03-20 09:00:08122std::string FixupHomedir(const std::string& text) {
[email protected]762c5542009-10-21 16:45:38123 DCHECK(text.length() > 0 && text[0] == '~');
124
125 if (text.length() == 1 || text[1] == '/') {
skuhne66e02552014-08-29 00:13:56126 base::FilePath file_path;
[email protected]9b5b1d602014-06-12 14:29:02127 if (url_fixer::home_directory_override)
skuhne66e02552014-08-29 00:13:56128 file_path = base::FilePath(url_fixer::home_directory_override);
129 else
130 PathService::Get(base::DIR_HOME, &file_path);
131
[email protected]762c5542009-10-21 16:45:38132 // We'll probably break elsewhere if $HOME is undefined, but check here
133 // just in case.
skuhne66e02552014-08-29 00:13:56134 if (file_path.value().empty())
[email protected]762c5542009-10-21 16:45:38135 return text;
skuhne66e02552014-08-29 00:13:56136 // Append requires to be a relative path, so we have to cut all preceeding
137 // '/' characters.
138 size_t i = 1;
139 while (i < text.length() && text[i] == '/')
140 ++i;
141 return file_path.Append(text.substr(i)).value();
[email protected]762c5542009-10-21 16:45:38142 }
143
[email protected]9b5b1d602014-06-12 14:29:02144// Otherwise, this is a path like ~foobar/baz, where we must expand to
145// user foobar's home directory. Officially, we should use getpwent(),
146// but that is a nasty blocking call.
[email protected]f20dead2013-03-02 03:01:48147
[email protected]762c5542009-10-21 16:45:38148#if defined(OS_MACOSX)
149 static const char kHome[] = "/Users/";
150#else
151 static const char kHome[] = "/home/";
152#endif
153 return kHome + text.substr(1);
154}
155#endif
156
initial.commit09911bf2008-07-26 23:55:29157// Tries to create a file: URL from |text| if it looks like a filename, even if
[email protected]ce85f602009-11-07 01:34:53158// it doesn't resolve as a valid path or to an existing file. Returns a
159// (possibly invalid) file: URL in |fixed_up_url| for input beginning
160// with a drive specifier or "\\". Returns the unchanged input in other cases
161// (including file: URLs: these don't look like filenames).
[email protected]ae8e3672013-03-20 09:00:08162std::string FixupPath(const std::string& text) {
[email protected]ba1321d12009-04-21 22:42:29163 DCHECK(!text.empty());
initial.commit09911bf2008-07-26 23:55:29164
[email protected]650b2d52013-02-10 03:41:45165 base::FilePath::StringType filename;
[email protected]b1c33f82009-01-23 01:51:23166#if defined(OS_WIN)
[email protected]036a5f32013-12-25 00:26:11167 base::FilePath input_path(base::UTF8ToWide(text));
[email protected]b1c33f82009-01-23 01:51:23168 PrepareStringForFileOps(input_path, &filename);
initial.commit09911bf2008-07-26 23:55:29169
[email protected]ba1321d12009-04-21 22:42:29170 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|".
171 if (filename.length() > 1 && filename[1] == '|')
initial.commit09911bf2008-07-26 23:55:29172 filename[1] = ':';
[email protected]ba1321d12009-04-21 22:42:29173#elif defined(OS_POSIX)
[email protected]650b2d52013-02-10 03:41:45174 base::FilePath input_path(text);
[email protected]ba1321d12009-04-21 22:42:29175 PrepareStringForFileOps(input_path, &filename);
[email protected]762c5542009-10-21 16:45:38176 if (filename.length() > 0 && filename[0] == '~')
177 filename = FixupHomedir(filename);
[email protected]ba1321d12009-04-21 22:42:29178#endif
initial.commit09911bf2008-07-26 23:55:29179
180 // Here, we know the input looks like a file.
[email protected]650b2d52013-02-10 03:41:45181 GURL file_url = net::FilePathToFileURL(base::FilePath(filename));
[email protected]b1c33f82009-01-23 01:51:23182 if (file_url.is_valid()) {
[email protected]9b5b1d602014-06-12 14:29:02183 return base::UTF16ToUTF8(net::FormatUrl(file_url,
184 std::string(),
185 net::kFormatUrlOmitUsernamePassword,
186 net::UnescapeRule::NORMAL,
187 NULL,
188 NULL,
189 NULL));
[email protected]b1c33f82009-01-23 01:51:23190 }
initial.commit09911bf2008-07-26 23:55:29191
192 // Invalid file URL, just return the input.
193 return text;
194}
195
196// Checks |domain| to see if a valid TLD is already present. If not, appends
[email protected]f20dead2013-03-02 03:01:48197// |desired_tld| to the domain, and prepends "www." unless it's already present.
[email protected]ae8e3672013-03-20 09:00:08198void AddDesiredTLD(const std::string& desired_tld, std::string* domain) {
initial.commit09911bf2008-07-26 23:55:29199 if (desired_tld.empty() || domain->empty())
200 return;
201
202 // Check the TLD. If the return value is positive, we already have a TLD, so
[email protected]7e563812010-03-22 20:05:59203 // abort. If the return value is std::string::npos, there's no valid host,
204 // but we can try to append a TLD anyway, since the host may become valid once
205 // the TLD is attached -- for example, "999999999999" is detected as a broken
206 // IP address and marked invalid, but attaching ".com" makes it legal. When
207 // the return value is 0, there's a valid host with no known TLD, so we can
208 // definitely append the user's TLD. We disallow unknown registries here so
209 // users can input "mail.yahoo" and hit ctrl-enter to get
210 // "www.mail.yahoo.com".
initial.commit09911bf2008-07-26 23:55:29211 const size_t registry_length =
[email protected]ed32c212013-05-14 20:49:29212 net::registry_controlled_domains::GetRegistryLength(
213 *domain,
214 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
215 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
[email protected]7e563812010-03-22 20:05:59216 if ((registry_length != 0) && (registry_length != std::string::npos))
initial.commit09911bf2008-07-26 23:55:29217 return;
218
219 // Add the suffix at the end of the domain.
220 const size_t domain_length(domain->length());
[email protected]1cb92b82010-03-08 23:12:15221 DCHECK_GT(domain_length, 0U);
222 DCHECK_NE(desired_tld[0], '.');
initial.commit09911bf2008-07-26 23:55:29223 if ((*domain)[domain_length - 1] != '.')
224 domain->push_back('.');
225 domain->append(desired_tld);
226
227 // Now, if the domain begins with "www.", stop.
[email protected]7e563812010-03-22 20:05:59228 const std::string prefix("www.");
initial.commit09911bf2008-07-26 23:55:29229 if (domain->compare(0, prefix.length(), prefix) != 0) {
230 // Otherwise, add www. to the beginning of the URL.
231 domain->insert(0, prefix);
232 }
233}
234
[email protected]ae8e3672013-03-20 09:00:08235inline void FixupUsername(const std::string& text,
[email protected]b45334502014-04-30 19:44:05236 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08237 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29238 if (!part.is_valid())
239 return;
[email protected]f20dead2013-03-02 03:01:48240
initial.commit09911bf2008-07-26 23:55:29241 // We don't fix up the username at the moment.
242 url->append(text, part.begin, part.len);
243 // Do not append the trailing '@' because we might need to include the user's
244 // password. FixupURL itself will append the '@' for us.
245}
246
[email protected]ae8e3672013-03-20 09:00:08247inline void FixupPassword(const std::string& text,
[email protected]b45334502014-04-30 19:44:05248 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08249 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29250 if (!part.is_valid())
251 return;
[email protected]f20dead2013-03-02 03:01:48252
initial.commit09911bf2008-07-26 23:55:29253 // We don't fix up the password at the moment.
[email protected]b1c33f82009-01-23 01:51:23254 url->append(":");
initial.commit09911bf2008-07-26 23:55:29255 url->append(text, part.begin, part.len);
256}
257
[email protected]ae8e3672013-03-20 09:00:08258void FixupHost(const std::string& text,
[email protected]b45334502014-04-30 19:44:05259 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08260 bool has_scheme,
261 const std::string& desired_tld,
262 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29263 if (!part.is_valid())
264 return;
265
266 // Make domain valid.
267 // Strip all leading dots and all but one trailing dot, unless the user only
268 // typed dots, in which case their input is totally invalid and we should just
269 // leave it unchanged.
[email protected]7e563812010-03-22 20:05:59270 std::string domain(text, part.begin, part.len);
initial.commit09911bf2008-07-26 23:55:29271 const size_t first_nondot(domain.find_first_not_of('.'));
[email protected]7e563812010-03-22 20:05:59272 if (first_nondot != std::string::npos) {
initial.commit09911bf2008-07-26 23:55:29273 domain.erase(0, first_nondot);
274 size_t last_nondot(domain.find_last_not_of('.'));
[email protected]7e563812010-03-22 20:05:59275 DCHECK(last_nondot != std::string::npos);
[email protected]1cb92b82010-03-08 23:12:15276 last_nondot += 2; // Point at second period in ending string
initial.commit09911bf2008-07-26 23:55:29277 if (last_nondot < domain.length())
278 domain.erase(last_nondot);
279 }
280
281 // Add any user-specified TLD, if applicable.
282 AddDesiredTLD(desired_tld, &domain);
[email protected]f20dead2013-03-02 03:01:48283
initial.commit09911bf2008-07-26 23:55:29284 url->append(domain);
285}
286
[email protected]ae8e3672013-03-20 09:00:08287void FixupPort(const std::string& text,
[email protected]b45334502014-04-30 19:44:05288 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08289 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29290 if (!part.is_valid())
291 return;
[email protected]f20dead2013-03-02 03:01:48292
[email protected]ce85f602009-11-07 01:34:53293 // We don't fix up the port at the moment.
[email protected]b1c33f82009-01-23 01:51:23294 url->append(":");
[email protected]ce85f602009-11-07 01:34:53295 url->append(text, part.begin, part.len);
initial.commit09911bf2008-07-26 23:55:29296}
297
[email protected]ae8e3672013-03-20 09:00:08298inline void FixupPath(const std::string& text,
[email protected]b45334502014-04-30 19:44:05299 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08300 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29301 if (!part.is_valid() || part.len == 0) {
302 // We should always have a path.
[email protected]b1c33f82009-01-23 01:51:23303 url->append("/");
initial.commit09911bf2008-07-26 23:55:29304 return;
305 }
306
307 // Append the path as is.
308 url->append(text, part.begin, part.len);
309}
310
[email protected]ae8e3672013-03-20 09:00:08311inline void FixupQuery(const std::string& text,
[email protected]b45334502014-04-30 19:44:05312 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08313 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29314 if (!part.is_valid())
315 return;
[email protected]f20dead2013-03-02 03:01:48316
initial.commit09911bf2008-07-26 23:55:29317 // We don't fix up the query at the moment.
[email protected]b1c33f82009-01-23 01:51:23318 url->append("?");
initial.commit09911bf2008-07-26 23:55:29319 url->append(text, part.begin, part.len);
320}
321
[email protected]ae8e3672013-03-20 09:00:08322inline void FixupRef(const std::string& text,
[email protected]b45334502014-04-30 19:44:05323 const url::Component& part,
[email protected]ae8e3672013-03-20 09:00:08324 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29325 if (!part.is_valid())
326 return;
[email protected]f20dead2013-03-02 03:01:48327
initial.commit09911bf2008-07-26 23:55:29328 // We don't fix up the ref at the moment.
[email protected]b1c33f82009-01-23 01:51:23329 url->append("#");
initial.commit09911bf2008-07-26 23:55:29330 url->append(text, part.begin, part.len);
331}
332
[email protected]ae8e3672013-03-20 09:00:08333bool HasPort(const std::string& original_text,
[email protected]b45334502014-04-30 19:44:05334 const url::Component& scheme_component) {
initial.commit09911bf2008-07-26 23:55:29335 // Find the range between the ":" and the "/".
336 size_t port_start = scheme_component.end() + 1;
337 size_t port_end = port_start;
338 while ((port_end < original_text.length()) &&
[email protected]b45334502014-04-30 19:44:05339 !url::IsAuthorityTerminator(original_text[port_end]))
initial.commit09911bf2008-07-26 23:55:29340 ++port_end;
341 if (port_end == port_start)
342 return false;
343
344 // Scan the range to see if it is entirely digits.
345 for (size_t i = port_start; i < port_end; ++i) {
brettwb3413062015-06-24 00:39:02346 if (!base::IsAsciiDigit(original_text[i]))
initial.commit09911bf2008-07-26 23:55:29347 return false;
348 }
349
350 return true;
351}
352
[email protected]818071ce2009-05-18 01:25:25353// Try to extract a valid scheme from the beginning of |text|.
354// If successful, set |scheme_component| to the text range where the scheme
355// was located, and fill |canon_scheme| with its canonicalized form.
356// Otherwise, return false and leave the outputs in an indeterminate state.
[email protected]135c45122014-02-12 02:44:32357bool GetValidScheme(const std::string& text,
[email protected]b45334502014-04-30 19:44:05358 url::Component* scheme_component,
[email protected]ae8e3672013-03-20 09:00:08359 std::string* canon_scheme) {
[email protected]135c45122014-02-12 02:44:32360 canon_scheme->clear();
361
[email protected]818071ce2009-05-18 01:25:25362 // Locate everything up to (but not including) the first ':'
[email protected]9b5b1d602014-06-12 14:29:02363 if (!url::ExtractScheme(
364 text.data(), static_cast<int>(text.length()), scheme_component)) {
[email protected]818071ce2009-05-18 01:25:25365 return false;
[email protected]ae8e3672013-03-20 09:00:08366 }
[email protected]818071ce2009-05-18 01:25:25367
368 // Make sure the scheme contains only valid characters, and convert
369 // to lowercase. This also catches IPv6 literals like [::1], because
370 // brackets are not in the whitelist.
[email protected]b45334502014-04-30 19:44:05371 url::StdStringCanonOutput canon_scheme_output(canon_scheme);
372 url::Component canon_scheme_component;
[email protected]9b5b1d602014-06-12 14:29:02373 if (!url::CanonicalizeScheme(text.data(),
374 *scheme_component,
375 &canon_scheme_output,
376 &canon_scheme_component)) {
[email protected]818071ce2009-05-18 01:25:25377 return false;
[email protected]b45334502014-04-30 19:44:05378 }
[email protected]818071ce2009-05-18 01:25:25379
380 // Strip the ':', and any trailing buffer space.
381 DCHECK_EQ(0, canon_scheme_component.begin);
382 canon_scheme->erase(canon_scheme_component.len);
383
384 // We need to fix up the segmentation for "www.example.com:/". For this
385 // case, we guess that schemes with a "." are not actually schemes.
[email protected]7e563812010-03-22 20:05:59386 if (canon_scheme->find('.') != std::string::npos)
[email protected]818071ce2009-05-18 01:25:25387 return false;
388
389 // We need to fix up the segmentation for "www:123/". For this case, we
390 // will add an HTTP scheme later and make the URL parser happy.
391 // TODO(pkasting): Maybe we should try to use GURL's parser for this?
392 if (HasPort(text, *scheme_component))
393 return false;
394
395 // Everything checks out.
396 return true;
397}
398
[email protected]9b5b1d602014-06-12 14:29:02399// Performs the work for url_fixer::SegmentURL. |text| may be modified on
[email protected]ae8e3672013-03-20 09:00:08400// output on success: a semicolon following a valid scheme is replaced with a
401// colon.
[email protected]b45334502014-04-30 19:44:05402std::string SegmentURLInternal(std::string* text, url::Parsed* parts) {
initial.commit09911bf2008-07-26 23:55:29403 // Initialize the result.
[email protected]b45334502014-04-30 19:44:05404 *parts = url::Parsed();
initial.commit09911bf2008-07-26 23:55:29405
[email protected]7e563812010-03-22 20:05:59406 std::string trimmed;
[email protected]8af69c6c2014-03-03 19:05:31407 TrimWhitespaceUTF8(*text, base::TRIM_ALL, &trimmed);
initial.commit09911bf2008-07-26 23:55:29408 if (trimmed.empty())
[email protected]7e563812010-03-22 20:05:59409 return std::string(); // Nothing to segment.
initial.commit09911bf2008-07-26 23:55:29410
[email protected]b1c33f82009-01-23 01:51:23411#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29412 int trimmed_length = static_cast<int>(trimmed.length());
[email protected]b45334502014-04-30 19:44:05413 if (url::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) ||
414 url::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true))
[email protected]b1c33f82009-01-23 01:51:23415 return "file";
416#elif defined(OS_POSIX)
[email protected]650b2d52013-02-10 03:41:45417 if (base::FilePath::IsSeparator(trimmed.data()[0]) ||
418 trimmed.data()[0] == '~')
[email protected]b1c33f82009-01-23 01:51:23419 return "file";
420#endif
initial.commit09911bf2008-07-26 23:55:29421
422 // Otherwise, we need to look at things carefully.
[email protected]7e563812010-03-22 20:05:59423 std::string scheme;
[email protected]ae8e3672013-03-20 09:00:08424 if (!GetValidScheme(*text, &parts->scheme, &scheme)) {
425 // Try again if there is a ';' in the text. If changing it to a ':' results
426 // in a scheme being found, continue processing with the modified text.
427 bool found_scheme = false;
428 size_t semicolon = text->find(';');
429 if (semicolon != 0 && semicolon != std::string::npos) {
430 (*text)[semicolon] = ':';
431 if (GetValidScheme(*text, &parts->scheme, &scheme))
432 found_scheme = true;
433 else
434 (*text)[semicolon] = ';';
435 }
436 if (!found_scheme) {
437 // Couldn't determine the scheme, so just pick one.
438 parts->scheme.reset();
brettw95509312015-07-16 23:57:33439 scheme = base::StartsWith(*text, "ftp.",
440 base::CompareCase::INSENSITIVE_ASCII) ?
441 url::kFtpScheme : url::kHttpScheme;
[email protected]ae8e3672013-03-20 09:00:08442 }
[email protected]dcf7d352009-02-26 01:56:02443 }
initial.commit09911bf2008-07-26 23:55:29444
[email protected]89f550b2011-06-08 18:34:03445 // Proceed with about and chrome schemes, but not file or nonstandard schemes.
[email protected]9b5b1d602014-06-12 14:29:02446 if ((scheme != url::kAboutScheme) && (scheme != kChromeUIScheme) &&
[email protected]cca6f392014-05-28 21:32:26447 ((scheme == url::kFileScheme) ||
[email protected]8e09c7af2014-06-10 11:46:17448 !url::IsStandard(
449 scheme.c_str(),
450 url::Component(0, static_cast<int>(scheme.length()))))) {
initial.commit09911bf2008-07-26 23:55:29451 return scheme;
[email protected]b45334502014-04-30 19:44:05452 }
initial.commit09911bf2008-07-26 23:55:29453
[email protected]cca6f392014-05-28 21:32:26454 if (scheme == url::kFileSystemScheme) {
[email protected]f1f86392012-04-03 13:51:58455 // Have the GURL parser do the heavy lifting for us.
[email protected]9b5b1d602014-06-12 14:29:02456 url::ParseFileSystemURL(
457 text->data(), static_cast<int>(text->length()), parts);
[email protected]f1f86392012-04-03 13:51:58458 return scheme;
459 }
460
initial.commit09911bf2008-07-26 23:55:29461 if (parts->scheme.is_valid()) {
462 // Have the GURL parser do the heavy lifting for us.
[email protected]9b5b1d602014-06-12 14:29:02463 url::ParseStandardURL(
464 text->data(), static_cast<int>(text->length()), parts);
initial.commit09911bf2008-07-26 23:55:29465 return scheme;
466 }
467
468 // We need to add a scheme in order for ParseStandardURL to be happy.
469 // Find the first non-whitespace character.
[email protected]ae8e3672013-03-20 09:00:08470 std::string::iterator first_nonwhite = text->begin();
brettwb3413062015-06-24 00:39:02471 while ((first_nonwhite != text->end()) &&
472 base::IsUnicodeWhitespace(*first_nonwhite))
initial.commit09911bf2008-07-26 23:55:29473 ++first_nonwhite;
474
475 // Construct the text to parse by inserting the scheme.
[email protected]7e563812010-03-22 20:05:59476 std::string inserted_text(scheme);
[email protected]fb4fe0952014-06-05 09:44:24477 inserted_text.append(url::kStandardSchemeSeparator);
[email protected]ae8e3672013-03-20 09:00:08478 std::string text_to_parse(text->begin(), first_nonwhite);
initial.commit09911bf2008-07-26 23:55:29479 text_to_parse.append(inserted_text);
[email protected]ae8e3672013-03-20 09:00:08480 text_to_parse.append(first_nonwhite, text->end());
initial.commit09911bf2008-07-26 23:55:29481
482 // Have the GURL parser do the heavy lifting for us.
[email protected]9b5b1d602014-06-12 14:29:02483 url::ParseStandardURL(
484 text_to_parse.data(), static_cast<int>(text_to_parse.length()), parts);
initial.commit09911bf2008-07-26 23:55:29485
486 // Offset the results of the parse to match the original text.
487 const int offset = -static_cast<int>(inserted_text.length());
[email protected]9b5b1d602014-06-12 14:29:02488 url_fixer::OffsetComponent(offset, &parts->scheme);
489 url_fixer::OffsetComponent(offset, &parts->username);
490 url_fixer::OffsetComponent(offset, &parts->password);
491 url_fixer::OffsetComponent(offset, &parts->host);
492 url_fixer::OffsetComponent(offset, &parts->port);
493 url_fixer::OffsetComponent(offset, &parts->path);
494 url_fixer::OffsetComponent(offset, &parts->query);
495 url_fixer::OffsetComponent(offset, &parts->ref);
[email protected]f20dead2013-03-02 03:01:48496
initial.commit09911bf2008-07-26 23:55:29497 return scheme;
498}
499
[email protected]ae8e3672013-03-20 09:00:08500} // namespace
501
[email protected]9b5b1d602014-06-12 14:29:02502std::string url_fixer::SegmentURL(const std::string& text, url::Parsed* parts) {
[email protected]ae8e3672013-03-20 09:00:08503 std::string mutable_text(text);
504 return SegmentURLInternal(&mutable_text, parts);
505}
506
[email protected]9b5b1d602014-06-12 14:29:02507base::string16 url_fixer::SegmentURL(const base::string16& text,
508 url::Parsed* parts) {
[email protected]036a5f32013-12-25 00:26:11509 std::string text_utf8 = base::UTF16ToUTF8(text);
[email protected]b45334502014-04-30 19:44:05510 url::Parsed parts_utf8;
[email protected]ae8e3672013-03-20 09:00:08511 std::string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8);
512 UTF8PartsToUTF16Parts(text_utf8, parts_utf8, parts);
[email protected]036a5f32013-12-25 00:26:11513 return base::UTF8ToUTF16(scheme_utf8);
[email protected]ae8e3672013-03-20 09:00:08514}
515
[email protected]9b5b1d602014-06-12 14:29:02516GURL url_fixer::FixupURL(const std::string& text,
517 const std::string& desired_tld) {
[email protected]7e563812010-03-22 20:05:59518 std::string trimmed;
[email protected]8af69c6c2014-03-03 19:05:31519 TrimWhitespaceUTF8(text, base::TRIM_ALL, &trimmed);
initial.commit09911bf2008-07-26 23:55:29520 if (trimmed.empty())
[email protected]76e7da22010-06-18 22:44:49521 return GURL(); // Nothing here.
[email protected]f20dead2013-03-02 03:01:48522
initial.commit09911bf2008-07-26 23:55:29523 // Segment the URL.
[email protected]b45334502014-04-30 19:44:05524 url::Parsed parts;
[email protected]ae8e3672013-03-20 09:00:08525 std::string scheme(SegmentURLInternal(&trimmed, &parts));
initial.commit09911bf2008-07-26 23:55:29526
[email protected]76e7da22010-06-18 22:44:49527 // For view-source: URLs, we strip "view-source:", do fixup, and stick it back
528 // on. This allows us to handle things like "view-source:google.com".
[email protected]9b5b1d602014-06-12 14:29:02529 if (scheme == kViewSourceScheme) {
[email protected]76e7da22010-06-18 22:44:49530 // Reject "view-source:view-source:..." to avoid deep recursion.
[email protected]9b5b1d602014-06-12 14:29:02531 std::string view_source(kViewSourceScheme + std::string(":"));
brettw95509312015-07-16 23:57:33532 if (!base::StartsWith(text, view_source + view_source,
533 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]9b5b1d602014-06-12 14:29:02534 return GURL(kViewSourceScheme + std::string(":") +
535 FixupURL(trimmed.substr(scheme.length() + 1), desired_tld)
536 .possibly_invalid_spec());
[email protected]76e7da22010-06-18 22:44:49537 }
538 }
539
initial.commit09911bf2008-07-26 23:55:29540 // We handle the file scheme separately.
[email protected]cca6f392014-05-28 21:32:26541 if (scheme == url::kFileScheme)
[email protected]76e7da22010-06-18 22:44:49542 return GURL(parts.scheme.is_valid() ? text : FixupPath(text));
initial.commit09911bf2008-07-26 23:55:29543
[email protected]f1f86392012-04-03 13:51:58544 // We handle the filesystem scheme separately.
[email protected]cca6f392014-05-28 21:32:26545 if (scheme == url::kFileSystemScheme) {
[email protected]f1f86392012-04-03 13:51:58546 if (parts.inner_parsed() && parts.inner_parsed()->scheme.is_valid())
547 return GURL(text);
548 return GURL();
549 }
550
[email protected]89f550b2011-06-08 18:34:03551 // Parse and rebuild about: and chrome: URLs, except about:blank.
[email protected]8e09c7af2014-06-10 11:46:17552 bool chrome_url =
brettwbc17d2c82015-06-09 22:39:08553 !base::LowerCaseEqualsASCII(trimmed, url::kAboutBlankURL) &&
[email protected]9b5b1d602014-06-12 14:29:02554 ((scheme == url::kAboutScheme) || (scheme == kChromeUIScheme));
[email protected]89f550b2011-06-08 18:34:03555
initial.commit09911bf2008-07-26 23:55:29556 // For some schemes whose layouts we understand, we rebuild it.
[email protected]b45334502014-04-30 19:44:05557 if (chrome_url ||
558 url::IsStandard(scheme.c_str(),
559 url::Component(0, static_cast<int>(scheme.length())))) {
[email protected]89f550b2011-06-08 18:34:03560 // Replace the about: scheme with the chrome: scheme.
[email protected]9b5b1d602014-06-12 14:29:02561 std::string url(chrome_url ? kChromeUIScheme : scheme);
[email protected]fb4fe0952014-06-05 09:44:24562 url.append(url::kStandardSchemeSeparator);
initial.commit09911bf2008-07-26 23:55:29563
564 // We need to check whether the |username| is valid because it is our
565 // responsibility to append the '@' to delineate the user information from
566 // the host portion of the URL.
567 if (parts.username.is_valid()) {
568 FixupUsername(trimmed, parts.username, &url);
569 FixupPassword(trimmed, parts.password, &url);
[email protected]b1c33f82009-01-23 01:51:23570 url.append("@");
initial.commit09911bf2008-07-26 23:55:29571 }
572
573 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url);
[email protected]89f550b2011-06-08 18:34:03574 if (chrome_url && !parts.host.is_valid())
[email protected]9b5b1d602014-06-12 14:29:02575 url.append(kChromeUIDefaultHost);
initial.commit09911bf2008-07-26 23:55:29576 FixupPort(trimmed, parts.port, &url);
577 FixupPath(trimmed, parts.path, &url);
578 FixupQuery(trimmed, parts.query, &url);
579 FixupRef(trimmed, parts.ref, &url);
[email protected]f20dead2013-03-02 03:01:48580
[email protected]76e7da22010-06-18 22:44:49581 return GURL(url);
initial.commit09911bf2008-07-26 23:55:29582 }
583
584 // In the worst-case, we insert a scheme if the URL lacks one.
585 if (!parts.scheme.is_valid()) {
[email protected]7e563812010-03-22 20:05:59586 std::string fixed_scheme(scheme);
[email protected]fb4fe0952014-06-05 09:44:24587 fixed_scheme.append(url::kStandardSchemeSeparator);
initial.commit09911bf2008-07-26 23:55:29588 trimmed.insert(0, fixed_scheme);
589 }
[email protected]f20dead2013-03-02 03:01:48590
[email protected]76e7da22010-06-18 22:44:49591 return GURL(trimmed);
initial.commit09911bf2008-07-26 23:55:29592}
593
594// The rules are different here than for regular fixup, since we need to handle
595// input like "hello.html" and know to look in the current directory. Regular
596// fixup will look for cues that it is actually a file path before trying to
597// figure out what file it is. If our logic doesn't work, we will fall back on
598// regular fixup.
[email protected]9b5b1d602014-06-12 14:29:02599GURL url_fixer::FixupRelativeFile(const base::FilePath& base_dir,
600 const base::FilePath& text) {
[email protected]650b2d52013-02-10 03:41:45601 base::FilePath old_cur_directory;
initial.commit09911bf2008-07-26 23:55:29602 if (!base_dir.empty()) {
[email protected]b1c33f82009-01-23 01:51:23603 // Save the old current directory before we move to the new one.
[email protected]37b3c1992014-03-11 20:59:02604 base::GetCurrentDirectory(&old_cur_directory);
605 base::SetCurrentDirectory(base_dir);
initial.commit09911bf2008-07-26 23:55:29606 }
607
[email protected]b1c33f82009-01-23 01:51:23608 // Allow funny input with extra whitespace and the wrong kind of slashes.
[email protected]650b2d52013-02-10 03:41:45609 base::FilePath::StringType trimmed;
initial.commit09911bf2008-07-26 23:55:29610 PrepareStringForFileOps(text, &trimmed);
611
612 bool is_file = true;
[email protected]a64c3cf2011-08-06 05:25:55613 // Avoid recognizing definite non-file URLs as file paths.
614 GURL gurl(trimmed);
615 if (gurl.is_valid() && gurl.IsStandard())
616 is_file = false;
[email protected]650b2d52013-02-10 03:41:45617 base::FilePath full_path;
[email protected]a64c3cf2011-08-06 05:25:55618 if (is_file && !ValidPathForFile(trimmed, &full_path)) {
[email protected]9b5b1d602014-06-12 14:29:02619// Not a path as entered, try unescaping it in case the user has
620// escaped things. We need to go through 8-bit since the escaped values
621// only represent 8-bit values.
[email protected]b1c33f82009-01-23 01:51:23622#if defined(OS_WIN)
[email protected]036a5f32013-12-25 00:26:11623 std::wstring unescaped = base::UTF8ToWide(net::UnescapeURLComponent(
624 base::WideToUTF8(trimmed),
[email protected]b60ae4b02011-11-15 14:58:21625 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS));
[email protected]b1c33f82009-01-23 01:51:23626#elif defined(OS_POSIX)
[email protected]48797902011-10-02 23:05:08627 std::string unescaped = net::UnescapeURLComponent(
[email protected]b1c33f82009-01-23 01:51:23628 trimmed,
[email protected]b60ae4b02011-11-15 14:58:21629 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
[email protected]b1c33f82009-01-23 01:51:23630#endif
631
initial.commit09911bf2008-07-26 23:55:29632 if (!ValidPathForFile(unescaped, &full_path))
633 is_file = false;
634 }
635
636 // Put back the current directory if we saved it.
[email protected]76e7da22010-06-18 22:44:49637 if (!base_dir.empty())
[email protected]37b3c1992014-03-11 20:59:02638 base::SetCurrentDirectory(old_cur_directory);
initial.commit09911bf2008-07-26 23:55:29639
640 if (is_file) {
[email protected]8ac1a752008-07-31 19:40:37641 GURL file_url = net::FilePathToFileURL(full_path);
initial.commit09911bf2008-07-26 23:55:29642 if (file_url.is_valid())
[email protected]9b5b1d602014-06-12 14:29:02643 return GURL(
644 base::UTF16ToUTF8(net::FormatUrl(file_url,
645 std::string(),
646 net::kFormatUrlOmitUsernamePassword,
647 net::UnescapeRule::NORMAL,
648 NULL,
649 NULL,
650 NULL)));
initial.commit09911bf2008-07-26 23:55:29651 // Invalid files fall through to regular processing.
652 }
653
[email protected]9b5b1d602014-06-12 14:29:02654// Fall back on regular fixup for this input.
[email protected]b1c33f82009-01-23 01:51:23655#if defined(OS_WIN)
[email protected]036a5f32013-12-25 00:26:11656 std::string text_utf8 = base::WideToUTF8(text.value());
[email protected]b1c33f82009-01-23 01:51:23657#elif defined(OS_POSIX)
[email protected]7e563812010-03-22 20:05:59658 std::string text_utf8 = text.value();
[email protected]b1c33f82009-01-23 01:51:23659#endif
[email protected]76e7da22010-06-18 22:44:49660 return FixupURL(text_utf8, std::string());
initial.commit09911bf2008-07-26 23:55:29661}
license.botbf09a502008-08-24 00:55:55662
[email protected]9b5b1d602014-06-12 14:29:02663void url_fixer::OffsetComponent(int offset, url::Component* part) {
[email protected]f20dead2013-03-02 03:01:48664 DCHECK(part);
665
666 if (part->is_valid()) {
[email protected]d1e83b32010-12-22 00:34:35667 // Offset the location of this component.
668 part->begin += offset;
[email protected]f20dead2013-03-02 03:01:48669
[email protected]d1e83b32010-12-22 00:34:35670 // This part might not have existed in the original text.
671 if (part->begin < 0)
672 part->reset();
673 }
674}
[email protected]8f6e5322014-08-11 08:06:08675
676bool url_fixer::IsEquivalentScheme(const std::string& scheme1,
677 const std::string& scheme2) {
678 return scheme1 == scheme2 ||
679 (scheme1 == url::kAboutScheme && scheme2 == kChromeUIScheme) ||
680 (scheme1 == kChromeUIScheme && scheme2 == url::kAboutScheme);
681}