url_canon_stdurl.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 to canonicalize "standard" URLs, which are ones that have an
  5. // authority section including a host name.
  6. #include "url/url_canon.h"
  7. #include "url/url_canon_internal.h"
  8. #include "url/url_constants.h"
  9. namespace url {
  10. namespace {
  11. template <typename CHAR, typename UCHAR>
  12. bool DoCanonicalizeStandardURL(const URLComponentSource<CHAR>& source,
  13. const Parsed& parsed,
  14. SchemeType scheme_type,
  15. CharsetConverter* query_converter,
  16. CanonOutput* output,
  17. Parsed* new_parsed) {
  18. // Scheme: this will append the colon.
  19. bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
  20. output, &new_parsed->scheme);
  21. bool scheme_supports_user_info =
  22. (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION);
  23. bool scheme_supports_ports =
  24. (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION ||
  25. scheme_type == SCHEME_WITH_HOST_AND_PORT);
  26. // Authority (username, password, host, port)
  27. bool have_authority;
  28. if ((scheme_supports_user_info &&
  29. (parsed.username.is_valid() || parsed.password.is_valid())) ||
  30. parsed.host.is_nonempty() ||
  31. (scheme_supports_ports && parsed.port.is_valid())) {
  32. have_authority = true;
  33. // Only write the authority separators when we have a scheme.
  34. if (parsed.scheme.is_valid()) {
  35. output->push_back('/');
  36. output->push_back('/');
  37. }
  38. // User info: the canonicalizer will handle the : and @.
  39. if (scheme_supports_user_info) {
  40. success &= CanonicalizeUserInfo(
  41. source.username, parsed.username, source.password, parsed.password,
  42. output, &new_parsed->username, &new_parsed->password);
  43. } else {
  44. new_parsed->username.reset();
  45. new_parsed->password.reset();
  46. }
  47. success &= CanonicalizeHost(source.host, parsed.host,
  48. output, &new_parsed->host);
  49. // Host must not be empty for standard URLs.
  50. if (!parsed.host.is_nonempty())
  51. success = false;
  52. // Port: the port canonicalizer will handle the colon.
  53. if (scheme_supports_ports) {
  54. int default_port = DefaultPortForScheme(
  55. &output->data()[new_parsed->scheme.begin], new_parsed->scheme.len);
  56. success &= CanonicalizePort(source.port, parsed.port, default_port,
  57. output, &new_parsed->port);
  58. } else {
  59. new_parsed->port.reset();
  60. }
  61. } else {
  62. // No authority, clear the components.
  63. have_authority = false;
  64. new_parsed->host.reset();
  65. new_parsed->username.reset();
  66. new_parsed->password.reset();
  67. new_parsed->port.reset();
  68. success = false; // Standard URLs must have an authority.
  69. }
  70. // Path
  71. if (parsed.path.is_valid()) {
  72. success &= CanonicalizePath(source.path, parsed.path,
  73. output, &new_parsed->path);
  74. } else if (have_authority ||
  75. parsed.query.is_valid() || parsed.ref.is_valid()) {
  76. // When we have an empty path, make up a path when we have an authority
  77. // or something following the path. The only time we allow an empty
  78. // output path is when there is nothing else.
  79. new_parsed->path = Component(output->length(), 1);
  80. output->push_back('/');
  81. } else {
  82. // No path at all
  83. new_parsed->path.reset();
  84. }
  85. // Query
  86. CanonicalizeQuery(source.query, parsed.query, query_converter,
  87. output, &new_parsed->query);
  88. // Ref: ignore failure for this, since the page can probably still be loaded.
  89. CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
  90. // Carry over the flag for potentially dangling markup:
  91. if (parsed.potentially_dangling_markup)
  92. new_parsed->potentially_dangling_markup = true;
  93. return success;
  94. }
  95. } // namespace
  96. // Returns the default port for the given canonical scheme, or PORT_UNSPECIFIED
  97. // if the scheme is unknown.
  98. //
  99. // Please keep blink::DefaultPortForProtocol and url::DefaultPortForProtocol in
  100. // sync.
  101. int DefaultPortForScheme(const char* scheme, int scheme_len) {
  102. int default_port = PORT_UNSPECIFIED;
  103. switch (scheme_len) {
  104. case 4:
  105. if (!strncmp(scheme, kHttpScheme, scheme_len))
  106. default_port = 80;
  107. break;
  108. case 5:
  109. if (!strncmp(scheme, kHttpsScheme, scheme_len))
  110. default_port = 443;
  111. break;
  112. case 3:
  113. if (!strncmp(scheme, kFtpScheme, scheme_len))
  114. default_port = 21;
  115. else if (!strncmp(scheme, kWssScheme, scheme_len))
  116. default_port = 443;
  117. break;
  118. case 2:
  119. if (!strncmp(scheme, kWsScheme, scheme_len))
  120. default_port = 80;
  121. break;
  122. }
  123. return default_port;
  124. }
  125. bool CanonicalizeStandardURL(const char* spec,
  126. int spec_len,
  127. const Parsed& parsed,
  128. SchemeType scheme_type,
  129. CharsetConverter* query_converter,
  130. CanonOutput* output,
  131. Parsed* new_parsed) {
  132. return DoCanonicalizeStandardURL<char, unsigned char>(
  133. URLComponentSource<char>(spec), parsed, scheme_type, query_converter,
  134. output, new_parsed);
  135. }
  136. bool CanonicalizeStandardURL(const char16_t* spec,
  137. int spec_len,
  138. const Parsed& parsed,
  139. SchemeType scheme_type,
  140. CharsetConverter* query_converter,
  141. CanonOutput* output,
  142. Parsed* new_parsed) {
  143. return DoCanonicalizeStandardURL<char16_t, char16_t>(
  144. URLComponentSource<char16_t>(spec), parsed, scheme_type, query_converter,
  145. output, new_parsed);
  146. }
  147. // It might be nice in the future to optimize this so unchanged components don't
  148. // need to be recanonicalized. This is especially true since the common case for
  149. // ReplaceComponents is removing things we don't want, like reference fragments
  150. // and usernames. These cases can become more efficient if we can assume the
  151. // rest of the URL is OK with these removed (or only the modified parts
  152. // recanonicalized). This would be much more complex to implement, however.
  153. //
  154. // You would also need to update DoReplaceComponents in url_util.cc which
  155. // relies on this re-checking everything (see the comment there for why).
  156. bool ReplaceStandardURL(const char* base,
  157. const Parsed& base_parsed,
  158. const Replacements<char>& replacements,
  159. SchemeType scheme_type,
  160. CharsetConverter* query_converter,
  161. CanonOutput* output,
  162. Parsed* new_parsed) {
  163. URLComponentSource<char> source(base);
  164. Parsed parsed(base_parsed);
  165. SetupOverrideComponents(base, replacements, &source, &parsed);
  166. return DoCanonicalizeStandardURL<char, unsigned char>(
  167. source, parsed, scheme_type, query_converter, output, new_parsed);
  168. }
  169. // For 16-bit replacements, we turn all the replacements into UTF-8 so the
  170. // regular code path can be used.
  171. bool ReplaceStandardURL(const char* base,
  172. const Parsed& base_parsed,
  173. const Replacements<char16_t>& replacements,
  174. SchemeType scheme_type,
  175. CharsetConverter* query_converter,
  176. CanonOutput* output,
  177. Parsed* new_parsed) {
  178. RawCanonOutput<1024> utf8;
  179. URLComponentSource<char> source(base);
  180. Parsed parsed(base_parsed);
  181. SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
  182. return DoCanonicalizeStandardURL<char, unsigned char>(
  183. source, parsed, scheme_type, query_converter, output, new_parsed);
  184. }
  185. } // namespace url