Cookie.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Cookie::Cookie(const std::string& name,
  42. const std::string& value,
  43. time_t expiry,
  44. const std::string& path,
  45. const std::string& domain,
  46. bool secure):
  47. _name(name),
  48. _value(value),
  49. _expiry(expiry),
  50. _path(path),
  51. _domain(Util::toLower(domain)),
  52. _secure(secure) {}
  53. Cookie::Cookie(const std::string& name,
  54. const std::string& value,
  55. const std::string& path,
  56. const std::string& domain,
  57. bool secure):
  58. _name(name),
  59. _value(value),
  60. _expiry(0),
  61. _path(path),
  62. _domain(Util::toLower(domain)),
  63. _secure(secure) {}
  64. Cookie::Cookie():_expiry(0), _secure(false) {}
  65. Cookie::~Cookie() {}
  66. std::string Cookie::toString() const
  67. {
  68. return _name+"="+_value;
  69. }
  70. bool Cookie::good() const
  71. {
  72. return !_name.empty();
  73. }
  74. static bool pathInclude(const std::string& requestPath, const std::string& path)
  75. {
  76. if(requestPath == path) {
  77. return true;
  78. }
  79. if(Util::startsWith(requestPath, path)) {
  80. if(*path.rbegin() != '/' && requestPath[path.size()] != '/') {
  81. return false;
  82. }
  83. } else if(*path.rbegin() != '/' || *requestPath.rbegin() == '/' ||
  84. !Util::startsWith(requestPath+"/", path)) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. static bool domainMatch(const std::string& requestHost,
  90. const std::string& domain)
  91. {
  92. if(*domain.begin() == '.') {
  93. return Util::endsWith("."+requestHost, domain);
  94. } else {
  95. return requestHost == domain;
  96. }
  97. }
  98. bool Cookie::match(const std::string& requestHost,
  99. const std::string& requestPath,
  100. time_t date, bool secure) const
  101. {
  102. std::string lowerRequestHost = Util::toLower(requestHost);
  103. if((secure || (!_secure && !secure)) &&
  104. domainMatch(lowerRequestHost, _domain) &&
  105. pathInclude(requestPath, _path) &&
  106. (isSessionCookie() || (date < _expiry))) {
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. bool Cookie::validate(const std::string& requestHost,
  113. const std::string& requestPath) const
  114. {
  115. std::string lowerRequestHost = Util::toLower(requestHost);
  116. if(lowerRequestHost != _domain) {
  117. // domain must start with '.'
  118. if(*_domain.begin() != '.') {
  119. return false;
  120. }
  121. // domain must not end with '.'
  122. if(*_domain.rbegin() == '.') {
  123. return false;
  124. }
  125. // domain must include at least one embeded '.'
  126. if(_domain.size() < 4 || _domain.find(".", 1) == std::string::npos) {
  127. return false;
  128. }
  129. if(!Util::endsWith(lowerRequestHost, _domain)) {
  130. return false;
  131. }
  132. // From RFC2109
  133. // * The request-host is a FQDN (not IP address) and has the form HD,
  134. // where D is the value of the Domain attribute, and H is a string
  135. // that contains one or more dots.
  136. if(std::count(lowerRequestHost.begin(),
  137. lowerRequestHost.begin()+
  138. (lowerRequestHost.size()-_domain.size()), '.')
  139. > 0) {
  140. return false;
  141. }
  142. }
  143. if(requestPath != _path) {
  144. // From RFC2109
  145. // * The value for the Path attribute is not a prefix of the request-
  146. // URI.
  147. if(!pathInclude(requestPath, _path)) {
  148. return false;
  149. }
  150. }
  151. return good();
  152. }
  153. bool Cookie::operator==(const Cookie& cookie) const
  154. {
  155. return _domain == cookie._domain && _path == cookie._path &&
  156. _name == cookie._name;
  157. }
  158. bool Cookie::isExpired() const
  159. {
  160. return !_expiry == 0 && Time().getTime() >= _expiry;
  161. }
  162. const std::string& Cookie::getName() const
  163. {
  164. return _name;
  165. }
  166. const std::string& Cookie::getValue() const
  167. {
  168. return _value;
  169. }
  170. const std::string& Cookie::getPath() const
  171. {
  172. return _path;
  173. }
  174. const std::string& Cookie::getDomain() const
  175. {
  176. return _domain;
  177. }
  178. time_t Cookie::getExpiry() const
  179. {
  180. return _expiry;
  181. }
  182. bool Cookie::isSecureCookie() const
  183. {
  184. return _secure;
  185. }
  186. bool Cookie::isSessionCookie() const
  187. {
  188. return _expiry == 0;
  189. }
  190. } // namespace aria2