Sqlite3CookieParser.cc 4.8 KB

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