Sqlite3MozCookieParser.cc 4.2 KB

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