url_canon_fileurl.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2013 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. // Functions for canonicalizing "file:" URLs.
  5. #include "base/strings/string_piece.h"
  6. #include "base/strings/string_util.h"
  7. #include "url/url_canon.h"
  8. #include "url/url_canon_internal.h"
  9. #include "url/url_file.h"
  10. #include "url/url_parse_internal.h"
  11. namespace url {
  12. namespace {
  13. bool IsLocalhost(const char* spec, int begin, int end) {
  14. if (begin > end)
  15. return false;
  16. return base::StringPiece(&spec[begin], end - begin) == "localhost";
  17. }
  18. bool IsLocalhost(const char16_t* spec, int begin, int end) {
  19. if (begin > end)
  20. return false;
  21. return base::StringPiece16(&spec[begin], end - begin) == u"localhost";
  22. }
  23. template <typename CHAR>
  24. int DoFindWindowsDriveLetter(const CHAR* spec, int begin, int end) {
  25. if (begin > end)
  26. return -1;
  27. // First guess the beginning of the drive letter.
  28. // If there is something that looks like a drive letter in the spec between
  29. // begin and end, store its position in drive_letter_pos.
  30. int drive_letter_pos =
  31. DoesContainWindowsDriveSpecUntil(spec, begin, end, end);
  32. if (drive_letter_pos < begin)
  33. return -1;
  34. // Check if the path up to the drive letter candidate can be canonicalized as
  35. // "/".
  36. Component sub_path = MakeRange(begin, drive_letter_pos);
  37. RawCanonOutput<1024> output;
  38. Component output_path;
  39. bool success = CanonicalizePath(spec, sub_path, &output, &output_path);
  40. if (!success || output_path.len != 1 || output.at(output_path.begin) != '/') {
  41. return -1;
  42. }
  43. return drive_letter_pos;
  44. }
  45. #ifdef WIN32
  46. // Given a pointer into the spec, this copies and canonicalizes the drive
  47. // letter and colon to the output, if one is found. If there is not a drive
  48. // spec, it won't do anything. The index of the next character in the input
  49. // spec is returned (after the colon when a drive spec is found, the begin
  50. // offset if one is not).
  51. template <typename CHAR>
  52. int FileDoDriveSpec(const CHAR* spec, int begin, int end, CanonOutput* output) {
  53. int drive_letter_pos = FindWindowsDriveLetter(spec, begin, end);
  54. if (drive_letter_pos < begin)
  55. return begin;
  56. // By now, a valid drive letter is confirmed at position drive_letter_pos,
  57. // followed by a valid drive letter separator (a colon or a pipe).
  58. output->push_back('/');
  59. // Normalize Windows drive letters to uppercase.
  60. if (base::IsAsciiLower(spec[drive_letter_pos]))
  61. output->push_back(static_cast<char>(spec[drive_letter_pos] - 'a' + 'A'));
  62. else
  63. output->push_back(static_cast<char>(spec[drive_letter_pos]));
  64. // Normalize the character following it to a colon rather than pipe.
  65. output->push_back(':');
  66. return drive_letter_pos + 2;
  67. }
  68. #endif // WIN32
  69. template<typename CHAR, typename UCHAR>
  70. bool DoFileCanonicalizePath(const CHAR* spec,
  71. const Component& path,
  72. CanonOutput* output,
  73. Component* out_path) {
  74. // Copies and normalizes the "c:" at the beginning, if present.
  75. out_path->begin = output->length();
  76. int after_drive;
  77. #ifdef WIN32
  78. after_drive = FileDoDriveSpec(spec, path.begin, path.end(), output);
  79. #else
  80. after_drive = path.begin;
  81. #endif
  82. // Copies the rest of the path, starting from the slash following the
  83. // drive colon (if any, Windows only), or the first slash of the path.
  84. bool success = true;
  85. if (after_drive < path.end()) {
  86. // Use the regular path canonicalizer to canonicalize the rest of the path
  87. // after the drive.
  88. //
  89. // Give it a fake output component to write into, since we will be
  90. // calculating the out_path ourselves (consisting of both the drive and the
  91. // path we canonicalize here).
  92. Component sub_path = MakeRange(after_drive, path.end());
  93. Component fake_output_path;
  94. success = CanonicalizePath(spec, sub_path, output, &fake_output_path);
  95. } else if (after_drive == path.begin) {
  96. // No input path and no drive spec, canonicalize to a slash.
  97. output->push_back('/');
  98. }
  99. out_path->len = output->length() - out_path->begin;
  100. return success;
  101. }
  102. template<typename CHAR, typename UCHAR>
  103. bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
  104. const Parsed& parsed,
  105. CharsetConverter* query_converter,
  106. CanonOutput* output,
  107. Parsed* new_parsed) {
  108. // Things we don't set in file: URLs.
  109. new_parsed->username = Component();
  110. new_parsed->password = Component();
  111. new_parsed->port = Component();
  112. // Scheme (known, so we don't bother running it through the more
  113. // complicated scheme canonicalizer).
  114. new_parsed->scheme.begin = output->length();
  115. output->Append("file://", 7);
  116. new_parsed->scheme.len = 4;
  117. // If the host is localhost, and the path starts with a Windows drive letter,
  118. // remove the host component. This does the following transformation:
  119. // file://localhost/C:/hello.txt -> file:///C:/hello.txt
  120. //
  121. // Note: we do this on every platform per URL Standard, not just Windows.
  122. //
  123. // TODO(https://crbug.com/688961): According to the latest URL spec, this
  124. // transformation should be done regardless of the path.
  125. Component host_range = parsed.host;
  126. if (IsLocalhost(source.host, host_range.begin, host_range.end()) &&
  127. FindWindowsDriveLetter(source.path, parsed.path.begin,
  128. parsed.path.end()) >= parsed.path.begin) {
  129. host_range.reset();
  130. }
  131. // Append the host. For many file URLs, this will be empty. For UNC, this
  132. // will be present.
  133. // TODO(brettw) This doesn't do any checking for host name validity. We
  134. // should probably handle validity checking of UNC hosts differently than
  135. // for regular IP hosts.
  136. bool success =
  137. CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
  138. success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
  139. output, &new_parsed->path);
  140. CanonicalizeQuery(source.query, parsed.query, query_converter,
  141. output, &new_parsed->query);
  142. CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
  143. return success;
  144. }
  145. } // namespace
  146. int FindWindowsDriveLetter(const char* spec, int begin, int end) {
  147. return DoFindWindowsDriveLetter(spec, begin, end);
  148. }
  149. int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
  150. return DoFindWindowsDriveLetter(spec, begin, end);
  151. }
  152. bool CanonicalizeFileURL(const char* spec,
  153. int spec_len,
  154. const Parsed& parsed,
  155. CharsetConverter* query_converter,
  156. CanonOutput* output,
  157. Parsed* new_parsed) {
  158. return DoCanonicalizeFileURL<char, unsigned char>(
  159. URLComponentSource<char>(spec), parsed, query_converter,
  160. output, new_parsed);
  161. }
  162. bool CanonicalizeFileURL(const char16_t* spec,
  163. int spec_len,
  164. const Parsed& parsed,
  165. CharsetConverter* query_converter,
  166. CanonOutput* output,
  167. Parsed* new_parsed) {
  168. return DoCanonicalizeFileURL<char16_t, char16_t>(
  169. URLComponentSource<char16_t>(spec), parsed, query_converter, output,
  170. new_parsed);
  171. }
  172. bool FileCanonicalizePath(const char* spec,
  173. const Component& path,
  174. CanonOutput* output,
  175. Component* out_path) {
  176. return DoFileCanonicalizePath<char, unsigned char>(spec, path,
  177. output, out_path);
  178. }
  179. bool FileCanonicalizePath(const char16_t* spec,
  180. const Component& path,
  181. CanonOutput* output,
  182. Component* out_path) {
  183. return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
  184. out_path);
  185. }
  186. bool ReplaceFileURL(const char* base,
  187. const Parsed& base_parsed,
  188. const Replacements<char>& replacements,
  189. CharsetConverter* query_converter,
  190. CanonOutput* output,
  191. Parsed* new_parsed) {
  192. URLComponentSource<char> source(base);
  193. Parsed parsed(base_parsed);
  194. SetupOverrideComponents(base, replacements, &source, &parsed);
  195. return DoCanonicalizeFileURL<char, unsigned char>(
  196. source, parsed, query_converter, output, new_parsed);
  197. }
  198. bool ReplaceFileURL(const char* base,
  199. const Parsed& base_parsed,
  200. const Replacements<char16_t>& replacements,
  201. CharsetConverter* query_converter,
  202. CanonOutput* output,
  203. Parsed* new_parsed) {
  204. RawCanonOutput<1024> utf8;
  205. URLComponentSource<char> source(base);
  206. Parsed parsed(base_parsed);
  207. SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
  208. return DoCanonicalizeFileURL<char, unsigned char>(
  209. source, parsed, query_converter, output, new_parsed);
  210. }
  211. } // namespace url