Sqlite3CookieParser.cc 4.5 KB

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