Sqlite3CookieParser.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "Sqlite3CookieParser.h"
  36. #include <cstring>
  37. #include "DlAbortEx.h"
  38. #include "util.h"
  39. #include "StringFormat.h"
  40. #include "A2STR.h"
  41. #include "cookie_helper.h"
  42. #ifndef HAVE_SQLITE3_OPEN_V2
  43. # include "File.h"
  44. #endif // !HAVE_SQLITE3_OPEN_V2
  45. namespace aria2 {
  46. Sqlite3CookieParser::Sqlite3CookieParser(const std::string& filename):db_(0)
  47. {
  48. int ret;
  49. #ifdef HAVE_SQLITE3_OPEN_V2
  50. ret = sqlite3_open_v2(filename.c_str(), &db_, SQLITE_OPEN_READONLY, 0);
  51. #else // !HAVE_SQLITE3_OPEN_V2
  52. if(!File(filename).isFile()) {
  53. return;
  54. }
  55. ret = sqlite3_open(filename.c_str(), &db_);
  56. #endif // !HAVE_SQLITE3_OPEN_V2
  57. if(SQLITE_OK != ret) {
  58. sqlite3_close(db_);
  59. db_ = 0;
  60. }
  61. }
  62. Sqlite3CookieParser::~Sqlite3CookieParser()
  63. {
  64. sqlite3_close(db_);
  65. }
  66. static std::string toString(const char* str)
  67. {
  68. if(str) {
  69. return str;
  70. } else {
  71. return A2STR::NIL;
  72. }
  73. }
  74. static bool parseTime(int64_t& time, const std::string& s)
  75. {
  76. if(!util::parseLLIntNoThrow(time, s)) {
  77. return false;
  78. }
  79. if(sizeof(time_t) == 4 && time > INT32_MAX) {
  80. time = INT32_MAX;
  81. }
  82. return true;
  83. }
  84. static int cookieRowMapper(void* data, int columns,
  85. char** values, char** names)
  86. {
  87. if(columns != 7) {
  88. return 0;
  89. }
  90. std::vector<Cookie>& cookies =
  91. *reinterpret_cast<std::vector<Cookie>*>(data);
  92. std::string cookieDomain = cookie::removePrecedingDots(toString(values[0]));
  93. std::string cookieName = toString(values[4]);
  94. std::string cookiePath = toString(values[1]);
  95. if(cookieName.empty() || cookieDomain.empty() ||
  96. !cookie::goodPath(cookiePath)) {
  97. return 0;
  98. }
  99. int64_t expiryTime;
  100. if(!parseTime(expiryTime, toString(values[3]))) {
  101. return 0;
  102. }
  103. int64_t lastAccessTime;
  104. if(!parseTime(lastAccessTime, toString(values[6]))) {
  105. return 0;
  106. }
  107. Cookie c(cookieName,
  108. toString(values[5]), // value
  109. expiryTime,
  110. true, // persistent
  111. cookieDomain,
  112. util::isNumericHost(cookieDomain) || values[0][0] != '.', // hostOnly
  113. cookiePath,
  114. strcmp(toString(values[2]).c_str(), "1") == 0, //secure
  115. false,
  116. lastAccessTime // creation time. Set this later.
  117. );
  118. cookies.push_back(c);
  119. return 0;
  120. }
  121. void Sqlite3CookieParser::parse(std::vector<Cookie>& cookies)
  122. {
  123. if(!db_) {
  124. throw DL_ABORT_EX(StringFormat("SQLite3 database is not opened.").str());
  125. }
  126. std::vector<Cookie> tcookies;
  127. char* sqlite3ErrMsg = 0;
  128. int ret = sqlite3_exec(db_, getQuery().c_str(), cookieRowMapper,
  129. &tcookies, &sqlite3ErrMsg);
  130. std::string errMsg;
  131. if(sqlite3ErrMsg) {
  132. errMsg = sqlite3ErrMsg;
  133. sqlite3_free(sqlite3ErrMsg);
  134. }
  135. if(SQLITE_OK != ret) {
  136. throw DL_ABORT_EX(StringFormat("Failed to read SQLite3 database: %s",
  137. errMsg.c_str()).str());
  138. }
  139. cookies.swap(tcookies);
  140. }
  141. } // namespace aria2