OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/common/net/url_fixer_upper.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #if defined(OS_POSIX) | |
10 #include "base/environment.h" | |
11 #endif | |
12 #include "base/file_util.h" | |
13 #include "base/logging.h" | |
14 #include "base/strings/string_util.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "net/base/escape.h" | |
18 #include "net/base/filename_util.h" | |
19 #include "net/base/net_util.h" | |
20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
21 #include "url/url_file.h" | |
22 #include "url/url_parse.h" | |
23 #include "url/url_util.h" | |
24 | |
25 const char* URLFixerUpper::home_directory_override = NULL; | |
26 | |
27 namespace { | |
28 | |
29 // TODO(estade): Remove these ugly, ugly functions. They are only used in | |
30 // SegmentURL. A url::Parsed object keeps track of a bunch of indices into | |
31 // a url string, and these need to be updated when the URL is converted from | |
32 // UTF8 to UTF16. Instead of this after-the-fact adjustment, we should parse it | |
33 // in the correct string format to begin with. | |
34 url::Component UTF8ComponentToUTF16Component( | |
35 const std::string& text_utf8, | |
36 const url::Component& component_utf8) { | |
37 if (component_utf8.len == -1) | |
38 return url::Component(); | |
39 | |
40 std::string before_component_string = | |
41 text_utf8.substr(0, component_utf8.begin); | |
42 std::string component_string = text_utf8.substr(component_utf8.begin, | |
43 component_utf8.len); | |
44 base::string16 before_component_string_16 = | |
45 base::UTF8ToUTF16(before_component_string); | |
46 base::string16 component_string_16 = base::UTF8ToUTF16(component_string); | |
47 url::Component component_16(before_component_string_16.length(), | |
48 component_string_16.length()); | |
49 return component_16; | |
50 } | |
51 | |
52 void UTF8PartsToUTF16Parts(const std::string& text_utf8, | |
53 const url::Parsed& parts_utf8, | |
54 url::Parsed* parts) { | |
55 if (base::IsStringASCII(text_utf8)) { | |
56 *parts = parts_utf8; | |
57 return; | |
58 } | |
59 | |
60 parts->scheme = | |
61 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.scheme); | |
62 parts ->username = | |
63 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.username); | |
64 parts->password = | |
65 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.password); | |
66 parts->host = | |
67 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.host); | |
68 parts->port = | |
69 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.port); | |
70 parts->path = | |
71 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.path); | |
72 parts->query = | |
73 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.query); | |
74 parts->ref = | |
75 UTF8ComponentToUTF16Component(text_utf8, parts_utf8.ref); | |
76 } | |
77 | |
78 base::TrimPositions TrimWhitespaceUTF8(const std::string& input, | |
79 base::TrimPositions positions, | |
80 std::string* output) { | |
81 // This implementation is not so fast since it converts the text encoding | |
82 // twice. Please feel free to file a bug if this function hurts the | |
83 // performance of Chrome. | |
84 DCHECK(base::IsStringUTF8(input)); | |
85 base::string16 input16 = base::UTF8ToUTF16(input); | |
86 base::string16 output16; | |
87 base::TrimPositions result = | |
88 base::TrimWhitespace(input16, positions, &output16); | |
89 *output = base::UTF16ToUTF8(output16); | |
90 return result; | |
91 } | |
92 | |
93 // does some basic fixes for input that we want to test for file-ness | |
94 void PrepareStringForFileOps(const base::FilePath& text, | |
95 base::FilePath::StringType* output) { | |
96 #if defined(OS_WIN) | |
97 base::TrimWhitespace(text.value(), base::TRIM_ALL, output); | |
98 replace(output->begin(), output->end(), '/', '\\'); | |
99 #else | |
100 TrimWhitespaceUTF8(text.value(), base::TRIM_ALL, output); | |
101 #endif | |
102 } | |
103 | |
104 // Tries to create a full path from |text|. If the result is valid and the | |
105 // file exists, returns true and sets |full_path| to the result. Otherwise, | |
106 // returns false and leaves |full_path| unchanged. | |
107 bool ValidPathForFile(const base::FilePath::StringType& text, | |
108 base::FilePath* full_path) { | |
109 base::FilePath file_path = base::MakeAbsoluteFilePath(base::FilePath(text)); | |
110 if (file_path.empty()) | |
111 return false; | |
112 | |
113 if (!base::PathExists(file_path)) | |
114 return false; | |
115 | |
116 *full_path = file_path; | |
117 return true; | |
118 } | |
119 | |
120 #if defined(OS_POSIX) | |
121 // Given a path that starts with ~, return a path that starts with an | |
122 // expanded-out /user/foobar directory. | |
123 std::string FixupHomedir(const std::string& text) { | |
124 DCHECK(text.length() > 0 && text[0] == '~'); | |
125 | |
126 if (text.length() == 1 || text[1] == '/') { | |
127 const char* home = getenv(base::env_vars::kHome); | |
128 if (URLFixerUpper::home_directory_override) | |
129 home = URLFixerUpper::home_directory_override; | |
130 // We'll probably break elsewhere if $HOME is undefined, but check here | |
131 // just in case. | |
132 if (!home) | |
133 return text; | |
134 return home + text.substr(1); | |
135 } | |
136 | |
137 // Otherwise, this is a path like ~foobar/baz, where we must expand to | |
138 // user foobar's home directory. Officially, we should use getpwent(), | |
139 // but that is a nasty blocking call. | |
140 | |
141 #if defined(OS_MACOSX) | |
142 static const char kHome[] = "/Users/"; | |
143 #else | |
144 static const char kHome[] = "/home/"; | |
145 #endif | |
146 return kHome + text.substr(1); | |
147 } | |
148 #endif | |
149 | |
150 // Tries to create a file: URL from |text| if it looks like a filename, even if | |
151 // it doesn't resolve as a valid path or to an existing file. Returns a | |
152 // (possibly invalid) file: URL in |fixed_up_url| for input beginning | |
153 // with a drive specifier or "\\". Returns the unchanged input in other cases | |
154 // (including file: URLs: these don't look like filenames). | |
155 std::string FixupPath(const std::string& text) { | |
156 DCHECK(!text.empty()); | |
157 | |
158 base::FilePath::StringType filename; | |
159 #if defined(OS_WIN) | |
160 base::FilePath input_path(base::UTF8ToWide(text)); | |
161 PrepareStringForFileOps(input_path, &filename); | |
162 | |
163 // Fixup Windows-style drive letters, where "C:" gets rewritten to "C|". | |
164 if (filename.length() > 1 && filename[1] == '|') | |
165 filename[1] = ':'; | |
166 #elif defined(OS_POSIX) | |
167 base::FilePath input_path(text); | |
168 PrepareStringForFileOps(input_path, &filename); | |
169 if (filename.length() > 0 && filename[0] == '~') | |
170 filename = FixupHomedir(filename); | |
171 #endif | |
172 | |
173 // Here, we know the input looks like a file. | |
174 GURL file_url = net::FilePathToFileURL(base::FilePath(filename)); | |
175 if (file_url.is_valid()) { | |
176 return base::UTF16ToUTF8(net::FormatUrl(file_url, std::string(), | |
177 net::kFormatUrlOmitUsernamePassword, net::UnescapeRule::NORMAL, NULL, | |
178 NULL, NULL)); | |
179 } | |
180 | |
181 // Invalid file URL, just return the input. | |
182 return text; | |
183 } | |
184 | |
185 // Checks |domain| to see if a valid TLD is already present. If not, appends | |
186 // |desired_tld| to the domain, and prepends "www." unless it's already present. | |
187 void AddDesiredTLD(const std::string& desired_tld, std::string* domain) { | |
188 if (desired_tld.empty() || domain->empty()) | |
189 return; | |
190 | |
191 // Check the TLD. If the return value is positive, we already have a TLD, so | |
192 // abort. If the return value is std::string::npos, there's no valid host, | |
193 // but we can try to append a TLD anyway, since the host may become valid once | |
194 // the TLD is attached -- for example, "999999999999" is detected as a broken | |
195 // IP address and marked invalid, but attaching ".com" makes it legal. When | |
196 // the return value is 0, there's a valid host with no known TLD, so we can | |
197 // definitely append the user's TLD. We disallow unknown registries here so | |
198 // users can input "mail.yahoo" and hit ctrl-enter to get | |
199 // "www.mail.yahoo.com". | |
200 const size_t registry_length = | |
201 net::registry_controlled_domains::GetRegistryLength( | |
202 *domain, | |
203 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | |
204 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
205 if ((registry_length != 0) && (registry_length != std::string::npos)) | |
206 return; | |
207 | |
208 // Add the suffix at the end of the domain. | |
209 const size_t domain_length(domain->length()); | |
210 DCHECK_GT(domain_length, 0U); | |
211 DCHECK_NE(desired_tld[0], '.'); | |
212 if ((*domain)[domain_length - 1] != '.') | |
213 domain->push_back('.'); | |
214 domain->append(desired_tld); | |
215 | |
216 // Now, if the domain begins with "www.", stop. | |
217 const std::string prefix("www."); | |
218 if (domain->compare(0, prefix.length(), prefix) != 0) { | |
219 // Otherwise, add www. to the beginning of the URL. | |
220 domain->insert(0, prefix); | |
221 } | |
222 } | |
223 | |
224 inline void FixupUsername(const std::string& text, | |
225 const url::Component& part, | |
226 std::string* url) { | |
227 if (!part.is_valid()) | |
228 return; | |
229 | |
230 // We don't fix up the username at the moment. | |
231 url->append(text, part.begin, part.len); | |
232 // Do not append the trailing '@' because we might need to include the user's | |
233 // password. FixupURL itself will append the '@' for us. | |
234 } | |
235 | |
236 inline void FixupPassword(const std::string& text, | |
237 const url::Component& part, | |
238 std::string* url) { | |
239 if (!part.is_valid()) | |
240 return; | |
241 | |
242 // We don't fix up the password at the moment. | |
243 url->append(":"); | |
244 url->append(text, part.begin, part.len); | |
245 } | |
246 | |
247 void FixupHost(const std::string& text, | |
248 const url::Component& part, | |
249 bool has_scheme, | |
250 const std::string& desired_tld, | |
251 std::string* url) { | |
252 if (!part.is_valid()) | |
253 return; | |
254 | |
255 // Make domain valid. | |
256 // Strip all leading dots and all but one trailing dot, unless the user only | |
257 // typed dots, in which case their input is totally invalid and we should just | |
258 // leave it unchanged. | |
259 std::string domain(text, part.begin, part.len); | |
260 const size_t first_nondot(domain.find_first_not_of('.')); | |
261 if (first_nondot != std::string::npos) { | |
262 domain.erase(0, first_nondot); | |
263 size_t last_nondot(domain.find_last_not_of('.')); | |
264 DCHECK(last_nondot != std::string::npos); | |
265 last_nondot += 2; // Point at second period in ending string | |
266 if (last_nondot < domain.length()) | |
267 domain.erase(last_nondot); | |
268 } | |
269 | |
270 // Add any user-specified TLD, if applicable. | |
271 AddDesiredTLD(desired_tld, &domain); | |
272 | |
273 url->append(domain); | |
274 } | |
275 | |
276 void FixupPort(const std::string& text, | |
277 const url::Component& part, | |
278 std::string* url) { | |
279 if (!part.is_valid()) | |
280 return; | |
281 | |
282 // We don't fix up the port at the moment. | |
283 url->append(":"); | |
284 url->append(text, part.begin, part.len); | |
285 } | |
286 | |
287 inline void FixupPath(const std::string& text, | |
288 const url::Component& part, | |
289 std::string* url) { | |
290 if (!part.is_valid() || part.len == 0) { | |
291 // We should always have a path. | |
292 url->append("/"); | |
293 return; | |
294 } | |
295 | |
296 // Append the path as is. | |
297 url->append(text, part.begin, part.len); | |
298 } | |
299 | |
300 inline void FixupQuery(const std::string& text, | |
301 const url::Component& part, | |
302 std::string* url) { | |
303 if (!part.is_valid()) | |
304 return; | |
305 | |
306 // We don't fix up the query at the moment. | |
307 url->append("?"); | |
308 url->append(text, part.begin, part.len); | |
309 } | |
310 | |
311 inline void FixupRef(const std::string& text, | |
312 const url::Component& part, | |
313 std::string* url) { | |
314 if (!part.is_valid()) | |
315 return; | |
316 | |
317 // We don't fix up the ref at the moment. | |
318 url->append("#"); | |
319 url->append(text, part.begin, part.len); | |
320 } | |
321 | |
322 bool HasPort(const std::string& original_text, | |
323 const url::Component& scheme_component) { | |
324 // Find the range between the ":" and the "/". | |
325 size_t port_start = scheme_component.end() + 1; | |
326 size_t port_end = port_start; | |
327 while ((port_end < original_text.length()) && | |
328 !url::IsAuthorityTerminator(original_text[port_end])) | |
329 ++port_end; | |
330 if (port_end == port_start) | |
331 return false; | |
332 | |
333 // Scan the range to see if it is entirely digits. | |
334 for (size_t i = port_start; i < port_end; ++i) { | |
335 if (!IsAsciiDigit(original_text[i])) | |
336 return false; | |
337 } | |
338 | |
339 return true; | |
340 } | |
341 | |
342 // Try to extract a valid scheme from the beginning of |text|. | |
343 // If successful, set |scheme_component| to the text range where the scheme | |
344 // was located, and fill |canon_scheme| with its canonicalized form. | |
345 // Otherwise, return false and leave the outputs in an indeterminate state. | |
346 bool GetValidScheme(const std::string& text, | |
347 url::Component* scheme_component, | |
348 std::string* canon_scheme) { | |
349 canon_scheme->clear(); | |
350 | |
351 // Locate everything up to (but not including) the first ':' | |
352 if (!url::ExtractScheme(text.data(), static_cast<int>(text.length()), | |
353 scheme_component)) { | |
354 return false; | |
355 } | |
356 | |
357 // Make sure the scheme contains only valid characters, and convert | |
358 // to lowercase. This also catches IPv6 literals like [::1], because | |
359 // brackets are not in the whitelist. | |
360 url::StdStringCanonOutput canon_scheme_output(canon_scheme); | |
361 url::Component canon_scheme_component; | |
362 if (!url::CanonicalizeScheme(text.data(), *scheme_component, | |
363 &canon_scheme_output, &canon_scheme_component)) { | |
364 return false; | |
365 } | |
366 | |
367 // Strip the ':', and any trailing buffer space. | |
368 DCHECK_EQ(0, canon_scheme_component.begin); | |
369 canon_scheme->erase(canon_scheme_component.len); | |
370 | |
371 // We need to fix up the segmentation for "www.example.com:/". For this | |
372 // case, we guess that schemes with a "." are not actually schemes. | |
373 if (canon_scheme->find('.') != std::string::npos) | |
374 return false; | |
375 | |
376 // We need to fix up the segmentation for "www:123/". For this case, we | |
377 // will add an HTTP scheme later and make the URL parser happy. | |
378 // TODO(pkasting): Maybe we should try to use GURL's parser for this? | |
379 if (HasPort(text, *scheme_component)) | |
380 return false; | |
381 | |
382 // Everything checks out. | |
383 return true; | |
384 } | |
385 | |
386 // Performs the work for URLFixerUpper::SegmentURL. |text| may be modified on | |
387 // output on success: a semicolon following a valid scheme is replaced with a | |
388 // colon. | |
389 std::string SegmentURLInternal(std::string* text, url::Parsed* parts) { | |
390 // Initialize the result. | |
391 *parts = url::Parsed(); | |
392 | |
393 std::string trimmed; | |
394 TrimWhitespaceUTF8(*text, base::TRIM_ALL, &trimmed); | |
395 if (trimmed.empty()) | |
396 return std::string(); // Nothing to segment. | |
397 | |
398 #if defined(OS_WIN) | |
399 int trimmed_length = static_cast<int>(trimmed.length()); | |
400 if (url::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) || | |
401 url::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, true)) | |
402 return "file"; | |
403 #elif defined(OS_POSIX) | |
404 if (base::FilePath::IsSeparator(trimmed.data()[0]) || | |
405 trimmed.data()[0] == '~') | |
406 return "file"; | |
407 #endif | |
408 | |
409 // Otherwise, we need to look at things carefully. | |
410 std::string scheme; | |
411 if (!GetValidScheme(*text, &parts->scheme, &scheme)) { | |
412 // Try again if there is a ';' in the text. If changing it to a ':' results | |
413 // in a scheme being found, continue processing with the modified text. | |
414 bool found_scheme = false; | |
415 size_t semicolon = text->find(';'); | |
416 if (semicolon != 0 && semicolon != std::string::npos) { | |
417 (*text)[semicolon] = ':'; | |
418 if (GetValidScheme(*text, &parts->scheme, &scheme)) | |
419 found_scheme = true; | |
420 else | |
421 (*text)[semicolon] = ';'; | |
422 } | |
423 if (!found_scheme) { | |
424 // Couldn't determine the scheme, so just pick one. | |
425 parts->scheme.reset(); | |
426 scheme = StartsWithASCII(*text, "ftp.", false) ? | |
427 url::kFtpScheme : url::kHttpScheme; | |
428 } | |
429 } | |
430 | |
431 // Proceed with about and chrome schemes, but not file or nonstandard schemes. | |
432 if ((scheme != url::kAboutScheme) && (scheme != content::kChromeUIScheme) && | |
433 ((scheme == url::kFileScheme) || | |
434 !url::IsStandard( | |
435 scheme.c_str(), | |
436 url::Component(0, static_cast<int>(scheme.length()))))) { | |
437 return scheme; | |
438 } | |
439 | |
440 if (scheme == url::kFileSystemScheme) { | |
441 // Have the GURL parser do the heavy lifting for us. | |
442 url::ParseFileSystemURL(text->data(), static_cast<int>(text->length()), | |
443 parts); | |
444 return scheme; | |
445 } | |
446 | |
447 if (parts->scheme.is_valid()) { | |
448 // Have the GURL parser do the heavy lifting for us. | |
449 url::ParseStandardURL(text->data(), static_cast<int>(text->length()), | |
450 parts); | |
451 return scheme; | |
452 } | |
453 | |
454 // We need to add a scheme in order for ParseStandardURL to be happy. | |
455 // Find the first non-whitespace character. | |
456 std::string::iterator first_nonwhite = text->begin(); | |
457 while ((first_nonwhite != text->end()) && IsWhitespace(*first_nonwhite)) | |
458 ++first_nonwhite; | |
459 | |
460 // Construct the text to parse by inserting the scheme. | |
461 std::string inserted_text(scheme); | |
462 inserted_text.append(url::kStandardSchemeSeparator); | |
463 std::string text_to_parse(text->begin(), first_nonwhite); | |
464 text_to_parse.append(inserted_text); | |
465 text_to_parse.append(first_nonwhite, text->end()); | |
466 | |
467 // Have the GURL parser do the heavy lifting for us. | |
468 url::ParseStandardURL(text_to_parse.data(), | |
469 static_cast<int>(text_to_parse.length()), parts); | |
470 | |
471 // Offset the results of the parse to match the original text. | |
472 const int offset = -static_cast<int>(inserted_text.length()); | |
473 URLFixerUpper::OffsetComponent(offset, &parts->scheme); | |
474 URLFixerUpper::OffsetComponent(offset, &parts->username); | |
475 URLFixerUpper::OffsetComponent(offset, &parts->password); | |
476 URLFixerUpper::OffsetComponent(offset, &parts->host); | |
477 URLFixerUpper::OffsetComponent(offset, &parts->port); | |
478 URLFixerUpper::OffsetComponent(offset, &parts->path); | |
479 URLFixerUpper::OffsetComponent(offset, &parts->query); | |
480 URLFixerUpper::OffsetComponent(offset, &parts->ref); | |
481 | |
482 return scheme; | |
483 } | |
484 | |
485 } // namespace | |
486 | |
487 std::string URLFixerUpper::SegmentURL(const std::string& text, | |
488 url::Parsed* parts) { | |
489 std::string mutable_text(text); | |
490 return SegmentURLInternal(&mutable_text, parts); | |
491 } | |
492 | |
493 base::string16 URLFixerUpper::SegmentURL(const base::string16& text, | |
494 url::Parsed* parts) { | |
495 std::string text_utf8 = base::UTF16ToUTF8(text); | |
496 url::Parsed parts_utf8; | |
497 std::string scheme_utf8 = SegmentURL(text_utf8, &parts_utf8); | |
498 UTF8PartsToUTF16Parts(text_utf8, parts_utf8, parts); | |
499 return base::UTF8ToUTF16(scheme_utf8); | |
500 } | |
501 | |
502 GURL URLFixerUpper::FixupURL(const std::string& text, | |
503 const std::string& desired_tld) { | |
504 std::string trimmed; | |
505 TrimWhitespaceUTF8(text, base::TRIM_ALL, &trimmed); | |
506 if (trimmed.empty()) | |
507 return GURL(); // Nothing here. | |
508 | |
509 // Segment the URL. | |
510 url::Parsed parts; | |
511 std::string scheme(SegmentURLInternal(&trimmed, &parts)); | |
512 | |
513 // For view-source: URLs, we strip "view-source:", do fixup, and stick it back | |
514 // on. This allows us to handle things like "view-source:google.com". | |
515 if (scheme == content::kViewSourceScheme) { | |
516 // Reject "view-source:view-source:..." to avoid deep recursion. | |
517 std::string view_source(content::kViewSourceScheme + std::string(":")); | |
518 if (!StartsWithASCII(text, view_source + view_source, false)) { | |
519 return GURL(content::kViewSourceScheme + std::string(":") + | |
520 FixupURL(trimmed.substr(scheme.length() + 1), | |
521 desired_tld).possibly_invalid_spec()); | |
522 } | |
523 } | |
524 | |
525 // We handle the file scheme separately. | |
526 if (scheme == url::kFileScheme) | |
527 return GURL(parts.scheme.is_valid() ? text : FixupPath(text)); | |
528 | |
529 // We handle the filesystem scheme separately. | |
530 if (scheme == url::kFileSystemScheme) { | |
531 if (parts.inner_parsed() && parts.inner_parsed()->scheme.is_valid()) | |
532 return GURL(text); | |
533 return GURL(); | |
534 } | |
535 | |
536 // Parse and rebuild about: and chrome: URLs, except about:blank. | |
537 bool chrome_url = | |
538 !LowerCaseEqualsASCII(trimmed, url::kAboutBlankURL) && | |
539 ((scheme == url::kAboutScheme) || (scheme == content::kChromeUIScheme)); | |
540 | |
541 // For some schemes whose layouts we understand, we rebuild it. | |
542 if (chrome_url || | |
543 url::IsStandard(scheme.c_str(), | |
544 url::Component(0, static_cast<int>(scheme.length())))) { | |
545 // Replace the about: scheme with the chrome: scheme. | |
546 std::string url(chrome_url ? content::kChromeUIScheme : scheme); | |
547 url.append(url::kStandardSchemeSeparator); | |
548 | |
549 // We need to check whether the |username| is valid because it is our | |
550 // responsibility to append the '@' to delineate the user information from | |
551 // the host portion of the URL. | |
552 if (parts.username.is_valid()) { | |
553 FixupUsername(trimmed, parts.username, &url); | |
554 FixupPassword(trimmed, parts.password, &url); | |
555 url.append("@"); | |
556 } | |
557 | |
558 FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url); | |
559 if (chrome_url && !parts.host.is_valid()) | |
560 url.append(chrome::kChromeUIDefaultHost); | |
561 FixupPort(trimmed, parts.port, &url); | |
562 FixupPath(trimmed, parts.path, &url); | |
563 FixupQuery(trimmed, parts.query, &url); | |
564 FixupRef(trimmed, parts.ref, &url); | |
565 | |
566 return GURL(url); | |
567 } | |
568 | |
569 // In the worst-case, we insert a scheme if the URL lacks one. | |
570 if (!parts.scheme.is_valid()) { | |
571 std::string fixed_scheme(scheme); | |
572 fixed_scheme.append(url::kStandardSchemeSeparator); | |
573 trimmed.insert(0, fixed_scheme); | |
574 } | |
575 | |
576 return GURL(trimmed); | |
577 } | |
578 | |
579 // The rules are different here than for regular fixup, since we need to handle | |
580 // input like "hello.html" and know to look in the current directory. Regular | |
581 // fixup will look for cues that it is actually a file path before trying to | |
582 // figure out what file it is. If our logic doesn't work, we will fall back on | |
583 // regular fixup. | |
584 GURL URLFixerUpper::FixupRelativeFile(const base::FilePath& base_dir, | |
585 const base::FilePath& text) { | |
586 base::FilePath old_cur_directory; | |
587 if (!base_dir.empty()) { | |
588 // Save the old current directory before we move to the new one. | |
589 base::GetCurrentDirectory(&old_cur_directory); | |
590 base::SetCurrentDirectory(base_dir); | |
591 } | |
592 | |
593 // Allow funny input with extra whitespace and the wrong kind of slashes. | |
594 base::FilePath::StringType trimmed; | |
595 PrepareStringForFileOps(text, &trimmed); | |
596 | |
597 bool is_file = true; | |
598 // Avoid recognizing definite non-file URLs as file paths. | |
599 GURL gurl(trimmed); | |
600 if (gurl.is_valid() && gurl.IsStandard()) | |
601 is_file = false; | |
602 base::FilePath full_path; | |
603 if (is_file && !ValidPathForFile(trimmed, &full_path)) { | |
604 // Not a path as entered, try unescaping it in case the user has | |
605 // escaped things. We need to go through 8-bit since the escaped values | |
606 // only represent 8-bit values. | |
607 #if defined(OS_WIN) | |
608 std::wstring unescaped = base::UTF8ToWide(net::UnescapeURLComponent( | |
609 base::WideToUTF8(trimmed), | |
610 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS)); | |
611 #elif defined(OS_POSIX) | |
612 std::string unescaped = net::UnescapeURLComponent( | |
613 trimmed, | |
614 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); | |
615 #endif | |
616 | |
617 if (!ValidPathForFile(unescaped, &full_path)) | |
618 is_file = false; | |
619 } | |
620 | |
621 // Put back the current directory if we saved it. | |
622 if (!base_dir.empty()) | |
623 base::SetCurrentDirectory(old_cur_directory); | |
624 | |
625 if (is_file) { | |
626 GURL file_url = net::FilePathToFileURL(full_path); | |
627 if (file_url.is_valid()) | |
628 return GURL(base::UTF16ToUTF8(net::FormatUrl(file_url, std::string(), | |
629 net::kFormatUrlOmitUsernamePassword, net::UnescapeRule::NORMAL, NULL, | |
630 NULL, NULL))); | |
631 // Invalid files fall through to regular processing. | |
632 } | |
633 | |
634 // Fall back on regular fixup for this input. | |
635 #if defined(OS_WIN) | |
636 std::string text_utf8 = base::WideToUTF8(text.value()); | |
637 #elif defined(OS_POSIX) | |
638 std::string text_utf8 = text.value(); | |
639 #endif | |
640 return FixupURL(text_utf8, std::string()); | |
641 } | |
642 | |
643 void URLFixerUpper::OffsetComponent(int offset, url::Component* part) { | |
644 DCHECK(part); | |
645 | |
646 if (part->is_valid()) { | |
647 // Offset the location of this component. | |
648 part->begin += offset; | |
649 | |
650 // This part might not have existed in the original text. | |
651 if (part->begin < 0) | |
652 part->reset(); | |
653 } | |
654 } | |
OLD | NEW |