url_fixer.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2011 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. #ifndef COMPONENTS_URL_FORMATTER_URL_FIXER_H_
  5. #define COMPONENTS_URL_FORMATTER_URL_FIXER_H_
  6. #include <string>
  7. #include "url/gurl.h"
  8. namespace base {
  9. class FilePath;
  10. }
  11. namespace url {
  12. struct Component;
  13. struct Parsed;
  14. }
  15. // These methods process user typed input that is meant to be a URL - like user
  16. // typing in the URL bar or command line switches. The output is NOT guaranteed
  17. // to be a valid URL.
  18. //
  19. // This is NOT the place for converting between different types of URLs or
  20. // parsing them, see net_util.h for that. These methods should only be used on
  21. // user typed input, NOT untrusted strings sourced from the web or elsewhere.
  22. namespace url_formatter {
  23. // Segments the given text string into parts of a URL. This is most useful for
  24. // schemes such as http, https, and ftp where |SegmentURL| will find many
  25. // segments. Currently does not segment "file" schemes.
  26. // Returns the canonicalized scheme, or the empty string when |text| is only
  27. // whitespace.
  28. std::string SegmentURL(const std::string& text, url::Parsed* parts);
  29. std::u16string SegmentURL(const std::u16string& text, url::Parsed* parts);
  30. // Attempts to fix common problems in user-typed text, making some "smart"
  31. // adjustments to obviously-invalid input where possible.
  32. //
  33. // The result can still be invalid, so check the return value's validity or
  34. // use possibly_invalid_spec(). DO NOT USE this method on untrusted strings
  35. // from the web or elsewhere. Only use this for user-typed input.
  36. //
  37. // If |text| may be an absolute path to a file, it will get converted to a
  38. // "file:" URL.
  39. //
  40. // Schemes "about" and "chrome" are normalized to "chrome://", with slashes.
  41. // "about:blank" is unaltered, as Webkit allows frames to access about:blank.
  42. // Additionally, if a chrome URL does not have a valid host, as in "about:", the
  43. // returned URL will have the host "version", as in "chrome://version".
  44. //
  45. // If |desired_tld| is non-empty, it represents the TLD the user wishes to
  46. // append in the case of an incomplete domain. We check that this is not a file
  47. // path and there does not appear to be a valid TLD already, then append
  48. // |desired_tld| to the domain and prepend "www." (unless it, or a scheme, are
  49. // already present.) This TLD should not have a leading '.' (use "com" instead
  50. // of ".com").
  51. GURL FixupURL(const std::string& text, const std::string& desired_tld);
  52. // Converts |text| to a fixed-up URL, allowing it to be a relative path on the
  53. // local filesystem. Begin searching in |base_dir|; if empty, use the current
  54. // working directory. If this resolves to a file on disk, convert it to a
  55. // "file:" URL in |fixed_up_url|; otherwise, fall back to the behavior of
  56. // FixupURL().
  57. //
  58. // For "regular" input, even if it is possibly a file with a full path, you
  59. // should use FixupURL() directly. This function should only be used when
  60. // relative path handling is desired, as for command line processing.
  61. GURL FixupRelativeFile(const base::FilePath& base_dir,
  62. const base::FilePath& text);
  63. // Offsets the beginning index of |part| by |offset|, which is allowed to be
  64. // negative. In some cases, the desired component does not exist at the given
  65. // offset. For example, when converting from "http://foo" to "foo", the scheme
  66. // component no longer exists. In such a case, the beginning index is set to 0.
  67. // Does nothing if |part| is invalid.
  68. void OffsetComponent(int offset, url::Component* part);
  69. // Returns true if |scheme1| is equivalent to |scheme2|.
  70. // Generally this is true if the two schemes are actually identical, but it's
  71. // also true when one scheme is "about" and the other "chrome".
  72. bool IsEquivalentScheme(const std::string& scheme1, const std::string& scheme2);
  73. // For paths like ~, we use $HOME for the current user's home directory.
  74. // For tests, we allow our idea of $HOME to be overriden by this variable.
  75. extern const char* home_directory_override;
  76. } // namespace url_formatter
  77. #endif // COMPONENTS_URL_FORMATTER_URL_FIXER_H_