Cookie.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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] != '.') ? A2STR::DOT_C+domain : domain;
  51. }
  52. std::string Cookie::normalizeDomain(const std::string& domain)
  53. {
  54. if(domain.empty() || util::isNumericHost(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(A2STR::DOT_C);
  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. creationTime_(time(0)),
  78. lastAccess_(creationTime_) {}
  79. Cookie::Cookie(const std::string& name,
  80. const std::string& value,
  81. const std::string& path,
  82. const std::string& domain,
  83. bool secure):
  84. name_(name),
  85. value_(value),
  86. expiry_(0),
  87. path_(path),
  88. domain_(normalizeDomain(domain)),
  89. secure_(secure),
  90. creationTime_(time(0)),
  91. lastAccess_(creationTime_) {}
  92. Cookie::Cookie():expiry_(0), secure_(false), lastAccess_(time(0)) {}
  93. Cookie::~Cookie() {}
  94. std::string Cookie::toString() const
  95. {
  96. return strconcat(name_, "=", value_);
  97. }
  98. bool Cookie::good() const
  99. {
  100. return !name_.empty();
  101. }
  102. static bool pathInclude(const std::string& requestPath, const std::string& path)
  103. {
  104. if(requestPath == path) {
  105. return true;
  106. }
  107. if(util::startsWith(requestPath, path)) {
  108. if(*path.rbegin() != '/' && requestPath[path.size()] != '/') {
  109. return false;
  110. }
  111. } else if(*path.rbegin() != '/' || *requestPath.rbegin() == '/' ||
  112. !util::startsWith(requestPath+"/", path)) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. static bool domainMatch(const std::string& normReqHost,
  118. const std::string& domain)
  119. {
  120. // RFC2965 stated that:
  121. //
  122. // A Set-Cookie2 with Domain=ajax.com will be accepted, and the
  123. // value for Domain will be taken to be .ajax.com, because a dot
  124. // gets prepended to the value.
  125. //
  126. // Also original Netscape implementation behaves exactly the same.
  127. // domain_ always starts ".". See Cookie::Cookie().
  128. return util::endsWith(normReqHost, domain);
  129. }
  130. bool Cookie::match(const std::string& requestHost,
  131. const std::string& requestPath,
  132. time_t date, bool secure) const
  133. {
  134. if((secure || (!secure_ && !secure)) &&
  135. (requestHost == domain_ || // For default domain or IP address
  136. domainMatch(normalizeDomain(requestHost), domain_)) &&
  137. pathInclude(requestPath, path_) &&
  138. (isSessionCookie() || (date < expiry_))) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. bool Cookie::validate(const std::string& requestHost,
  145. const std::string& requestPath) const
  146. {
  147. // If domain_ doesn't start with "." or it is IP address, then it
  148. // must equal to requestHost. Otherwise, do domain tail match.
  149. if(requestHost != domain_) {
  150. std::string normReqHost = normalizeDomain(requestHost);
  151. if(normReqHost != domain_) {
  152. // domain must start with '.'
  153. if(*domain_.begin() != '.') {
  154. return false;
  155. }
  156. // domain must not end with '.'
  157. if(*domain_.rbegin() == '.') {
  158. return false;
  159. }
  160. // domain must include at least one embeded '.'
  161. if(domain_.size() < 4 ||
  162. domain_.find(A2STR::DOT_C, 1) == std::string::npos) {
  163. return false;
  164. }
  165. if(!util::endsWith(normReqHost, domain_)) {
  166. return false;
  167. }
  168. // From RFC2965 3.3.2 Rejecting Cookies
  169. // * The request-host is a HDN (not IP address) and has the form HD,
  170. // where D is the value of the Domain attribute, and H is a string
  171. // that contains one or more dots.
  172. size_t dotCount = std::count(normReqHost.begin(),
  173. normReqHost.begin()+
  174. (normReqHost.size()-domain_.size()), '.');
  175. if(dotCount > 1 || (dotCount == 1 && normReqHost[0] != '.')) {
  176. return false;
  177. }
  178. }
  179. }
  180. if(requestPath != path_) {
  181. // From RFC2965 3.3.2 Rejecting Cookies
  182. // * The value for the Path attribute is not a prefix of the request-URI.
  183. if(!pathInclude(requestPath, path_)) {
  184. return false;
  185. }
  186. }
  187. return good();
  188. }
  189. bool Cookie::operator==(const Cookie& cookie) const
  190. {
  191. return domain_ == cookie.domain_ && path_ == cookie.path_ &&
  192. name_ == cookie.name_;
  193. }
  194. bool Cookie::isExpired() const
  195. {
  196. return !expiry_ == 0 && Time().getTime() >= expiry_;
  197. }
  198. std::string Cookie::toNsCookieFormat() const
  199. {
  200. std::stringstream ss;
  201. ss << domain_ << "\t";
  202. if(util::startsWith(domain_, A2STR::DOT_C)) {
  203. ss << "TRUE";
  204. } else {
  205. ss << "FALSE";
  206. }
  207. ss << "\t";
  208. ss << path_ << "\t";
  209. if(secure_) {
  210. ss << "TRUE";
  211. } else {
  212. ss << "FALSE";
  213. }
  214. ss << "\t";
  215. ss << expiry_ << "\t";
  216. ss << name_ << "\t";
  217. ss << value_;
  218. return ss.str();
  219. }
  220. void Cookie::markOriginServerOnly()
  221. {
  222. if(util::startsWith(domain_, A2STR::DOT_C)) {
  223. domain_.erase(domain_.begin(), domain_.begin()+1);
  224. }
  225. }
  226. void Cookie::updateLastAccess()
  227. {
  228. lastAccess_ = time(0);
  229. }
  230. } // namespace aria2