Cookie.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Cookie.h"
  36. #include <algorithm>
  37. #include "Util.h"
  38. #include "A2STR.h"
  39. #include "TimeA2.h"
  40. namespace aria2 {
  41. static std::string prependDotIfNotExists(const std::string& domain)
  42. {
  43. // From RFC2965:
  44. // * Domain=value
  45. // OPTIONAL. The value of the Domain attribute specifies the domain
  46. // for which the cookie is valid. If an explicitly specified value
  47. // does not start with a dot, the user agent supplies a leading dot.
  48. return (!domain.empty() && domain[0] != '.') ? "."+domain : domain;
  49. }
  50. static std::string normalizeDomain(const std::string& domain)
  51. {
  52. if(domain.empty() || Util::isNumbersAndDotsNotation(domain)) {
  53. return domain;
  54. }
  55. std::string md = prependDotIfNotExists(domain);
  56. // TODO use Util::split to strict verification
  57. std::string::size_type p = md.find_last_of(".");
  58. if(p == 0 || p == std::string::npos) {
  59. md += ".local";
  60. }
  61. return Util::toLower(prependDotIfNotExists(md));
  62. }
  63. Cookie::Cookie(const std::string& name,
  64. const std::string& value,
  65. time_t expiry,
  66. const std::string& path,
  67. const std::string& domain,
  68. bool secure):
  69. _name(name),
  70. _value(value),
  71. _expiry(expiry),
  72. _path(path),
  73. _domain(normalizeDomain(domain)),
  74. _secure(secure) {}
  75. Cookie::Cookie(const std::string& name,
  76. const std::string& value,
  77. const std::string& path,
  78. const std::string& domain,
  79. bool secure):
  80. _name(name),
  81. _value(value),
  82. _expiry(0),
  83. _path(path),
  84. _domain(normalizeDomain(domain)),
  85. _secure(secure) {}
  86. Cookie::Cookie():_expiry(0), _secure(false) {}
  87. Cookie::~Cookie() {}
  88. std::string Cookie::toString() const
  89. {
  90. return _name+"="+_value;
  91. }
  92. bool Cookie::good() const
  93. {
  94. return !_name.empty();
  95. }
  96. static bool pathInclude(const std::string& requestPath, const std::string& path)
  97. {
  98. if(requestPath == path) {
  99. return true;
  100. }
  101. if(Util::startsWith(requestPath, path)) {
  102. if(*path.rbegin() != '/' && requestPath[path.size()] != '/') {
  103. return false;
  104. }
  105. } else if(*path.rbegin() != '/' || *requestPath.rbegin() == '/' ||
  106. !Util::startsWith(requestPath+"/", path)) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. static bool domainMatch(const std::string& normReqHost,
  112. const std::string& domain)
  113. {
  114. // RFC2965 stated that:
  115. //
  116. // A Set-Cookie2 with Domain=ajax.com will be accepted, and the
  117. // value for Domain will be taken to be .ajax.com, because a dot
  118. // gets prepended to the value.
  119. //
  120. // Also original Netscape implementation behaves exactly the same.
  121. // _domain always starts ".". See Cookie::Cookie().
  122. return Util::endsWith(normReqHost, domain);
  123. }
  124. bool Cookie::match(const std::string& requestHost,
  125. const std::string& requestPath,
  126. time_t date, bool secure) const
  127. {
  128. std::string normReqHost = normalizeDomain(requestHost);
  129. if((secure || (!_secure && !secure)) &&
  130. domainMatch(normReqHost, _domain) &&
  131. pathInclude(requestPath, _path) &&
  132. (isSessionCookie() || (date < _expiry))) {
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. bool Cookie::validate(const std::string& requestHost,
  139. const std::string& requestPath) const
  140. {
  141. std::string normReqHost = normalizeDomain(requestHost);
  142. // If _domain is IP address, then it should be matched to requestHost.
  143. // In other words, _domain == normReqHost
  144. if(normReqHost != _domain) {
  145. // domain must start with '.'
  146. if(*_domain.begin() != '.') {
  147. return false;
  148. }
  149. // domain must not end with '.'
  150. if(*_domain.rbegin() == '.') {
  151. return false;
  152. }
  153. // domain must include at least one embeded '.'
  154. if(_domain.size() < 4 || _domain.find(".", 1) == std::string::npos) {
  155. return false;
  156. }
  157. if(!Util::endsWith(normReqHost, _domain)) {
  158. return false;
  159. }
  160. // From RFC2965 3.3.2 Rejecting Cookies
  161. // * The request-host is a HDN (not IP address) and has the form HD,
  162. // where D is the value of the Domain attribute, and H is a string
  163. // that contains one or more dots.
  164. size_t dotCount = std::count(normReqHost.begin(),
  165. normReqHost.begin()+
  166. (normReqHost.size()-_domain.size()), '.');
  167. if(dotCount > 1 || (dotCount == 1 && normReqHost[0] != '.')) {
  168. return false;
  169. }
  170. }
  171. if(requestPath != _path) {
  172. // From RFC2965 3.3.2 Rejecting Cookies
  173. // * The value for the Path attribute is not a prefix of the request-URI.
  174. if(!pathInclude(requestPath, _path)) {
  175. return false;
  176. }
  177. }
  178. return good();
  179. }
  180. bool Cookie::operator==(const Cookie& cookie) const
  181. {
  182. return _domain == cookie._domain && _path == cookie._path &&
  183. _name == cookie._name;
  184. }
  185. bool Cookie::isExpired() const
  186. {
  187. return !_expiry == 0 && Time().getTime() >= _expiry;
  188. }
  189. const std::string& Cookie::getName() const
  190. {
  191. return _name;
  192. }
  193. const std::string& Cookie::getValue() const
  194. {
  195. return _value;
  196. }
  197. const std::string& Cookie::getPath() const
  198. {
  199. return _path;
  200. }
  201. const std::string& Cookie::getDomain() const
  202. {
  203. return _domain;
  204. }
  205. time_t Cookie::getExpiry() const
  206. {
  207. return _expiry;
  208. }
  209. bool Cookie::isSecureCookie() const
  210. {
  211. return _secure;
  212. }
  213. bool Cookie::isSessionCookie() const
  214. {
  215. return _expiry == 0;
  216. }
  217. } // namespace aria2