1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/url_formatter/url_fixer.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
13 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "components/url_formatter/url_formatter.h"
18 #include "net/base/escape.h"
19 #include "net/base/filename_util.h"
20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
21 #include "url/third_party/mozilla/url_parse.h"
22 #include "url/url_file.h"
23 #include "url/url_util.h"
25 namespace url_formatter
{
27 const char* home_directory_override
= nullptr;
31 // Hardcode these constants to avoid dependences on //chrome and //content.
32 const char kChromeUIScheme
[] = "chrome";
33 const char kChromeUIDefaultHost
[] = "version";
34 const char kViewSourceScheme
[] = "view-source";
36 // TODO(estade): Remove these ugly, ugly functions. They are only used in
37 // SegmentURL. A url::Parsed object keeps track of a bunch of indices into
38 // a url string, and these need to be updated when the URL is converted from
39 // UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it
40 // in the correct string format to begin with.
41 url::Component
UTF8ComponentToUTF16Component(
42 const std::string
& text_utf8
,
43 const url::Component
& component_utf8
) {
44 if (component_utf8
.len
== -1)
45 return url::Component();
47 std::string before_component_string
=
48 text_utf8
.substr(0, component_utf8
.begin
);
49 std::string component_string
=
50 text_utf8
.substr(component_utf8
.begin
, component_utf8
.len
);
51 base::string16 before_component_string_16
=
52 base::UTF8ToUTF16(before_component_string
);
53 base::string16 component_string_16
= base::UTF8ToUTF16(component_string
);
54 url::Component
component_16(before_component_string_16
.length(),
55 component_string_16
.length());
59 void UTF8PartsToUTF16Parts(const std::string
& text_utf8
,
60 const url::Parsed
& parts_utf8
,
62 if (base::IsStringASCII(text_utf8
)) {
67 parts
->scheme
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.scheme
);
69 UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.username
);
71 UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.password
);
72 parts
->host
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.host
);
73 parts
->port
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.port
);
74 parts
->path
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.path
);
75 parts
->query
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.query
);
76 parts
->ref
= UTF8ComponentToUTF16Component(text_utf8
, parts_utf8
.ref
);
79 base::TrimPositions
TrimWhitespaceUTF8(const std::string
& input
,
80 base::TrimPositions positions
,
81 std::string
* output
) {
82 // This implementation is not so fast since it converts the text encoding
83 // twice. Please feel free to file a bug if this function hurts the
84 // performance of Chrome.
85 DCHECK(base::IsStringUTF8(input
));
86 base::string16 input16
= base::UTF8ToUTF16(input
);
87 base::string16 output16
;
88 base::TrimPositions result
=
89 base::TrimWhitespace(input16
, positions
, &output16
);
90 *output
= base::UTF16ToUTF8(output16
);
94 // does some basic fixes for input that we want to test for file-ness
95 void PrepareStringForFileOps(const base::FilePath
& text
,
96 base::FilePath::StringType
* output
) {
98 base::TrimWhitespace(text
.value(), base::TRIM_ALL
, output
);
99 replace(output
->begin(), output
->end(), '/', '\\');
101 TrimWhitespaceUTF8(text
.value(), base::TRIM_ALL
, output
);
105 // Tries to create a full path from |text|. If the result is valid and the
106 // file exists, returns true and sets |full_path| to the result. Otherwise,
107 // returns false and leaves |full_path| unchanged.
108 bool ValidPathForFile(const base::FilePath::StringType
& text
,
109 base::FilePath
* full_path
) {
110 base::FilePath file_path
= base::MakeAbsoluteFilePath(base::FilePath(text
));
111 if (file_path
.empty())
114 if (!base::PathExists(file_path
))
117 *full_path
= file_path
;
121 #if defined(OS_POSIX)
122 // Given a path that starts with ~, return a path that starts with an
123 // expanded-out /user/foobar directory.
124 std::string
FixupHomedir(const std::string
& text
) {
125 DCHECK(text
.length() > 0 && text
[0] == '~');
127 if (text
.length() == 1 || text
[1] == '/') {
128 base::FilePath file_path
;
129 if (home_directory_override
)
130 file_path
= base::FilePath(home_directory_override
);
132 PathService::Get(base::DIR_HOME
, &file_path
);
134 // We'll probably break elsewhere if $HOME is undefined, but check here
136 if (file_path
.value().empty())
138 // Append requires to be a relative path, so we have to cut all preceeding
141 while (i
< text
.length() && text
[i
] == '/')
143 return file_path
.Append(text
.substr(i
)).value();
146 // Otherwise, this is a path like ~foobar/baz, where we must expand to
147 // user foobar's home directory. Officially, we should use getpwent(),
148 // but that is a nasty blocking call.
150 #if defined(OS_MACOSX)
151 static const char kHome
[] = "/Users/";
153 static const char kHome
[] = "/home/";
155 return kHome
+ text
.substr(1);
159 // Tries to create a file: URL from |text| if it looks like a filename, even if
160 // it doesn't resolve as a valid path or to an existing file. Returns a
161 // (possibly invalid) file: URL in |fixed_up_url| for input beginning
162 // with a drive specifier or "\\". Returns the unchanged input in other cases
163 // (including file: URLs: these don't look like filenames).
164 std::string
FixupPath(const std::string
& text
) {
165 DCHECK(!text
.empty());
167 base::FilePath::StringType filename
;
169 base::FilePath
input_path(base::UTF8ToWide(text
));
170 PrepareStringForFileOps(input_path
, &filename
);
172 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|".
173 if (filename
.length() > 1 && filename
[1] == '|')
175 #elif defined(OS_POSIX)
176 base::FilePath
input_path(text
);
177 PrepareStringForFileOps(input_path
, &filename
);
178 if (filename
.length() > 0 && filename
[0] == '~')
179 filename
= FixupHomedir(filename
);
182 // Here, we know the input looks like a file.
183 GURL file_url
= net::FilePathToFileURL(base::FilePath(filename
));
184 if (file_url
.is_valid()) {
185 return base::UTF16ToUTF8(url_formatter::FormatUrl(
186 file_url
, std::string(), url_formatter::kFormatUrlOmitUsernamePassword
,
187 net::UnescapeRule::NORMAL
, nullptr, nullptr, nullptr));
190 // Invalid file URL, just return the input.
194 // Checks |domain| to see if a valid TLD is already present. If not, appends
195 // |desired_tld| to the domain, and prepends "www." unless it's already present.
196 void AddDesiredTLD(const std::string
& desired_tld
, std::string
* domain
) {
197 if (desired_tld
.empty() || domain
->empty())
200 // Check the TLD. If the return value is positive, we already have a TLD, so
201 // abort. If the return value is std::string::npos, there's no valid host,
202 // but we can try to append a TLD anyway, since the host may become valid once
203 // the TLD is attached -- for example, "999999999999" is detected as a broken
204 // IP address and marked invalid, but attaching ".com" makes it legal. When
205 // the return value is 0, there's a valid host with no known TLD, so we can
206 // definitely append the user's TLD. We disallow unknown registries here so
207 // users can input "mail.yahoo" and hit ctrl-enter to get
208 // "www.mail.yahoo.com".
209 const size_t registry_length
=
210 net::registry_controlled_domains::GetRegistryLength(
211 *domain
, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
212 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
213 if ((registry_length
!= 0) && (registry_length
!= std::string::npos
))
216 // Add the suffix at the end of the domain.
217 const size_t domain_length(domain
->length());
218 DCHECK_GT(domain_length
, 0U);
219 DCHECK_NE(desired_tld
[0], '.');
220 if ((*domain
)[domain_length
- 1] != '.')
221 domain
->push_back('.');
222 domain
->append(desired_tld
);
224 // Now, if the domain begins with "www.", stop.
225 const std::string
prefix("www.");
226 if (domain
->compare(0, prefix
.length(), prefix
) != 0) {
227 // Otherwise, add www. to the beginning of the URL.
228 domain
->insert(0, prefix
);
232 inline void FixupUsername(const std::string
& text
,
233 const url::Component
& part
,
235 if (!part
.is_valid())
238 // We don't fix up the username at the moment.
239 url
->append(text
, part
.begin
, part
.len
);
240 // Do not append the trailing '@' because we might need to include the user's
241 // password. FixupURL itself will append the '@' for us.
244 inline void FixupPassword(const std::string
& text
,
245 const url::Component
& part
,
247 if (!part
.is_valid())
250 // We don't fix up the password at the moment.
252 url
->append(text
, part
.begin
, part
.len
);
255 void FixupHost(const std::string
& text
,
256 const url::Component
& part
,
258 const std::string
& desired_tld
,
260 if (!part
.is_valid())
263 // Make domain valid.
264 // Strip all leading dots and all but one trailing dot, unless the user only
265 // typed dots, in which case their input is totally invalid and we should just
266 // leave it unchanged.
267 std::string
domain(text
, part
.begin
, part
.len
);
268 const size_t first_nondot(domain
.find_first_not_of('.'));
269 if (first_nondot
!= std::string::npos
) {
270 domain
.erase(0, first_nondot
);
271 size_t last_nondot(domain
.find_last_not_of('.'));
272 DCHECK(last_nondot
!= std::string::npos
);
273 last_nondot
+= 2; // Point at second period in ending string
274 if (last_nondot
< domain
.length())
275 domain
.erase(last_nondot
);
278 // Add any user-specified TLD, if applicable.
279 AddDesiredTLD(desired_tld
, &domain
);
284 void FixupPort(const std::string
& text
,
285 const url::Component
& part
,
287 if (!part
.is_valid())
290 // We don't fix up the port at the moment.
292 url
->append(text
, part
.begin
, part
.len
);
295 inline void FixupPath(const std::string
& text
,
296 const url::Component
& part
,
298 if (!part
.is_valid() || part
.len
== 0) {
299 // We should always have a path.
304 // Append the path as is.
305 url
->append(text
, part
.begin
, part
.len
);
308 inline void FixupQuery(const std::string
& text
,
309 const url::Component
& part
,
311 if (!part
.is_valid())
314 // We don't fix up the query at the moment.
316 url
->append(text
, part
.begin
, part
.len
);
319 inline void FixupRef(const std::string
& text
,
320 const url::Component
& part
,
322 if (!part
.is_valid())
325 // We don't fix up the ref at the moment.
327 url
->append(text
, part
.begin
, part
.len
);
330 bool HasPort(const std::string
& original_text
,
331 const url::Component
& scheme_component
) {
332 // Find the range between the ":" and the "/".
333 size_t port_start
= scheme_component
.end() + 1;
334 size_t port_end
= port_start
;
335 while ((port_end
< original_text
.length()) &&
336 !url::IsAuthorityTerminator(original_text
[port_end
]))
338 if (port_end
== port_start
)
341 // Scan the range to see if it is entirely digits.
342 for (size_t i
= port_start
; i
< port_end
; ++i
) {
343 if (!base::IsAsciiDigit(original_text
[i
]))
350 // Try to extract a valid scheme from the beginning of |text|.
351 // If successful, set |scheme_component| to the text range where the scheme
352 // was located, and fill |canon_scheme| with its canonicalized form.
353 // Otherwise, return false and leave the outputs in an indeterminate state.
354 bool GetValidScheme(const std::string
& text
,
355 url::Component
* scheme_component
,
356 std::string
* canon_scheme
) {
357 canon_scheme
->clear();
359 // Locate everything up to (but not including) the first ':'
360 if (!url::ExtractScheme(text
.data(), static_cast<int>(text
.length()),
365 // Make sure the scheme contains only valid characters, and convert
366 // to lowercase. This also catches IPv6 literals like [::1], because
367 // brackets are not in the whitelist.
368 url::StdStringCanonOutput
canon_scheme_output(canon_scheme
);
369 url::Component canon_scheme_component
;
370 if (!url::CanonicalizeScheme(text
.data(), *scheme_component
,
371 &canon_scheme_output
, &canon_scheme_component
)) {
375 // Strip the ':', and any trailing buffer space.
376 DCHECK_EQ(0, canon_scheme_component
.begin
);
377 canon_scheme
->erase(canon_scheme_component
.len
);
379 // We need to fix up the segmentation for "www.example.com:/". For this
380 // case, we guess that schemes with a "." are not actually schemes.
381 if (canon_scheme
->find('.') != std::string::npos
)
384 // We need to fix up the segmentation for "www:123/". For this case, we
385 // will add an HTTP scheme later and make the URL parser happy.
386 // TODO(pkasting): Maybe we should try to use GURL's parser for this?
387 if (HasPort(text
, *scheme_component
))
390 // Everything checks out.
394 // Performs the work for url_formatter::SegmentURL. |text| may be modified on
395 // output on success: a semicolon following a valid scheme is replaced with a
397 std::string
SegmentURLInternal(std::string
* text
, url::Parsed
* parts
) {
398 // Initialize the result.
399 *parts
= url::Parsed();
402 TrimWhitespaceUTF8(*text
, base::TRIM_ALL
, &trimmed
);
404 return std::string(); // Nothing to segment.
407 int trimmed_length
= static_cast<int>(trimmed
.length());
408 if (url::DoesBeginWindowsDriveSpec(trimmed
.data(), 0, trimmed_length
) ||
409 url::DoesBeginUNCPath(trimmed
.data(), 0, trimmed_length
, true))
411 #elif defined(OS_POSIX)
412 if (base::FilePath::IsSeparator(trimmed
.data()[0]) ||
413 trimmed
.data()[0] == '~')
417 // Otherwise, we need to look at things carefully.
419 if (!GetValidScheme(*text
, &parts
->scheme
, &scheme
)) {
420 // Try again if there is a ';' in the text. If changing it to a ':' results
421 // in a scheme being found, continue processing with the modified text.
422 bool found_scheme
= false;
423 size_t semicolon
= text
->find(';');
424 if (semicolon
!= 0 && semicolon
!= std::string::npos
) {
425 (*text
)[semicolon
] = ':';
426 if (GetValidScheme(*text
, &parts
->scheme
, &scheme
))
429 (*text
)[semicolon
] = ';';
432 // Couldn't determine the scheme, so just pick one.
433 parts
->scheme
.reset();
435 base::StartsWith(*text
, "ftp.", base::CompareCase::INSENSITIVE_ASCII
)
441 // Proceed with about and chrome schemes, but not file or nonstandard schemes.
442 if ((scheme
!= url::kAboutScheme
) && (scheme
!= kChromeUIScheme
) &&
443 ((scheme
== url::kFileScheme
) ||
446 url::Component(0, static_cast<int>(scheme
.length()))))) {
450 if (scheme
== url::kFileSystemScheme
) {
451 // Have the GURL parser do the heavy lifting for us.
452 url::ParseFileSystemURL(text
->data(), static_cast<int>(text
->length()),
457 if (parts
->scheme
.is_valid()) {
458 // Have the GURL parser do the heavy lifting for us.
459 url::ParseStandardURL(text
->data(), static_cast<int>(text
->length()),
464 // We need to add a scheme in order for ParseStandardURL to be happy.
465 // Find the first non-whitespace character.
466 std::string::iterator first_nonwhite
= text
->begin();
467 while ((first_nonwhite
!= text
->end()) &&
468 base::IsUnicodeWhitespace(*first_nonwhite
))
471 // Construct the text to parse by inserting the scheme.
472 std::string
inserted_text(scheme
);
473 inserted_text
.append(url::kStandardSchemeSeparator
);
474 std::string
text_to_parse(text
->begin(), first_nonwhite
);
475 text_to_parse
.append(inserted_text
);
476 text_to_parse
.append(first_nonwhite
, text
->end());
478 // Have the GURL parser do the heavy lifting for us.
479 url::ParseStandardURL(text_to_parse
.data(),
480 static_cast<int>(text_to_parse
.length()), parts
);
482 // Offset the results of the parse to match the original text.
483 const int offset
= -static_cast<int>(inserted_text
.length());
484 OffsetComponent(offset
, &parts
->scheme
);
485 OffsetComponent(offset
, &parts
->username
);
486 OffsetComponent(offset
, &parts
->password
);
487 OffsetComponent(offset
, &parts
->host
);
488 OffsetComponent(offset
, &parts
->port
);
489 OffsetComponent(offset
, &parts
->path
);
490 OffsetComponent(offset
, &parts
->query
);
491 OffsetComponent(offset
, &parts
->ref
);
498 std::string
SegmentURL(const std::string
& text
, url::Parsed
* parts
) {
499 std::string
mutable_text(text
);
500 return SegmentURLInternal(&mutable_text
, parts
);
503 base::string16
SegmentURL(const base::string16
& text
, url::Parsed
* parts
) {
504 std::string text_utf8
= base::UTF16ToUTF8(text
);
505 url::Parsed parts_utf8
;
506 std::string scheme_utf8
= SegmentURL(text_utf8
, &parts_utf8
);
507 UTF8PartsToUTF16Parts(text_utf8
, parts_utf8
, parts
);
508 return base::UTF8ToUTF16(scheme_utf8
);
511 GURL
FixupURL(const std::string
& text
, const std::string
& desired_tld
) {
513 TrimWhitespaceUTF8(text
, base::TRIM_ALL
, &trimmed
);
515 return GURL(); // Nothing here.
519 std::string
scheme(SegmentURLInternal(&trimmed
, &parts
));
521 // For view-source: URLs, we strip "view-source:", do fixup, and stick it back
522 // on. This allows us to handle things like "view-source:google.com".
523 if (scheme
== kViewSourceScheme
) {
524 // Reject "view-source:view-source:..." to avoid deep recursion.
525 std::string
view_source(kViewSourceScheme
+ std::string(":"));
526 if (!base::StartsWith(text
, view_source
+ view_source
,
527 base::CompareCase::INSENSITIVE_ASCII
)) {
528 return GURL(kViewSourceScheme
+ std::string(":") +
529 FixupURL(trimmed
.substr(scheme
.length() + 1), desired_tld
)
530 .possibly_invalid_spec());
534 // We handle the file scheme separately.
535 if (scheme
== url::kFileScheme
)
536 return GURL(parts
.scheme
.is_valid() ? text
: FixupPath(text
));
538 // We handle the filesystem scheme separately.
539 if (scheme
== url::kFileSystemScheme
) {
540 if (parts
.inner_parsed() && parts
.inner_parsed()->scheme
.is_valid())
545 // Parse and rebuild about: and chrome: URLs, except about:blank.
547 !base::LowerCaseEqualsASCII(trimmed
, url::kAboutBlankURL
) &&
548 ((scheme
== url::kAboutScheme
) || (scheme
== kChromeUIScheme
));
550 // For some schemes whose layouts we understand, we rebuild it.
552 url::IsStandard(scheme
.c_str(),
553 url::Component(0, static_cast<int>(scheme
.length())))) {
554 // Replace the about: scheme with the chrome: scheme.
555 std::string
url(chrome_url
? kChromeUIScheme
: scheme
);
556 url
.append(url::kStandardSchemeSeparator
);
558 // We need to check whether the |username| is valid because it is our
559 // responsibility to append the '@' to delineate the user information from
560 // the host portion of the URL.
561 if (parts
.username
.is_valid()) {
562 FixupUsername(trimmed
, parts
.username
, &url
);
563 FixupPassword(trimmed
, parts
.password
, &url
);
567 FixupHost(trimmed
, parts
.host
, parts
.scheme
.is_valid(), desired_tld
, &url
);
568 if (chrome_url
&& !parts
.host
.is_valid())
569 url
.append(kChromeUIDefaultHost
);
570 FixupPort(trimmed
, parts
.port
, &url
);
571 FixupPath(trimmed
, parts
.path
, &url
);
572 FixupQuery(trimmed
, parts
.query
, &url
);
573 FixupRef(trimmed
, parts
.ref
, &url
);
578 // In the worst-case, we insert a scheme if the URL lacks one.
579 if (!parts
.scheme
.is_valid()) {
580 std::string
fixed_scheme(scheme
);
581 fixed_scheme
.append(url::kStandardSchemeSeparator
);
582 trimmed
.insert(0, fixed_scheme
);
585 return GURL(trimmed
);
588 // The rules are different here than for regular fixup, since we need to handle
589 // input like "hello.html" and know to look in the current directory. Regular
590 // fixup will look for cues that it is actually a file path before trying to
591 // figure out what file it is. If our logic doesn't work, we will fall back on
593 GURL
FixupRelativeFile(const base::FilePath
& base_dir
,
594 const base::FilePath
& text
) {
595 base::FilePath old_cur_directory
;
596 if (!base_dir
.empty()) {
597 // Save the old current directory before we move to the new one.
598 base::GetCurrentDirectory(&old_cur_directory
);
599 base::SetCurrentDirectory(base_dir
);
602 // Allow funny input with extra whitespace and the wrong kind of slashes.
603 base::FilePath::StringType trimmed
;
604 PrepareStringForFileOps(text
, &trimmed
);
607 // Avoid recognizing definite non-file URLs as file paths.
609 if (gurl
.is_valid() && gurl
.IsStandard())
611 base::FilePath full_path
;
612 if (is_file
&& !ValidPathForFile(trimmed
, &full_path
)) {
613 // Not a path as entered, try unescaping it in case the user has
614 // escaped things. We need to go through 8-bit since the escaped values
615 // only represent 8-bit values.
617 std::wstring unescaped
= base::UTF8ToWide(net::UnescapeURLComponent(
618 base::WideToUTF8(trimmed
),
619 net::UnescapeRule::SPACES
| net::UnescapeRule::URL_SPECIAL_CHARS
));
620 #elif defined(OS_POSIX)
621 std::string unescaped
= net::UnescapeURLComponent(
623 net::UnescapeRule::SPACES
| net::UnescapeRule::URL_SPECIAL_CHARS
);
626 if (!ValidPathForFile(unescaped
, &full_path
))
630 // Put back the current directory if we saved it.
631 if (!base_dir
.empty())
632 base::SetCurrentDirectory(old_cur_directory
);
635 GURL file_url
= net::FilePathToFileURL(full_path
);
636 if (file_url
.is_valid())
637 return GURL(base::UTF16ToUTF8(url_formatter::FormatUrl(
638 file_url
, std::string(),
639 url_formatter::kFormatUrlOmitUsernamePassword
,
640 net::UnescapeRule::NORMAL
, nullptr, nullptr, nullptr)));
641 // Invalid files fall through to regular processing.
644 // Fall back on regular fixup for this input.
646 std::string text_utf8
= base::WideToUTF8(text
.value());
647 #elif defined(OS_POSIX)
648 std::string text_utf8
= text
.value();
650 return FixupURL(text_utf8
, std::string());
653 void OffsetComponent(int offset
, url::Component
* part
) {
656 if (part
->is_valid()) {
657 // Offset the location of this component.
658 part
->begin
+= offset
;
660 // This part might not have existed in the original text.
666 bool IsEquivalentScheme(const std::string
& scheme1
,
667 const std::string
& scheme2
) {
668 return scheme1
== scheme2
||
669 (scheme1
== url::kAboutScheme
&& scheme2
== kChromeUIScheme
) ||
670 (scheme1
== kChromeUIScheme
&& scheme2
== url::kAboutScheme
);
673 } // namespace url_formatter