cookie_helper.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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_helper.h"
  36. #include <cstring>
  37. #include <vector>
  38. #include <limits>
  39. #include "util.h"
  40. #include "array_fun.h"
  41. #include "Cookie.h"
  42. #include "a2functional.h"
  43. namespace aria2 {
  44. namespace cookie {
  45. namespace {
  46. bool isDelimiter(unsigned char c)
  47. {
  48. return c == 0x09u || in(c, 0x20u, 0x2fu) || in(c, 0x3bu, 0x40u) ||
  49. in(c, 0x5bu, 0x60u) || in(c, 0x7bu, 0x7eu);
  50. }
  51. } // namespace
  52. namespace {
  53. std::string::const_iterator getNextDigit
  54. (std::string::const_iterator first, std::string::const_iterator last)
  55. {
  56. for(; first != last && in(static_cast<unsigned char>(*first), 0x30u, 0x39u);
  57. ++first);
  58. return first;
  59. }
  60. } // namespace
  61. bool parseDate
  62. (time_t& time,
  63. std::string::const_iterator first,
  64. std::string::const_iterator last)
  65. {
  66. // Following algorithm is described in
  67. // http://tools.ietf.org/html/rfc6265#section-5.1.1
  68. std::vector<std::string> dateTokens;
  69. for(std::string::const_iterator i = first,
  70. eoi = last; i != eoi;) {
  71. unsigned char c = *i;
  72. if(isDelimiter(c)) {
  73. ++i;
  74. continue;
  75. }
  76. std::string::const_iterator s = i;
  77. for(; s != eoi && !isDelimiter(static_cast<unsigned char>(*s)); ++s);
  78. dateTokens.push_back(std::string(i, s));
  79. i = s;
  80. }
  81. int dayOfMonth = 0;
  82. bool foundDayOfMonth = false;
  83. int month = 0;
  84. bool foundMonth = false;
  85. int year = 0;
  86. bool foundYear = false;
  87. int hour = 0;
  88. int minute = 0;
  89. int second = 0;
  90. bool foundTime = false;
  91. for(std::vector<std::string>::const_iterator i = dateTokens.begin(),
  92. eoi = dateTokens.end(); i != eoi; ++i) {
  93. if(!foundTime) {
  94. std::string::const_iterator hEnd;
  95. std::string::const_iterator mEnd;
  96. std::string::const_iterator sEnd;
  97. hEnd = getNextDigit((*i).begin(),(*i).end());
  98. size_t len = std::distance((*i).begin(), hEnd);
  99. if(len == 0 || 2 < len || hEnd == (*i).end() || *hEnd != ':') {
  100. goto NOT_TIME;
  101. }
  102. mEnd = getNextDigit(hEnd+1, (*i).end());
  103. len = std::distance(hEnd+1, mEnd);
  104. if(len == 0 || 2 < len || mEnd == (*i).end() || *mEnd != ':') {
  105. goto NOT_TIME;
  106. }
  107. sEnd = getNextDigit(mEnd+1, (*i).end());
  108. len = std::distance(mEnd+1, sEnd);
  109. if(len == 0 || 2 < len) {
  110. goto NOT_TIME;
  111. }
  112. foundTime = true;
  113. hour = util::parseInt((*i).begin(), hEnd);
  114. minute = util::parseInt(hEnd+1, mEnd);
  115. second = util::parseInt(mEnd+1, sEnd);
  116. continue;
  117. NOT_TIME:
  118. ;
  119. }
  120. if(!foundDayOfMonth) {
  121. std::string::const_iterator j = getNextDigit((*i).begin(), (*i).end());
  122. size_t len = std::distance((*i).begin(), j);
  123. if(1 <= len && len <= 2) {
  124. foundDayOfMonth = true;
  125. dayOfMonth = util::parseInt((*i).begin(), j);
  126. continue;
  127. }
  128. }
  129. if(!foundMonth) {
  130. static const char MONTH[][12] = {
  131. "jan", "feb", "mar", "apr",
  132. "may", "jun", "jul", "aug",
  133. "sep", "oct", "nov", "dec" };
  134. if((*i).size() >= 3) {
  135. bool found = false;
  136. size_t j;
  137. for(j = 0; j < 12; ++j) {
  138. if(util::strieq((*i).begin(), (*i).begin()+3,
  139. &MONTH[j][0], &MONTH[j][3])) {
  140. found = true;
  141. break;
  142. }
  143. }
  144. if(found) {
  145. foundMonth = true;
  146. month = j+1;
  147. continue;
  148. }
  149. }
  150. }
  151. if(!foundYear) {
  152. std::string::const_iterator j = getNextDigit((*i).begin(), (*i).end());
  153. size_t len = std::distance((*i).begin(), j);
  154. if(1 <= len && len <= 4) {
  155. foundYear = true;
  156. year = util::parseInt((*i).begin(), j);
  157. continue;
  158. }
  159. }
  160. }
  161. if(in(year, 70, 99)) {
  162. year += 1900;
  163. } else if(in(year, 0, 69)) {
  164. year += 2000;
  165. }
  166. if(!foundDayOfMonth || !foundMonth || !foundYear || !foundTime ||
  167. !in(dayOfMonth, 1, 31) || year < 1601 || hour > 23 ||
  168. minute > 59 || second > 59) {
  169. return false;
  170. }
  171. if((month == 4 || month == 6 || month == 9 || month == 11) &&
  172. dayOfMonth > 30) {
  173. return false;
  174. }
  175. if(month == 2) {
  176. if((year%4 == 0 && year%100 != 0) || year%400 == 0) {
  177. if(dayOfMonth > 29) {
  178. return false;
  179. }
  180. } else if(dayOfMonth > 28) {
  181. return false;
  182. }
  183. }
  184. tm timespec;
  185. memset(&timespec, 0, sizeof(timespec));
  186. timespec.tm_sec = second;
  187. timespec.tm_min = minute;
  188. timespec.tm_hour = hour;
  189. timespec.tm_mday = dayOfMonth;
  190. timespec.tm_mon = month-1;
  191. timespec.tm_year = year-1900;
  192. time = timegm(&timespec);
  193. return time != -1;
  194. }
  195. bool parse
  196. (Cookie& cookie,
  197. const std::string& cookieStr,
  198. const std::string& requestHost,
  199. const std::string& defaultPath,
  200. time_t creationTime)
  201. {
  202. // This implementation is based on the algorithm listed in
  203. // http://tools.ietf.org/html/rfc6265
  204. std::string::const_iterator nvEnd = cookieStr.begin();
  205. std::string::const_iterator end = cookieStr.end();
  206. for(; nvEnd != end && *nvEnd != ';'; ++nvEnd);
  207. std::string::const_iterator eq = cookieStr.begin();
  208. for(; eq != nvEnd && *eq != '='; ++eq);
  209. if(eq == nvEnd) {
  210. return false;
  211. }
  212. std::pair<std::string::const_iterator,
  213. std::string::const_iterator> p =
  214. util::stripIter(cookieStr.begin(), eq);
  215. if(p.first == p.second) {
  216. return false;
  217. }
  218. Scip cookieName(p.first, p.second);
  219. p = util::stripIter(eq+1, nvEnd);
  220. p = util::stripIter(p.first, p.second, "\"");
  221. Scip cookieValue(p.first, p.second);
  222. time_t expiryTime = 0;
  223. bool foundExpires = false;
  224. bool persistent = false;
  225. time_t maxAge = 0;
  226. bool foundMaxAge = false;
  227. std::string cookieDomain;
  228. bool hostOnly = false;
  229. std::string cookiePath;
  230. bool secure = false;
  231. bool httpOnly = false;
  232. if(nvEnd != end) {
  233. ++nvEnd;
  234. }
  235. static const char A2_EXPIRES[] = "expires";
  236. static const char A2_MAX_AGE[] = "max-age";
  237. static const char A2_DOMAIN[] = "domain";
  238. static const char A2_PATH[] = "path";
  239. static const char A2_SECURE[] = "secure";
  240. static const char A2_HTTPONLY[] = "httponly";
  241. for(std::string::const_iterator i = nvEnd; i != end;) {
  242. std::string::const_iterator j = std::find(i, end, ';');
  243. std::string::const_iterator eq = std::find(i, j, '=');
  244. p = util::stripIter(i, eq);
  245. std::pair<std::string::const_iterator,
  246. std::string::const_iterator> attrp;
  247. if(eq == j) {
  248. attrp.first = attrp.second = j;
  249. } else {
  250. attrp = util::stripIter(eq+1, j);
  251. }
  252. i = j;
  253. if(j != end) {
  254. ++i;
  255. }
  256. if(util::strieq(p.first, p.second, A2_EXPIRES, vend(A2_EXPIRES)-1)) {
  257. if(parseDate(expiryTime, attrp.first, attrp.second)) {
  258. foundExpires = true;
  259. } else {
  260. return false;
  261. }
  262. } else if(util::strieq(p.first, p.second, A2_MAX_AGE, vend(A2_MAX_AGE)-1)) {
  263. if(attrp.first == attrp.second ||
  264. (!in(static_cast<unsigned char>(*attrp.first), 0x30u, 0x39u) &&
  265. *attrp.first != '-')) {
  266. return false;
  267. }
  268. for(std::string::const_iterator s = attrp.first+1,
  269. eos = attrp.second; s != eos; ++s) {
  270. if(!in(static_cast<unsigned char>(*s), 0x30u, 0x39u)) {
  271. return false;
  272. }
  273. }
  274. int64_t delta;
  275. if(util::parseLLIntNoThrow(delta, attrp.first, attrp.second)) {
  276. foundMaxAge = true;
  277. if(delta <= 0) {
  278. maxAge = 0;
  279. } else {
  280. int64_t n = creationTime;
  281. n += delta;
  282. if(n < 0 || std::numeric_limits<time_t>::max() < n) {
  283. maxAge = std::numeric_limits<time_t>::max();
  284. } else {
  285. maxAge = n;
  286. }
  287. }
  288. } else {
  289. return false;
  290. }
  291. } else if(util::strieq(p.first, p.second, A2_DOMAIN, vend(A2_DOMAIN)-1)) {
  292. if(attrp.first == attrp.second) {
  293. return false;
  294. }
  295. std::string::const_iterator noDot = attrp.first;
  296. std::string::const_iterator end = attrp.second;
  297. for(; noDot != end && *noDot == '.'; ++noDot);
  298. if(noDot == end) {
  299. return false;
  300. }
  301. cookieDomain.assign(noDot, end);
  302. } else if(util::strieq(p.first, p.second, A2_PATH, vend(A2_PATH)-1)) {
  303. if(goodPath(attrp.first, attrp.second)) {
  304. cookiePath.assign(attrp.first, attrp.second);
  305. } else {
  306. cookiePath = defaultPath;
  307. }
  308. } else if(util::strieq(p.first, p.second, A2_SECURE, vend(A2_SECURE)-1)) {
  309. secure = true;
  310. } else if(util::strieq(p.first, p.second, A2_HTTPONLY, vend(A2_HTTPONLY)-1)) {
  311. httpOnly = true;
  312. }
  313. }
  314. if(foundMaxAge) {
  315. expiryTime = maxAge;
  316. persistent = true;
  317. } else if(foundExpires) {
  318. persistent = true;
  319. } else {
  320. expiryTime = std::numeric_limits<time_t>::max();
  321. persistent = false;
  322. }
  323. std::string canonicalizedHost = canonicalizeHost(requestHost);
  324. if(cookieDomain.empty()) {
  325. hostOnly = true;
  326. cookieDomain = canonicalizedHost;
  327. } else if(domainMatch(canonicalizedHost, cookieDomain)) {
  328. hostOnly = util::isNumericHost(canonicalizedHost);
  329. } else {
  330. return false;
  331. }
  332. if(cookiePath.empty()) {
  333. cookiePath = defaultPath;
  334. }
  335. cookie.setName(cookieName.first, cookieName.second);
  336. cookie.setValue(cookieValue.first, cookieValue.second);
  337. cookie.setExpiryTime(expiryTime);
  338. cookie.setPersistent(persistent);
  339. cookie.setDomain(cookieDomain);
  340. cookie.setHostOnly(hostOnly);
  341. cookie.setPath(cookiePath);
  342. cookie.setSecure(secure);
  343. cookie.setHttpOnly(httpOnly);
  344. cookie.setCreationTime(creationTime);
  345. cookie.setLastAccessTime(creationTime);
  346. return true;
  347. }
  348. bool goodPath
  349. (std::string::const_iterator first,
  350. std::string::const_iterator last)
  351. {
  352. return first != last && *first == '/';
  353. }
  354. std::string canonicalizeHost(const std::string& host)
  355. {
  356. std::string ch = util::toLower(host);
  357. return ch;
  358. }
  359. bool domainMatch(const std::string& requestHost, const std::string& domain)
  360. {
  361. return requestHost == domain ||
  362. (util::endsWith(requestHost.begin(), requestHost.end(),
  363. domain.begin(), domain.end()) &&
  364. requestHost[requestHost.size()-domain.size()-1] == '.' &&
  365. !util::isNumericHost(requestHost));
  366. }
  367. bool pathMatch(const std::string& requestPath, const std::string& path)
  368. {
  369. return requestPath == path ||
  370. (util::startsWith(requestPath.begin(), requestPath.end(),
  371. path.begin(), path.end()) &&
  372. (path[path.size()-1] == '/' || requestPath[path.size()] == '/'));
  373. }
  374. std::string reverseDomainLevel(const std::string& domain)
  375. {
  376. std::string r;
  377. for(std::string::const_iterator i = domain.begin(), eoi = domain.end();
  378. i != eoi;) {
  379. std::string::const_iterator j = std::find(i, eoi, '.');
  380. r.insert(r.begin(), '.');
  381. r.insert(r.begin(), i, j);
  382. i = j;
  383. if(j != eoi) {
  384. ++i;
  385. }
  386. }
  387. if(!r.empty()) {
  388. r.erase(r.size()-1, 1);
  389. }
  390. return r;
  391. }
  392. } // namespace cookie
  393. } // namespace aria2