blob: 6c82ba4c8d72fff2c45b47943264e5be588b8cb0 [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]f870a322009-01-16 21:47:275#include "chrome/browser/net/url_fixer_upper.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]91e81ae2009-05-08 22:14:387#include <algorithm>
8
[email protected]c5212892010-09-08 06:30:339#if defined(OS_POSIX)
[email protected]76b90d312010-08-03 03:00:5010#include "base/environment.h"
[email protected]c5212892010-09-08 06:30:3311#endif
initial.commit09911bf2008-07-26 23:55:2912#include "base/file_util.h"
13#include "base/logging.h"
[email protected]f9b294362013-06-10 20:22:3114#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1815#include "base/strings/utf_string_conversions.h"
[email protected]dcf7d352009-02-26 01:56:0216#include "chrome/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2917#include "net/base/escape.h"
18#include "net/base/net_util.h"
[email protected]be28b5f42012-07-20 11:31:2519#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]761fa4702013-07-02 15:25:1520#include "url/url_file.h"
21#include "url/url_parse.h"
22#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2923
[email protected]762c5542009-10-21 16:45:3824const char* URLFixerUpper::home_directory_override = NULL;
25
[email protected]a63801082009-04-08 04:28:2526namespace {
27
[email protected]f20dead2013-03-02 03:01:4828// TODO(estade): Remove these ugly, ugly functions. They are only used in
29// SegmentURL. A url_parse::Parsed object keeps track of a bunch of indices into
30// a url string, and these need to be updated when the URL is converted from
[email protected]a63801082009-04-08 04:28:2531// UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it
32// in the correct string format to begin with.
[email protected]a2fedb1e2011-01-25 15:23:3633url_parse::Component UTF8ComponentToUTF16Component(
34 const std::string& text_utf8,
35 const url_parse::Component& component_utf8) {
[email protected]f20dead2013-03-02 03:01:4836 if (component_utf8.len == -1)
[email protected]a2fedb1e2011-01-25 15:23:3637 return url_parse::Component();
38
39 std::string before_component_string =
40 text_utf8.substr(0, component_utf8.begin);
41 std::string component_string = text_utf8.substr(component_utf8.begin,
42 component_utf8.len);
43 string16 before_component_string_16 = UTF8ToUTF16(before_component_string);
44 string16 component_string_16 = UTF8ToUTF16(component_string);
45 url_parse::Component component_16(before_component_string_16.length(),
46 component_string_16.length());
47 return component_16;
48}
49
50void UTF8PartsToUTF16Parts(const std::string& text_utf8,
51 const url_parse::Parsed& parts_utf8,
52 url_parse::Parsed* parts) {
53 if (IsStringASCII(text_utf8)) {
54 *parts = parts_utf8;
55 return;
56 }
57
58 parts->scheme =
59 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.scheme);
60 parts ->username =
61 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.username);
62 parts->password =
63 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.password);
64 parts->host =
65 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.host);
66 parts->port =
67 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.port);
68 parts->path =
69 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.path);
70 parts->query =
71 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.query);
72 parts->ref =
73 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.ref);
74}
[email protected]a63801082009-04-08 04:28:2575
[email protected]d0767cb542009-10-08 17:38:3076TrimPositions TrimWhitespaceUTF8(const std::string& input,
77 TrimPositions positions,
78 std::string* output) {
[email protected]f20dead2013-03-02 03:01:4879 // This implementation is not so fast since it converts the text encoding
[email protected]d0767cb542009-10-08 17:38:3080 // twice. Please feel free to file a bug if this function hurts the
81 // performance of Chrome.
82 DCHECK(IsStringUTF8(input));
[email protected]cf81d2d2011-05-10 17:42:1383 string16 input16 = UTF8ToUTF16(input);
84 string16 output16;
85 TrimPositions result = TrimWhitespace(input16, positions, &output16);
86 *output = UTF16ToUTF8(output16);
[email protected]d0767cb542009-10-08 17:38:3087 return result;
88}
89
initial.commit09911bf2008-07-26 23:55:2990// does some basic fixes for input that we want to test for file-ness
[email protected]ae8e3672013-03-20 09:00:0891void PrepareStringForFileOps(const base::FilePath& text,
92 base::FilePath::StringType* output) {
[email protected]b1c33f82009-01-23 01:51:2393#if defined(OS_WIN)
[email protected]94161ccf2009-08-19 09:22:5694 TrimWhitespace(text.value(), TRIM_ALL, output);
initial.commit09911bf2008-07-26 23:55:2995 replace(output->begin(), output->end(), '/', '\\');
[email protected]94161ccf2009-08-19 09:22:5696#else
97 TrimWhitespaceUTF8(text.value(), TRIM_ALL, output);
[email protected]b1c33f82009-01-23 01:51:2398#endif
initial.commit09911bf2008-07-26 23:55:2999}
100
101// Tries to create a full path from |text|. If the result is valid and the
102// file exists, returns true and sets |full_path| to the result. Otherwise,
103// returns false and leaves |full_path| unchanged.
[email protected]ae8e3672013-03-20 09:00:08104bool ValidPathForFile(const base::FilePath::StringType& text,
105 base::FilePath* full_path) {
[email protected]15476932013-04-12 05:17:15106 base::FilePath file_path = base::MakeAbsoluteFilePath(base::FilePath(text));
107 if (file_path.empty())
[email protected]6c56c992009-03-19 04:06:37108 return false;
[email protected]f20dead2013-03-02 03:01:48109
initial.commit09911bf2008-07-26 23:55:29110 if (!file_util::PathExists(file_path))
111 return false;
[email protected]f20dead2013-03-02 03:01:48112
[email protected]b1c33f82009-01-23 01:51:23113 *full_path = file_path;
initial.commit09911bf2008-07-26 23:55:29114 return true;
115}
116
[email protected]762c5542009-10-21 16:45:38117#if defined(OS_POSIX)
118// Given a path that starts with ~, return a path that starts with an
119// expanded-out /user/foobar directory.
[email protected]ae8e3672013-03-20 09:00:08120std::string FixupHomedir(const std::string& text) {
[email protected]762c5542009-10-21 16:45:38121 DCHECK(text.length() > 0 && text[0] == '~');
122
123 if (text.length() == 1 || text[1] == '/') {
[email protected]574f6f0c2010-07-21 02:59:28124 const char* home = getenv(base::env_vars::kHome);
[email protected]762c5542009-10-21 16:45:38125 if (URLFixerUpper::home_directory_override)
126 home = URLFixerUpper::home_directory_override;
127 // We'll probably break elsewhere if $HOME is undefined, but check here
128 // just in case.
129 if (!home)
130 return text;
131 return home + text.substr(1);
132 }
133
134 // Otherwise, this is a path like ~foobar/baz, where we must expand to
135 // user foobar's home directory. Officially, we should use getpwent(),
136 // but that is a nasty blocking call.
[email protected]f20dead2013-03-02 03:01:48137
[email protected]762c5542009-10-21 16:45:38138#if defined(OS_MACOSX)
139 static const char kHome[] = "/Users/";
140#else
141 static const char kHome[] = "/home/";
142#endif
143 return kHome + text.substr(1);
144}
145#endif
146
initial.commit09911bf2008-07-26 23:55:29147// Tries to create a file: URL from |text| if it looks like a filename, even if
[email protected]ce85f602009-11-07 01:34:53148// it doesn't resolve as a valid path or to an existing file. Returns a
149// (possibly invalid) file: URL in |fixed_up_url| for input beginning
150// with a drive specifier or "\\". Returns the unchanged input in other cases
151// (including file: URLs: these don't look like filenames).
[email protected]ae8e3672013-03-20 09:00:08152std::string FixupPath(const std::string& text) {
[email protected]ba1321d12009-04-21 22:42:29153 DCHECK(!text.empty());
initial.commit09911bf2008-07-26 23:55:29154
[email protected]650b2d52013-02-10 03:41:45155 base::FilePath::StringType filename;
[email protected]b1c33f82009-01-23 01:51:23156#if defined(OS_WIN)
[email protected]47e870b2013-02-24 21:14:53157 base::FilePath input_path(UTF8ToWide(text));
[email protected]b1c33f82009-01-23 01:51:23158 PrepareStringForFileOps(input_path, &filename);
initial.commit09911bf2008-07-26 23:55:29159
[email protected]ba1321d12009-04-21 22:42:29160 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|".
161 if (filename.length() > 1 && filename[1] == '|')
initial.commit09911bf2008-07-26 23:55:29162 filename[1] = ':';
[email protected]ba1321d12009-04-21 22:42:29163#elif defined(OS_POSIX)
[email protected]650b2d52013-02-10 03:41:45164 base::FilePath input_path(text);
[email protected]ba1321d12009-04-21 22:42:29165 PrepareStringForFileOps(input_path, &filename);
[email protected]762c5542009-10-21 16:45:38166 if (filename.length() > 0 && filename[0] == '~')
167 filename = FixupHomedir(filename);
[email protected]ba1321d12009-04-21 22:42:29168#endif
initial.commit09911bf2008-07-26 23:55:29169
170 // Here, we know the input looks like a file.
[email protected]650b2d52013-02-10 03:41:45171 GURL file_url = net::FilePathToFileURL(base::FilePath(filename));
[email protected]b1c33f82009-01-23 01:51:23172 if (file_url.is_valid()) {
[email protected]9f284f132010-08-31 06:14:17173 return UTF16ToUTF8(net::FormatUrl(file_url, std::string(),
[email protected]b60ae4b02011-11-15 14:58:21174 net::kFormatUrlOmitUsernamePassword, net::UnescapeRule::NORMAL, NULL,
[email protected]69c579e2010-04-23 20:01:00175 NULL, NULL));
[email protected]b1c33f82009-01-23 01:51:23176 }
initial.commit09911bf2008-07-26 23:55:29177
178 // Invalid file URL, just return the input.
179 return text;
180}
181
182// Checks |domain| to see if a valid TLD is already present. If not, appends
[email protected]f20dead2013-03-02 03:01:48183// |desired_tld| to the domain, and prepends "www." unless it's already present.
[email protected]ae8e3672013-03-20 09:00:08184void AddDesiredTLD(const std::string& desired_tld, std::string* domain) {
initial.commit09911bf2008-07-26 23:55:29185 if (desired_tld.empty() || domain->empty())
186 return;
187
188 // Check the TLD. If the return value is positive, we already have a TLD, so
[email protected]7e563812010-03-22 20:05:59189 // abort. If the return value is std::string::npos, there's no valid host,
190 // but we can try to append a TLD anyway, since the host may become valid once
191 // the TLD is attached -- for example, "999999999999" is detected as a broken
192 // IP address and marked invalid, but attaching ".com" makes it legal. When
193 // the return value is 0, there's a valid host with no known TLD, so we can
194 // definitely append the user's TLD. We disallow unknown registries here so
195 // users can input "mail.yahoo" and hit ctrl-enter to get
196 // "www.mail.yahoo.com".
initial.commit09911bf2008-07-26 23:55:29197 const size_t registry_length =
[email protected]ed32c212013-05-14 20:49:29198 net::registry_controlled_domains::GetRegistryLength(
199 *domain,
200 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
201 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
[email protected]7e563812010-03-22 20:05:59202 if ((registry_length != 0) && (registry_length != std::string::npos))
initial.commit09911bf2008-07-26 23:55:29203 return;
204
205 // Add the suffix at the end of the domain.
206 const size_t domain_length(domain->length());
[email protected]1cb92b82010-03-08 23:12:15207 DCHECK_GT(domain_length, 0U);
208 DCHECK_NE(desired_tld[0], '.');
initial.commit09911bf2008-07-26 23:55:29209 if ((*domain)[domain_length - 1] != '.')
210 domain->push_back('.');
211 domain->append(desired_tld);
212
213 // Now, if the domain begins with "www.", stop.
[email protected]7e563812010-03-22 20:05:59214 const std::string prefix("www.");
initial.commit09911bf2008-07-26 23:55:29215 if (domain->compare(0, prefix.length(), prefix) != 0) {
216 // Otherwise, add www. to the beginning of the URL.
217 domain->insert(0, prefix);
218 }
219}
220
[email protected]ae8e3672013-03-20 09:00:08221inline void FixupUsername(const std::string& text,
222 const url_parse::Component& part,
223 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29224 if (!part.is_valid())
225 return;
[email protected]f20dead2013-03-02 03:01:48226
initial.commit09911bf2008-07-26 23:55:29227 // We don't fix up the username at the moment.
228 url->append(text, part.begin, part.len);
229 // Do not append the trailing '@' because we might need to include the user's
230 // password. FixupURL itself will append the '@' for us.
231}
232
[email protected]ae8e3672013-03-20 09:00:08233inline void FixupPassword(const std::string& text,
234 const url_parse::Component& part,
235 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29236 if (!part.is_valid())
237 return;
[email protected]f20dead2013-03-02 03:01:48238
initial.commit09911bf2008-07-26 23:55:29239 // We don't fix up the password at the moment.
[email protected]b1c33f82009-01-23 01:51:23240 url->append(":");
initial.commit09911bf2008-07-26 23:55:29241 url->append(text, part.begin, part.len);
242}
243
[email protected]ae8e3672013-03-20 09:00:08244void FixupHost(const std::string& text,
245 const url_parse::Component& part,
246 bool has_scheme,
247 const std::string& desired_tld,
248 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29249 if (!part.is_valid())
250 return;
251
252 // Make domain valid.
253 // Strip all leading dots and all but one trailing dot, unless the user only
254 // typed dots, in which case their input is totally invalid and we should just
255 // leave it unchanged.
[email protected]7e563812010-03-22 20:05:59256 std::string domain(text, part.begin, part.len);
initial.commit09911bf2008-07-26 23:55:29257 const size_t first_nondot(domain.find_first_not_of('.'));
[email protected]7e563812010-03-22 20:05:59258 if (first_nondot != std::string::npos) {
initial.commit09911bf2008-07-26 23:55:29259 domain.erase(0, first_nondot);
260 size_t last_nondot(domain.find_last_not_of('.'));
[email protected]7e563812010-03-22 20:05:59261 DCHECK(last_nondot != std::string::npos);
[email protected]1cb92b82010-03-08 23:12:15262 last_nondot += 2; // Point at second period in ending string
initial.commit09911bf2008-07-26 23:55:29263 if (last_nondot < domain.length())
264 domain.erase(last_nondot);
265 }
266
267 // Add any user-specified TLD, if applicable.
268 AddDesiredTLD(desired_tld, &domain);
[email protected]f20dead2013-03-02 03:01:48269
initial.commit09911bf2008-07-26 23:55:29270 url->append(domain);
271}
272
[email protected]ae8e3672013-03-20 09:00:08273void FixupPort(const std::string& text,
274 const url_parse::Component& part,
275 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29276 if (!part.is_valid())
277 return;
[email protected]f20dead2013-03-02 03:01:48278
[email protected]ce85f602009-11-07 01:34:53279 // We don't fix up the port at the moment.
[email protected]b1c33f82009-01-23 01:51:23280 url->append(":");
[email protected]ce85f602009-11-07 01:34:53281 url->append(text, part.begin, part.len);
initial.commit09911bf2008-07-26 23:55:29282}
283
[email protected]ae8e3672013-03-20 09:00:08284inline void FixupPath(const std::string& text,
285 const url_parse::Component& part,
286 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29287 if (!part.is_valid() || part.len == 0) {
288 // We should always have a path.
[email protected]b1c33f82009-01-23 01:51:23289 url->append("/");
initial.commit09911bf2008-07-26 23:55:29290 return;
291 }
292
293 // Append the path as is.
294 url->append(text, part.begin, part.len);
295}
296
[email protected]ae8e3672013-03-20 09:00:08297inline void FixupQuery(const std::string& text,
298 const url_parse::Component& part,
299 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29300 if (!part.is_valid())
301 return;
[email protected]f20dead2013-03-02 03:01:48302
initial.commit09911bf2008-07-26 23:55:29303 // We don't fix up the query at the moment.
[email protected]b1c33f82009-01-23 01:51:23304 url->append("?");
initial.commit09911bf2008-07-26 23:55:29305 url->append(text, part.begin, part.len);
306}
307
[email protected]ae8e3672013-03-20 09:00:08308inline void FixupRef(const std::string& text,
309 const url_parse::Component& part,
310 std::string* url) {
initial.commit09911bf2008-07-26 23:55:29311 if (!part.is_valid())
312 return;
[email protected]f20dead2013-03-02 03:01:48313
initial.commit09911bf2008-07-26 23:55:29314 // We don't fix up the ref at the moment.
[email protected]b1c33f82009-01-23 01:51:23315 url->append("#");
initial.commit09911bf2008-07-26 23:55:29316 url->append(text, part.begin, part.len);
317}
318
[email protected]ae8e3672013-03-20 09:00:08319bool HasPort(const std::string& original_text,
320 const url_parse::Component& scheme_component) {
initial.commit09911bf2008-07-26 23:55:29321 // Find the range between the ":" and the "/".
322 size_t port_start = scheme_component.end() + 1;
323 size_t port_end = port_start;
324 while ((port_end < original_text.length()) &&
325 !url_parse::IsAuthorityTerminator(original_text[port_end]))
326 ++port_end;
327 if (port_end == port_start)
328 return false;
329
330 // Scan the range to see if it is entirely digits.
331 for (size_t i = port_start; i < port_end; ++i) {
332 if (!IsAsciiDigit(original_text[i]))
333 return false;
334 }
335
336 return true;
337}
338
[email protected]818071ce2009-05-18 01:25:25339// Try to extract a valid scheme from the beginning of |text|.
340// If successful, set |scheme_component| to the text range where the scheme
341// was located, and fill |canon_scheme| with its canonicalized form.
342// Otherwise, return false and leave the outputs in an indeterminate state.
[email protected]ae8e3672013-03-20 09:00:08343bool GetValidScheme(const std::string &text,
344 url_parse::Component* scheme_component,
345 std::string* canon_scheme) {
[email protected]818071ce2009-05-18 01:25:25346 // Locate everything up to (but not including) the first ':'
347 if (!url_parse::ExtractScheme(text.data(), static_cast<int>(text.length()),
[email protected]ae8e3672013-03-20 09:00:08348 scheme_component)) {
[email protected]818071ce2009-05-18 01:25:25349 return false;
[email protected]ae8e3672013-03-20 09:00:08350 }
[email protected]818071ce2009-05-18 01:25:25351
352 // Make sure the scheme contains only valid characters, and convert
353 // to lowercase. This also catches IPv6 literals like [::1], because
354 // brackets are not in the whitelist.
355 url_canon::StdStringCanonOutput canon_scheme_output(canon_scheme);
356 url_parse::Component canon_scheme_component;
357 if (!url_canon::CanonicalizeScheme(text.data(), *scheme_component,
358 &canon_scheme_output,
359 &canon_scheme_component))
360 return false;
361
362 // Strip the ':', and any trailing buffer space.
363 DCHECK_EQ(0, canon_scheme_component.begin);
364 canon_scheme->erase(canon_scheme_component.len);
365
366 // We need to fix up the segmentation for "www.example.com:/". For this
367 // case, we guess that schemes with a "." are not actually schemes.
[email protected]7e563812010-03-22 20:05:59368 if (canon_scheme->find('.') != std::string::npos)
[email protected]818071ce2009-05-18 01:25:25369 return false;
370
371 // We need to fix up the segmentation for "www:123/". For this case, we
372 // will add an HTTP scheme later and make the URL parser happy.
373 // TODO(pkasting): Maybe we should try to use GURL's parser for this?
374 if (HasPort(text, *scheme_component))
375 return false;
376
377 // Everything checks out.
378 return true;
379}
380
[email protected]ae8e3672013-03-20 09:00:08381// Performs the work for URLFixerUpper::SegmentURL. |text| may be modified on
382// output on success: a semicolon following a valid scheme is replaced with a
383// colon.
384std::string SegmentURLInternal(std::string* text, url_parse::Parsed* parts) {
initial.commit09911bf2008-07-26 23:55:29385 // Initialize the result.
386 *parts = url_parse::Parsed();
387
[email protected]7e563812010-03-22 20:05:59388 std::string trimmed;
[email protected]ae8e3672013-03-20 09:00:08389 TrimWhitespaceUTF8(*text, TRIM_ALL, &trimmed);
initial.commit09911bf2008-07-26 23:55:29390 if (trimmed.empty())
[email protected]7e563812010-03-22 20:05:59391 return std::string(); // Nothing to segment.
initial.commit09911bf2008-07-26 23:55:29392
[email protected]b1c33f82009-01-23 01:51:23393#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29394 int trimmed_length = static_cast<int>(trimmed.length());
[email protected]b1c33f82009-01-23 01:51:23395 if (url_parse::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) ||
[email protected]7fc13ed2010-03-06 05:06:20396 url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true))
[email protected]b1c33f82009-01-23 01:51:23397 return "file";
398#elif defined(OS_POSIX)
[email protected]650b2d52013-02-10 03:41:45399 if (base::FilePath::IsSeparator(trimmed.data()[0]) ||
400 trimmed.data()[0] == '~')
[email protected]b1c33f82009-01-23 01:51:23401 return "file";
402#endif
initial.commit09911bf2008-07-26 23:55:29403
404 // Otherwise, we need to look at things carefully.
[email protected]7e563812010-03-22 20:05:59405 std::string scheme;
[email protected]ae8e3672013-03-20 09:00:08406 if (!GetValidScheme(*text, &parts->scheme, &scheme)) {
407 // Try again if there is a ';' in the text. If changing it to a ':' results
408 // in a scheme being found, continue processing with the modified text.
409 bool found_scheme = false;
410 size_t semicolon = text->find(';');
411 if (semicolon != 0 && semicolon != std::string::npos) {
412 (*text)[semicolon] = ':';
413 if (GetValidScheme(*text, &parts->scheme, &scheme))
414 found_scheme = true;
415 else
416 (*text)[semicolon] = ';';
417 }
418 if (!found_scheme) {
419 // Couldn't determine the scheme, so just pick one.
420 parts->scheme.reset();
421 scheme.assign(StartsWithASCII(*text, "ftp.", false) ?
422 chrome::kFtpScheme : chrome::kHttpScheme);
423 }
[email protected]dcf7d352009-02-26 01:56:02424 }
initial.commit09911bf2008-07-26 23:55:29425
[email protected]89f550b2011-06-08 18:34:03426 // Proceed with about and chrome schemes, but not file or nonstandard schemes.
427 if ((scheme != chrome::kAboutScheme) && (scheme != chrome::kChromeUIScheme) &&
428 ((scheme == chrome::kFileScheme) || !url_util::IsStandard(scheme.c_str(),
429 url_parse::Component(0, static_cast<int>(scheme.length())))))
initial.commit09911bf2008-07-26 23:55:29430 return scheme;
431
[email protected]f1f86392012-04-03 13:51:58432 if (scheme == chrome::kFileSystemScheme) {
433 // Have the GURL parser do the heavy lifting for us.
[email protected]ae8e3672013-03-20 09:00:08434 url_parse::ParseFileSystemURL(text->data(),
435 static_cast<int>(text->length()), parts);
[email protected]f1f86392012-04-03 13:51:58436 return scheme;
437 }
438
initial.commit09911bf2008-07-26 23:55:29439 if (parts->scheme.is_valid()) {
440 // Have the GURL parser do the heavy lifting for us.
[email protected]ae8e3672013-03-20 09:00:08441 url_parse::ParseStandardURL(text->data(), static_cast<int>(text->length()),
initial.commit09911bf2008-07-26 23:55:29442 parts);
443 return scheme;
444 }
445
446 // We need to add a scheme in order for ParseStandardURL to be happy.
447 // Find the first non-whitespace character.
[email protected]ae8e3672013-03-20 09:00:08448 std::string::iterator first_nonwhite = text->begin();
449 while ((first_nonwhite != text->end()) && IsWhitespace(*first_nonwhite))
initial.commit09911bf2008-07-26 23:55:29450 ++first_nonwhite;
451
452 // Construct the text to parse by inserting the scheme.
[email protected]7e563812010-03-22 20:05:59453 std::string inserted_text(scheme);
[email protected]fea79efe2012-05-02 01:14:01454 inserted_text.append(content::kStandardSchemeSeparator);
[email protected]ae8e3672013-03-20 09:00:08455 std::string text_to_parse(text->begin(), first_nonwhite);
initial.commit09911bf2008-07-26 23:55:29456 text_to_parse.append(inserted_text);
[email protected]ae8e3672013-03-20 09:00:08457 text_to_parse.append(first_nonwhite, text->end());
initial.commit09911bf2008-07-26 23:55:29458
459 // Have the GURL parser do the heavy lifting for us.
[email protected]91136d32008-12-16 20:34:39460 url_parse::ParseStandardURL(text_to_parse.data(),
461 static_cast<int>(text_to_parse.length()),
initial.commit09911bf2008-07-26 23:55:29462 parts);
463
464 // Offset the results of the parse to match the original text.
465 const int offset = -static_cast<int>(inserted_text.length());
[email protected]ae8e3672013-03-20 09:00:08466 URLFixerUpper::OffsetComponent(offset, &parts->scheme);
467 URLFixerUpper::OffsetComponent(offset, &parts->username);
468 URLFixerUpper::OffsetComponent(offset, &parts->password);
469 URLFixerUpper::OffsetComponent(offset, &parts->host);
470 URLFixerUpper::OffsetComponent(offset, &parts->port);
471 URLFixerUpper::OffsetComponent(offset, &parts->path);
472 URLFixerUpper::OffsetComponent(offset, &parts->query);
473 URLFixerUpper::OffsetComponent(offset, &parts->ref);
[email protected]f20dead2013-03-02 03:01:48474
initial.commit09911bf2008-07-26 23:55:29475 return scheme;
476}
477
[email protected]ae8e3672013-03-20 09:00:08478} // namespace
479
480std::string URLFixerUpper::SegmentURL(const std::string& text,
481 url_parse::Parsed* parts) {
482 std::string mutable_text(text);
483 return SegmentURLInternal(&mutable_text, parts);
484}
485
486string16 URLFixerUpper::SegmentURL(const string16& text,
487 url_parse::Parsed* parts) {
488 std::string text_utf8 = UTF16ToUTF8(text);
489 url_parse::Parsed parts_utf8;
490 std::string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8);
491 UTF8PartsToUTF16Parts(text_utf8, parts_utf8, parts);
492 return UTF8ToUTF16(scheme_utf8);
493}
494
[email protected]76e7da22010-06-18 22:44:49495GURL URLFixerUpper::FixupURL(const std::string& text,
496 const std::string& desired_tld) {
[email protected]7e563812010-03-22 20:05:59497 std::string trimmed;
[email protected]94161ccf2009-08-19 09:22:56498 TrimWhitespaceUTF8(text, TRIM_ALL, &trimmed);
initial.commit09911bf2008-07-26 23:55:29499 if (trimmed.empty())
[email protected]76e7da22010-06-18 22:44:49500 return GURL(); // Nothing here.
[email protected]f20dead2013-03-02 03:01:48501
initial.commit09911bf2008-07-26 23:55:29502 // Segment the URL.
503 url_parse::Parsed parts;
[email protected]ae8e3672013-03-20 09:00:08504 std::string scheme(SegmentURLInternal(&trimmed, &parts));
initial.commit09911bf2008-07-26 23:55:29505
[email protected]76e7da22010-06-18 22:44:49506 // For view-source: URLs, we strip "view-source:", do fixup, and stick it back
507 // on. This allows us to handle things like "view-source:google.com".
[email protected]dbdda5402013-05-30 22:13:48508 if (scheme == content::kViewSourceScheme) {
[email protected]76e7da22010-06-18 22:44:49509 // Reject "view-source:view-source:..." to avoid deep recursion.
[email protected]dbdda5402013-05-30 22:13:48510 std::string view_source(content::kViewSourceScheme + std::string(":"));
[email protected]76e7da22010-06-18 22:44:49511 if (!StartsWithASCII(text, view_source + view_source, false)) {
[email protected]dbdda5402013-05-30 22:13:48512 return GURL(content::kViewSourceScheme + std::string(":") +
[email protected]76e7da22010-06-18 22:44:49513 FixupURL(trimmed.substr(scheme.length() + 1),
514 desired_tld).possibly_invalid_spec());
515 }
516 }
517
initial.commit09911bf2008-07-26 23:55:29518 // We handle the file scheme separately.
[email protected]76e7da22010-06-18 22:44:49519 if (scheme == chrome::kFileScheme)
520 return GURL(parts.scheme.is_valid() ? text : FixupPath(text));
initial.commit09911bf2008-07-26 23:55:29521
[email protected]f1f86392012-04-03 13:51:58522 // We handle the filesystem scheme separately.
523 if (scheme == chrome::kFileSystemScheme) {
524 if (parts.inner_parsed() && parts.inner_parsed()->scheme.is_valid())
525 return GURL(text);
526 return GURL();
527 }
528
[email protected]89f550b2011-06-08 18:34:03529 // Parse and rebuild about: and chrome: URLs, except about:blank.
[email protected]081dc522013-05-15 04:59:20530 bool chrome_url = !LowerCaseEqualsASCII(trimmed, content::kAboutBlankURL) &&
[email protected]89f550b2011-06-08 18:34:03531 ((scheme == chrome::kAboutScheme) || (scheme == chrome::kChromeUIScheme));
532
initial.commit09911bf2008-07-26 23:55:29533 // For some schemes whose layouts we understand, we rebuild it.
[email protected]89f550b2011-06-08 18:34:03534 if (chrome_url || url_util::IsStandard(scheme.c_str(),
[email protected]91136d32008-12-16 20:34:39535 url_parse::Component(0, static_cast<int>(scheme.length())))) {
[email protected]89f550b2011-06-08 18:34:03536 // Replace the about: scheme with the chrome: scheme.
537 std::string url(chrome_url ? chrome::kChromeUIScheme : scheme);
[email protected]fea79efe2012-05-02 01:14:01538 url.append(content::kStandardSchemeSeparator);
initial.commit09911bf2008-07-26 23:55:29539
540 // We need to check whether the |username| is valid because it is our
541 // responsibility to append the '@' to delineate the user information from
542 // the host portion of the URL.
543 if (parts.username.is_valid()) {
544 FixupUsername(trimmed, parts.username, &url);
545 FixupPassword(trimmed, parts.password, &url);
[email protected]b1c33f82009-01-23 01:51:23546 url.append("@");
initial.commit09911bf2008-07-26 23:55:29547 }
548
549 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url);
[email protected]89f550b2011-06-08 18:34:03550 if (chrome_url && !parts.host.is_valid())
551 url.append(chrome::kChromeUIDefaultHost);
initial.commit09911bf2008-07-26 23:55:29552 FixupPort(trimmed, parts.port, &url);
553 FixupPath(trimmed, parts.path, &url);
554 FixupQuery(trimmed, parts.query, &url);
555 FixupRef(trimmed, parts.ref, &url);
[email protected]f20dead2013-03-02 03:01:48556
[email protected]76e7da22010-06-18 22:44:49557 return GURL(url);
initial.commit09911bf2008-07-26 23:55:29558 }
559
560 // In the worst-case, we insert a scheme if the URL lacks one.
561 if (!parts.scheme.is_valid()) {
[email protected]7e563812010-03-22 20:05:59562 std::string fixed_scheme(scheme);
[email protected]fea79efe2012-05-02 01:14:01563 fixed_scheme.append(content::kStandardSchemeSeparator);
initial.commit09911bf2008-07-26 23:55:29564 trimmed.insert(0, fixed_scheme);
565 }
[email protected]f20dead2013-03-02 03:01:48566
[email protected]76e7da22010-06-18 22:44:49567 return GURL(trimmed);
initial.commit09911bf2008-07-26 23:55:29568}
569
570// The rules are different here than for regular fixup, since we need to handle
571// input like "hello.html" and know to look in the current directory. Regular
572// fixup will look for cues that it is actually a file path before trying to
573// figure out what file it is. If our logic doesn't work, we will fall back on
574// regular fixup.
[email protected]650b2d52013-02-10 03:41:45575GURL URLFixerUpper::FixupRelativeFile(const base::FilePath& base_dir,
576 const base::FilePath& text) {
577 base::FilePath old_cur_directory;
initial.commit09911bf2008-07-26 23:55:29578 if (!base_dir.empty()) {
[email protected]b1c33f82009-01-23 01:51:23579 // Save the old current directory before we move to the new one.
580 file_util::GetCurrentDirectory(&old_cur_directory);
581 file_util::SetCurrentDirectory(base_dir);
initial.commit09911bf2008-07-26 23:55:29582 }
583
[email protected]b1c33f82009-01-23 01:51:23584 // Allow funny input with extra whitespace and the wrong kind of slashes.
[email protected]650b2d52013-02-10 03:41:45585 base::FilePath::StringType trimmed;
initial.commit09911bf2008-07-26 23:55:29586 PrepareStringForFileOps(text, &trimmed);
587
588 bool is_file = true;
[email protected]a64c3cf2011-08-06 05:25:55589 // Avoid recognizing definite non-file URLs as file paths.
590 GURL gurl(trimmed);
591 if (gurl.is_valid() && gurl.IsStandard())
592 is_file = false;
[email protected]650b2d52013-02-10 03:41:45593 base::FilePath full_path;
[email protected]a64c3cf2011-08-06 05:25:55594 if (is_file && !ValidPathForFile(trimmed, &full_path)) {
initial.commit09911bf2008-07-26 23:55:29595 // Not a path as entered, try unescaping it in case the user has
596 // escaped things. We need to go through 8-bit since the escaped values
597 // only represent 8-bit values.
[email protected]b1c33f82009-01-23 01:51:23598#if defined(OS_WIN)
[email protected]47e870b2013-02-24 21:14:53599 std::wstring unescaped = UTF8ToWide(net::UnescapeURLComponent(
600 WideToUTF8(trimmed),
[email protected]b60ae4b02011-11-15 14:58:21601 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS));
[email protected]b1c33f82009-01-23 01:51:23602#elif defined(OS_POSIX)
[email protected]48797902011-10-02 23:05:08603 std::string unescaped = net::UnescapeURLComponent(
[email protected]b1c33f82009-01-23 01:51:23604 trimmed,
[email protected]b60ae4b02011-11-15 14:58:21605 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
[email protected]b1c33f82009-01-23 01:51:23606#endif
607
initial.commit09911bf2008-07-26 23:55:29608 if (!ValidPathForFile(unescaped, &full_path))
609 is_file = false;
610 }
611
612 // Put back the current directory if we saved it.
[email protected]76e7da22010-06-18 22:44:49613 if (!base_dir.empty())
[email protected]b1c33f82009-01-23 01:51:23614 file_util::SetCurrentDirectory(old_cur_directory);
initial.commit09911bf2008-07-26 23:55:29615
616 if (is_file) {
[email protected]8ac1a752008-07-31 19:40:37617 GURL file_url = net::FilePathToFileURL(full_path);
initial.commit09911bf2008-07-26 23:55:29618 if (file_url.is_valid())
[email protected]9f284f132010-08-31 06:14:17619 return GURL(UTF16ToUTF8(net::FormatUrl(file_url, std::string(),
[email protected]b60ae4b02011-11-15 14:58:21620 net::kFormatUrlOmitUsernamePassword, net::UnescapeRule::NORMAL, NULL,
[email protected]76e7da22010-06-18 22:44:49621 NULL, NULL)));
initial.commit09911bf2008-07-26 23:55:29622 // Invalid files fall through to regular processing.
623 }
624
625 // Fall back on regular fixup for this input.
[email protected]b1c33f82009-01-23 01:51:23626#if defined(OS_WIN)
[email protected]47e870b2013-02-24 21:14:53627 std::string text_utf8 = WideToUTF8(text.value());
[email protected]b1c33f82009-01-23 01:51:23628#elif defined(OS_POSIX)
[email protected]7e563812010-03-22 20:05:59629 std::string text_utf8 = text.value();
[email protected]b1c33f82009-01-23 01:51:23630#endif
[email protected]76e7da22010-06-18 22:44:49631 return FixupURL(text_utf8, std::string());
initial.commit09911bf2008-07-26 23:55:29632}
license.botbf09a502008-08-24 00:55:55633
[email protected]d1e83b32010-12-22 00:34:35634void URLFixerUpper::OffsetComponent(int offset, url_parse::Component* part) {
[email protected]f20dead2013-03-02 03:01:48635 DCHECK(part);
636
637 if (part->is_valid()) {
[email protected]d1e83b32010-12-22 00:34:35638 // Offset the location of this component.
639 part->begin += offset;
[email protected]f20dead2013-03-02 03:01:48640
[email protected]d1e83b32010-12-22 00:34:35641 // This part might not have existed in the original text.
642 if (part->begin < 0)
643 part->reset();
644 }
645}