cookie_helper.cc 12 KB

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