CookieBox.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Strings terms;
  52. Util::slice(terms, cookieStr, ';');
  53. for(Strings::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. Cookies CookieBox::criteriaFind(string host, string dir, bool secure) const {
  60. Cookies result;
  61. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  62. const Cookie& c = *itr;
  63. if((secure || !c.secure && !secure) &&
  64. Util::endsWith(host, c.domain) &&
  65. Util::startsWith(dir, c.path)) {
  66. // TODO we currently ignore expire date.
  67. result.push_back(c);
  68. }
  69. }
  70. return result;
  71. }