CookieBox.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "CookieBox.h"
  23. #include "Util.h"
  24. CookieBox::CookieBox() {}
  25. CookieBox::~CookieBox() {}
  26. void CookieBox::add(const Cookie& cookie) {
  27. cookies.push_back(cookie);
  28. }
  29. void CookieBox::add(string cookieStr) {
  30. Cookie c;
  31. parse(c, cookieStr);
  32. cookies.push_back(c);
  33. }
  34. void CookieBox::setField(Cookie& cookie, string name, string value) const {
  35. if(name.size() == string("secure").size() &&
  36. strcasecmp(name.c_str(), "secure") == 0) {
  37. cookie.secure = true;
  38. } else if(name.size() == string("domain").size() && strcasecmp(name.c_str(), "domain") == 0) {
  39. cookie.domain = value;
  40. } else if(name.size() == string("path").size() && strcasecmp(name.c_str(), "path") == 0) {
  41. cookie.path = value;
  42. } else if(name.size() == string("expires").size() && strcasecmp(name.c_str(), "expires") == 0) {
  43. cookie.expires = value;
  44. } else {
  45. cookie.name = name;
  46. cookie.value = value;
  47. }
  48. }
  49. void CookieBox::parse(Cookie& cookie, string cookieStr) const {
  50. cookie.clear();
  51. vector<string> terms;
  52. Util::slice(terms, cookieStr, ';');
  53. for(vector<string>::iterator itr = terms.begin(); itr != terms.end(); itr++) {
  54. pair<string, string> nv;
  55. Util::split(nv, *itr, '=');
  56. setField(cookie, nv.first, nv.second);
  57. }
  58. }
  59. vector<Cookie> CookieBox::criteriaFind(string host, string dir, bool secure) const {
  60. vector<Cookie> result;
  61. for(vector<Cookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  62. const Cookie& c = *itr;
  63. if((secure || !c.secure && !secure) &&
  64. c.domain.size() <= host.size() &&
  65. c.path.size() <= dir.size() &&
  66. c.domain.compare(0, c.domain.size(), host, host.size()-c.domain.size(), c.domain.size()) == 0 &&
  67. c.path.compare(0, c.path.size(), dir, 0, c.path.size()) == 0) {
  68. // TODO we currently ignore expire date.
  69. result.push_back(c);
  70. }
  71. }
  72. return result;
  73. }