url_canon_internal_file.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef URL_URL_CANON_INTERNAL_FILE_H_
  5. #define URL_URL_CANON_INTERNAL_FILE_H_
  6. // As with url_canon_internal.h, this file is intended to be included in
  7. // another C++ file where the template types are defined. This allows the
  8. // programmer to use this to use these functions for their own strings
  9. // types, without bloating the code by having inline templates used in
  10. // every call site.
  11. //
  12. // *** This file must be included after url_canon_internal as we depend on some
  13. // functions in it. ***
  14. #include "base/strings/string_util.h"
  15. #include "url/url_file.h"
  16. #include "url/url_parse_internal.h"
  17. namespace url {
  18. // Given a pointer into the spec, this copies and canonicalizes the drive
  19. // letter and colon to the output, if one is found. If there is not a drive
  20. // spec, it won't do anything. The index of the next character in the input
  21. // spec is returned (after the colon when a drive spec is found, the begin
  22. // offset if one is not).
  23. template<typename CHAR>
  24. static int FileDoDriveSpec(const CHAR* spec, int begin, int end,
  25. CanonOutput* output) {
  26. // The path could be one of several things: /foo/bar, c:/foo/bar, /c:/foo,
  27. // (with backslashes instead of slashes as well).
  28. int num_slashes = CountConsecutiveSlashes(spec, begin, end);
  29. int after_slashes = begin + num_slashes;
  30. if (!DoesBeginWindowsDriveSpec(spec, after_slashes, end))
  31. return begin; // Haven't consumed any characters
  32. // DoesBeginWindowsDriveSpec will ensure that the drive letter is valid
  33. // and that it is followed by a colon/pipe.
  34. // Normalize Windows drive letters to uppercase
  35. if (base::IsAsciiLower(spec[after_slashes]))
  36. output->push_back(spec[after_slashes] - 'a' + 'A');
  37. else
  38. output->push_back(static_cast<char>(spec[after_slashes]));
  39. // Normalize the character following it to a colon rather than pipe.
  40. output->push_back(':');
  41. output->push_back('/');
  42. return after_slashes + 2;
  43. }
  44. // FileDoDriveSpec will have already added the first backslash, so we need to
  45. // write everything following the slashes using the path canonicalizer.
  46. template<typename CHAR, typename UCHAR>
  47. static void FileDoPath(const CHAR* spec, int begin, int end,
  48. CanonOutput* output) {
  49. // Normalize the number of slashes after the drive letter. The path
  50. // canonicalizer expects the input to begin in a slash already so
  51. // doesn't check. We want to handle no-slashes
  52. int num_slashes = CountConsecutiveSlashes(spec, begin, end);
  53. int after_slashes = begin + num_slashes;
  54. // Now use the regular path canonicalizer to canonicalize the rest of the
  55. // path. We supply it with the path following the slashes. It won't prepend
  56. // a slash because it assumes any nonempty path already starts with one.
  57. // We explicitly filter out calls with no path here to prevent that case.
  58. ParsedComponent sub_path(after_slashes, end - after_slashes);
  59. if (sub_path.len > 0) {
  60. // Give it a fake output component to write into. DoCanonicalizeFile will
  61. // compute the full path component.
  62. ParsedComponent fake_output_path;
  63. URLCanonInternal<CHAR, UCHAR>::DoPath(
  64. spec, sub_path, output, &fake_output_path);
  65. }
  66. }
  67. template<typename CHAR, typename UCHAR>
  68. static bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
  69. const ParsedURL& parsed,
  70. CanonOutput* output,
  71. ParsedURL* new_parsed) {
  72. // Things we don't set in file: URLs.
  73. new_parsed->username = ParsedComponent(0, -1);
  74. new_parsed->password = ParsedComponent(0, -1);
  75. new_parsed->port = ParsedComponent(0, -1);
  76. // Scheme (known, so we don't bother running it through the more
  77. // complicated scheme canonicalizer).
  78. new_parsed->scheme.begin = output->length();
  79. output->push_back('f');
  80. output->push_back('i');
  81. output->push_back('l');
  82. output->push_back('e');
  83. new_parsed->scheme.len = output->length() - new_parsed->scheme.begin;
  84. output->push_back(':');
  85. // Write the separator for the host.
  86. output->push_back('/');
  87. output->push_back('/');
  88. // Append the host. For many file URLs, this will be empty. For UNC, this
  89. // will be present.
  90. // TODO(brettw) This doesn't do any checking for host name validity. We
  91. // should probably handle validity checking of UNC hosts differently than
  92. // for regular IP hosts.
  93. bool success = URLCanonInternal<CHAR, UCHAR>::DoHost(
  94. source.host, parsed.host, output, &new_parsed->host);
  95. // Write a separator for the start of the path. We'll ignore any slashes
  96. // already at the beginning of the path.
  97. new_parsed->path.begin = output->length();
  98. output->push_back('/');
  99. // Copy and normalize the "c:" at the beginning, if present.
  100. int after_drive = FileDoDriveSpec(source.path, parsed.path.begin,
  101. parsed.path.end(), output);
  102. // Copy the rest of the path.
  103. FileDoPath<CHAR, UCHAR>(source.path, after_drive, parsed.path.end(), output);
  104. new_parsed->path.len = output->length() - new_parsed->path.begin;
  105. // For things following the path, we can use the standard canonicalizers.
  106. success &= URLCanonInternal<CHAR, UCHAR>::DoQuery(
  107. source.query, parsed.query, output, &new_parsed->query);
  108. success &= URLCanonInternal<CHAR, UCHAR>::DoRef(
  109. source.ref, parsed.ref, output, &new_parsed->ref);
  110. return success;
  111. }
  112. } // namespace url
  113. #endif // URL_URL_CANON_INTERNAL_FILE_H_