Cookie.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <sstream>
  38. #include "Util.h"
  39. #include "A2STR.h"
  40. #include "TimeA2.h"
  41. #include "a2functional.h"
  42. namespace aria2 {
  43. static std::string prependDotIfNotExists(const std::string& domain)
  44. {
  45. // From RFC2965:
  46. // * Domain=value
  47. // OPTIONAL. The value of the Domain attribute specifies the domain
  48. // for which the cookie is valid. If an explicitly specified value
  49. // does not start with a dot, the user agent supplies a leading dot.
  50. return (!domain.empty() && domain[0] != '.') ? "."+domain : domain;
  51. }
  52. static std::string normalizeDomain(const std::string& domain)
  53. {
  54. if(domain.empty() || Util::isNumbersAndDotsNotation(domain)) {
  55. return domain;
  56. }
  57. std::string md = prependDotIfNotExists(domain);
  58. // TODO use Util::split to strict verification
  59. std::string::size_type p = md.find_last_of(".");
  60. if(p == 0 || p == std::string::npos) {
  61. md += ".local";
  62. }
  63. return Util::toLower(prependDotIfNotExists(md));
  64. }
  65. Cookie::Cookie(const std::string& name,
  66. const std::string& value,
  67. time_t expiry,
  68. const std::string& path,
  69. const std::string& domain,
  70. bool secure):
  71. _name(name),
  72. _value(value),
  73. _expiry(expiry),
  74. _path(path),
  75. _domain(normalizeDomain(domain)),
  76. _secure(secure) {}
  77. Cookie::Cookie(const std::string& name,
  78. const std::string& value,
  79. const std::string& path,
  80. const std::string& domain,
  81. bool secure):
  82. _name(name),
  83. _value(value),
  84. _expiry(0),
  85. _path(path),
  86. _domain(normalizeDomain(domain)),
  87. _secure(secure) {}
  88. Cookie::Cookie():_expiry(0), _secure(false) {}
  89. Cookie::~Cookie() {}
  90. std::string Cookie::toString() const
  91. {
  92. return strconcat(_name, "=", _value);
  93. }
  94. bool Cookie::good() const
  95. {
  96. return !_name.empty();
  97. }
  98. static bool pathInclude(const std::string& requestPath, const std::string& path)
  99. {
  100. if(requestPath == path) {
  101. return true;
  102. }
  103. if(Util::startsWith(requestPath, path)) {
  104. if(*path.rbegin() != '/' && requestPath[path.size()] != '/') {
  105. return false;
  106. }
  107. } else if(*path.rbegin() != '/' || *requestPath.rbegin() == '/' ||
  108. !Util::startsWith(requestPath+"/", path)) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. static bool domainMatch(const std::string& normReqHost,
  114. const std::string& domain)
  115. {
  116. // RFC2965 stated that:
  117. //
  118. // A Set-Cookie2 with Domain=ajax.com will be accepted, and the
  119. // value for Domain will be taken to be .ajax.com, because a dot
  120. // gets prepended to the value.
  121. //
  122. // Also original Netscape implementation behaves exactly the same.
  123. // _domain always starts ".". See Cookie::Cookie().
  124. return Util::endsWith(normReqHost, domain);
  125. }
  126. bool Cookie::match(const std::string& requestHost,
  127. const std::string& requestPath,
  128. time_t date, bool secure) const
  129. {
  130. std::string normReqHost = normalizeDomain(requestHost);
  131. if((secure || (!_secure && !secure)) &&
  132. domainMatch(normReqHost, _domain) &&
  133. pathInclude(requestPath, _path) &&
  134. (isSessionCookie() || (date < _expiry))) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. bool Cookie::validate(const std::string& requestHost,
  141. const std::string& requestPath) const
  142. {
  143. std::string normReqHost = normalizeDomain(requestHost);
  144. // If _domain is IP address, then it should be matched to requestHost.
  145. // In other words, _domain == normReqHost
  146. if(normReqHost != _domain) {
  147. // domain must start with '.'
  148. if(*_domain.begin() != '.') {
  149. return false;
  150. }
  151. // domain must not end with '.'
  152. if(*_domain.rbegin() == '.') {
  153. return false;
  154. }
  155. // domain must include at least one embeded '.'
  156. if(_domain.size() < 4 || _domain.find(".", 1) == std::string::npos) {
  157. return false;
  158. }
  159. if(!Util::endsWith(normReqHost, _domain)) {
  160. return false;
  161. }
  162. // From RFC2965 3.3.2 Rejecting Cookies
  163. // * The request-host is a HDN (not IP address) and has the form HD,
  164. // where D is the value of the Domain attribute, and H is a string
  165. // that contains one or more dots.
  166. size_t dotCount = std::count(normReqHost.begin(),
  167. normReqHost.begin()+
  168. (normReqHost.size()-_domain.size()), '.');
  169. if(dotCount > 1 || (dotCount == 1 && normReqHost[0] != '.')) {
  170. return false;
  171. }
  172. }
  173. if(requestPath != _path) {
  174. // From RFC2965 3.3.2 Rejecting Cookies
  175. // * The value for the Path attribute is not a prefix of the request-URI.
  176. if(!pathInclude(requestPath, _path)) {
  177. return false;
  178. }
  179. }
  180. return good();
  181. }
  182. bool Cookie::operator==(const Cookie& cookie) const
  183. {
  184. return _domain == cookie._domain && _path == cookie._path &&
  185. _name == cookie._name;
  186. }
  187. bool Cookie::isExpired() const
  188. {
  189. return !_expiry == 0 && Time().getTime() >= _expiry;
  190. }
  191. std::string Cookie::toNsCookieFormat() const
  192. {
  193. std::stringstream ss;
  194. ss << _domain << "\t";
  195. if(Util::startsWith(_domain, ".")) {
  196. ss << "TRUE";
  197. } else {
  198. ss << "FALSE";
  199. }
  200. ss << "\t";
  201. ss << _path << "\t";
  202. if(_secure) {
  203. ss << "TRUE";
  204. } else {
  205. ss << "FALSE";
  206. }
  207. ss << "\t";
  208. ss << _expiry << "\t";
  209. ss << _name << "\t";
  210. ss << _value;
  211. return ss.str();
  212. }
  213. } // namespace aria2