Sqlite3MozCookieParser.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "Sqlite3MozCookieParser.h"
  36. #include "RecoverableException.h"
  37. #include "Util.h"
  38. #include "StringFormat.h"
  39. #include "A2STR.h"
  40. #include <cstring>
  41. #include <sqlite3.h>
  42. namespace aria2 {
  43. Sqlite3MozCookieParser::Sqlite3MozCookieParser() {}
  44. Sqlite3MozCookieParser::~Sqlite3MozCookieParser() {}
  45. static std::string toString(const char* str)
  46. {
  47. if(str) {
  48. return str;
  49. } else {
  50. return A2STR::NIL;
  51. }
  52. }
  53. static int cookieRowMapper(void* data, int rowIndex,
  54. char** values, char** names)
  55. {
  56. try {
  57. int64_t expireDate = Util::parseLLInt(toString(values[3]));
  58. // TODO assuming time_t is int32_t...
  59. if(expireDate > INT32_MAX) {
  60. expireDate = INT32_MAX;
  61. }
  62. Cookie c(toString(values[4]), // name
  63. toString(values[5]), // value
  64. expireDate, // expires
  65. toString(values[1]), // path
  66. toString(values[0]), // domain
  67. strcmp(toString(values[2]).c_str(), "1") == 0 ? true:false //secure
  68. );
  69. if(c.good()) {
  70. ((std::deque<Cookie>*)data)->push_back(c);
  71. }
  72. } catch(RecoverableException& e) {
  73. //failed to parse expiry.
  74. }
  75. return 0;
  76. }
  77. std::deque<Cookie>
  78. Sqlite3MozCookieParser::parse(const std::string& filename) const
  79. {
  80. sqlite3* db = 0;
  81. int ret;
  82. ret = sqlite3_open_v2(filename.c_str(), &db, SQLITE_OPEN_READONLY, 0);
  83. if(SQLITE_OK != ret) {
  84. std::string errMsg = sqlite3_errmsg(db);
  85. sqlite3_close(db);
  86. throw RecoverableException
  87. (StringFormat("Failed to open SQLite3 database: %s",
  88. errMsg.c_str()).str());
  89. }
  90. std::deque<Cookie> cookies;
  91. char* sqlite3ErrMsg = 0;
  92. static const char* QUERY =
  93. "SELECT host, path, isSecure, expiry, name, value FROM moz_cookies";
  94. ret = sqlite3_exec(db, QUERY, cookieRowMapper, &cookies, &sqlite3ErrMsg);
  95. std::string errMsg;
  96. if(sqlite3ErrMsg) {
  97. errMsg = sqlite3ErrMsg;
  98. sqlite3_free(sqlite3ErrMsg);
  99. }
  100. if(SQLITE_OK != ret) {
  101. sqlite3_close(db);
  102. throw RecoverableException
  103. (StringFormat("Failed to read SQLite3 database: %s",
  104. errMsg.c_str()).str());
  105. }
  106. sqlite3_close(db);
  107. return cookies;
  108. }
  109. } // namespace aria2