LinkHash.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
  3. * (C) 1999 Antti Koivisto (koivisto@kde.org)
  4. * (C) 2001 Dirk Mueller (mueller@kde.org)
  5. * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
  6. * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public License
  19. * along with this library; see the file COPYING.LIB. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. */
  23. #include "config.h"
  24. #include "KURL.h"
  25. #include "LinkHash.h"
  26. #include <wtf/text/AtomicString.h>
  27. #include <wtf/text/StringHash.h>
  28. #include <wtf/text/WTFString.h>
  29. namespace WebCore {
  30. template <typename CharacterType>
  31. static inline size_t findSlashDotDotSlash(const CharacterType* characters, size_t length, size_t position)
  32. {
  33. if (length < 4)
  34. return notFound;
  35. size_t loopLimit = length - 3;
  36. for (size_t i = position; i < loopLimit; ++i) {
  37. if (characters[i] == '/' && characters[i + 1] == '.' && characters[i + 2] == '.' && characters[i + 3] == '/')
  38. return i;
  39. }
  40. return notFound;
  41. }
  42. template <typename CharacterType>
  43. static inline size_t findSlashSlash(const CharacterType* characters, size_t length, size_t position)
  44. {
  45. if (length < 2)
  46. return notFound;
  47. size_t loopLimit = length - 1;
  48. for (size_t i = position; i < loopLimit; ++i) {
  49. if (characters[i] == '/' && characters[i + 1] == '/')
  50. return i;
  51. }
  52. return notFound;
  53. }
  54. template <typename CharacterType>
  55. static inline size_t findSlashDotSlash(const CharacterType* characters, size_t length, size_t position)
  56. {
  57. if (length < 3)
  58. return notFound;
  59. size_t loopLimit = length - 2;
  60. for (size_t i = position; i < loopLimit; ++i) {
  61. if (characters[i] == '/' && characters[i + 1] == '.' && characters[i + 2] == '/')
  62. return i;
  63. }
  64. return notFound;
  65. }
  66. template <typename CharacterType>
  67. static inline bool containsColonSlashSlash(const CharacterType* characters, unsigned length)
  68. {
  69. if (length < 3)
  70. return false;
  71. unsigned loopLimit = length - 2;
  72. for (unsigned i = 0; i < loopLimit; ++i) {
  73. if (characters[i] == ':' && characters[i + 1] == '/' && characters[i + 2] == '/')
  74. return true;
  75. }
  76. return false;
  77. }
  78. template <typename CharacterType>
  79. static inline void squeezeOutNullCharacters(Vector<CharacterType, 512>& string)
  80. {
  81. size_t size = string.size();
  82. size_t i = 0;
  83. for (i = 0; i < size; ++i) {
  84. if (!string[i])
  85. break;
  86. }
  87. if (i == size)
  88. return;
  89. size_t j = i;
  90. for (++i; i < size; ++i) {
  91. if (CharacterType character = string[i])
  92. string[j++] = character;
  93. }
  94. ASSERT(j < size);
  95. string.shrink(j);
  96. }
  97. template <typename CharacterType>
  98. static void cleanSlashDotDotSlashes(Vector<CharacterType, 512>& path, size_t firstSlash)
  99. {
  100. size_t slash = firstSlash;
  101. do {
  102. size_t previousSlash = slash ? reverseFind(path.data(), path.size(), '/', slash - 1) : notFound;
  103. // Don't remove the host, i.e. http://foo.org/../foo.html
  104. if (previousSlash == notFound || (previousSlash > 3 && path[previousSlash - 2] == ':' && path[previousSlash - 1] == '/')) {
  105. path[slash] = 0;
  106. path[slash + 1] = 0;
  107. path[slash + 2] = 0;
  108. } else {
  109. for (size_t i = previousSlash; i < slash + 3; ++i)
  110. path[i] = 0;
  111. }
  112. slash += 3;
  113. } while ((slash = findSlashDotDotSlash(path.data(), path.size(), slash)) != notFound);
  114. squeezeOutNullCharacters(path);
  115. }
  116. template <typename CharacterType>
  117. static void mergeDoubleSlashes(Vector<CharacterType, 512>& path, size_t firstSlash)
  118. {
  119. size_t refPos = find(path.data(), path.size(), '#');
  120. if (!refPos || refPos == notFound)
  121. refPos = path.size();
  122. size_t slash = firstSlash;
  123. while (slash < refPos) {
  124. if (!slash || path[slash - 1] != ':')
  125. path[slash++] = 0;
  126. else
  127. slash += 2;
  128. if ((slash = findSlashSlash(path.data(), path.size(), slash)) == notFound)
  129. break;
  130. }
  131. squeezeOutNullCharacters(path);
  132. }
  133. template <typename CharacterType>
  134. static void cleanSlashDotSlashes(Vector<CharacterType, 512>& path, size_t firstSlash)
  135. {
  136. size_t slash = firstSlash;
  137. do {
  138. path[slash] = 0;
  139. path[slash + 1] = 0;
  140. slash += 2;
  141. } while ((slash = findSlashDotSlash(path.data(), path.size(), slash)) != notFound);
  142. squeezeOutNullCharacters(path);
  143. }
  144. template <typename CharacterType>
  145. static inline void cleanPath(Vector<CharacterType, 512>& path)
  146. {
  147. // FIXME: Should not do this in the query or anchor part of the URL.
  148. size_t firstSlash = findSlashDotDotSlash(path.data(), path.size(), 0);
  149. if (firstSlash != notFound)
  150. cleanSlashDotDotSlashes(path, firstSlash);
  151. // FIXME: Should not do this in the query part.
  152. firstSlash = findSlashSlash(path.data(), path.size(), 0);
  153. if (firstSlash != notFound)
  154. mergeDoubleSlashes(path, firstSlash);
  155. // FIXME: Should not do this in the query or anchor part.
  156. firstSlash = findSlashDotSlash(path.data(), path.size(), 0);
  157. if (firstSlash != notFound)
  158. cleanSlashDotSlashes(path, firstSlash);
  159. }
  160. template <typename CharacterType>
  161. static inline bool matchLetter(CharacterType c, char lowercaseLetter)
  162. {
  163. return (c | 0x20) == lowercaseLetter;
  164. }
  165. template <typename CharacterType>
  166. static inline bool needsTrailingSlash(const CharacterType* characters, unsigned length)
  167. {
  168. if (length < 6)
  169. return false;
  170. if (!matchLetter(characters[0], 'h')
  171. || !matchLetter(characters[1], 't')
  172. || !matchLetter(characters[2], 't')
  173. || !matchLetter(characters[3], 'p'))
  174. return false;
  175. if (!(characters[4] == ':'
  176. || (matchLetter(characters[4], 's') && characters[5] == ':')))
  177. return false;
  178. unsigned pos = characters[4] == ':' ? 5 : 6;
  179. // Skip initial two slashes if present.
  180. if (pos + 1 < length && characters[pos] == '/' && characters[pos + 1] == '/')
  181. pos += 2;
  182. // Find next slash.
  183. while (pos < length && characters[pos] != '/')
  184. ++pos;
  185. return pos == length;
  186. }
  187. template <typename CharacterType>
  188. static ALWAYS_INLINE LinkHash visitedLinkHashInline(const CharacterType* url, unsigned length)
  189. {
  190. return AlreadyHashed::avoidDeletedValue(StringHasher::computeHash(url, length));
  191. }
  192. LinkHash visitedLinkHash(const String& url)
  193. {
  194. unsigned length = url.length();
  195. if (length && url.is8Bit())
  196. return visitedLinkHashInline(url.characters8(), length);
  197. return visitedLinkHashInline(url.characters(), length);
  198. }
  199. LinkHash visitedLinkHash(const UChar* url, unsigned length)
  200. {
  201. return visitedLinkHashInline(url, length);
  202. }
  203. template <typename CharacterType>
  204. static ALWAYS_INLINE void visitedURLInline(const KURL& base, const CharacterType* characters, unsigned length, Vector<CharacterType, 512>& buffer)
  205. {
  206. if (!length)
  207. return;
  208. // This is a poor man's completeURL. Faster with less memory allocation.
  209. // FIXME: It's missing a lot of what completeURL does and a lot of what KURL does.
  210. // For example, it does not handle international domain names properly.
  211. // FIXME: It is wrong that we do not do further processing on strings that have "://" in them:
  212. // 1) The "://" could be in the query or anchor.
  213. // 2) The URL's path could have a "/./" or a "/../" or a "//" sequence in it.
  214. // FIXME: needsTrailingSlash does not properly return true for a URL that has no path, but does
  215. // have a query or anchor.
  216. bool hasColonSlashSlash = containsColonSlashSlash(characters, length);
  217. if (hasColonSlashSlash && !needsTrailingSlash(characters, length)) {
  218. buffer.append(characters, length);
  219. return;
  220. }
  221. if (hasColonSlashSlash) {
  222. // FIXME: This is incorrect for URLs that have a query or anchor; the "/" needs to go at the
  223. // end of the path, *before* the query or anchor.
  224. buffer.append(characters, length);
  225. buffer.append('/');
  226. return;
  227. }
  228. if (!length)
  229. buffer.append(base.string().getCharactersWithUpconvert<CharacterType>(), base.string().length());
  230. else {
  231. switch (characters[0]) {
  232. case '/':
  233. buffer.append(base.string().getCharactersWithUpconvert<CharacterType>(), base.pathStart());
  234. break;
  235. case '#':
  236. buffer.append(base.string().getCharactersWithUpconvert<CharacterType>(), base.pathEnd());
  237. break;
  238. default:
  239. buffer.append(base.string().getCharactersWithUpconvert<CharacterType>(), base.pathAfterLastSlash());
  240. break;
  241. }
  242. }
  243. buffer.append(characters, length);
  244. cleanPath(buffer);
  245. if (needsTrailingSlash(buffer.data(), buffer.size())) {
  246. // FIXME: This is incorrect for URLs that have a query or anchor; the "/" needs to go at the
  247. // end of the path, *before* the query or anchor.
  248. buffer.append('/');
  249. }
  250. return;
  251. }
  252. void visitedURL(const KURL& base, const AtomicString& attributeURL, Vector<UChar, 512>& buffer)
  253. {
  254. return visitedURLInline(base, attributeURL.characters(), attributeURL.length(), buffer);
  255. }
  256. LinkHash visitedLinkHash(const KURL& base, const AtomicString& attributeURL)
  257. {
  258. if (attributeURL.isEmpty())
  259. return 0;
  260. if (!base.string().isEmpty() && base.string().is8Bit() && attributeURL.is8Bit()) {
  261. Vector<LChar, 512> url;
  262. visitedURLInline(base, attributeURL.characters8(), attributeURL.length(), url);
  263. if (url.isEmpty())
  264. return 0;
  265. return visitedLinkHashInline(url.data(), url.size());
  266. }
  267. Vector<UChar, 512> url;
  268. visitedURLInline(base, attributeURL.characters(), attributeURL.length(), url);
  269. if (url.isEmpty())
  270. return 0;
  271. return visitedLinkHashInline(url.data(), url.size());
  272. }
  273. } // namespace WebCore