summaryrefslogtreecommitdiff
path: root/src/Sqlite3CookieParser.cc
blob: 088ff99a9303439a72978b2ff582ce6b4a56159c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2010 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "Sqlite3CookieParser.h"

#include <cstring>

#include "DlAbortEx.h"
#include "util.h"
#include "StringFormat.h"
#include "A2STR.h"
#include "cookie_helper.h"
#ifndef HAVE_SQLITE3_OPEN_V2
# include "File.h"
#endif // !HAVE_SQLITE3_OPEN_V2

namespace aria2 {

Sqlite3CookieParser::Sqlite3CookieParser(const std::string& filename):db_(0)
{
  int ret;
#ifdef HAVE_SQLITE3_OPEN_V2
  ret = sqlite3_open_v2(filename.c_str(), &db_, SQLITE_OPEN_READONLY, 0);
#else // !HAVE_SQLITE3_OPEN_V2
  if(!File(filename).isFile()) {
    return;
  }
  ret = sqlite3_open(filename.c_str(), &db_);
#endif // !HAVE_SQLITE3_OPEN_V2
  if(SQLITE_OK != ret) {
    sqlite3_close(db_);
    db_ = 0;
  }
}

Sqlite3CookieParser::~Sqlite3CookieParser()
{
  sqlite3_close(db_);
}

static std::string toString(const char* str)
{
  if(str) {
    return str;
  } else {
    return A2STR::NIL;
  }
}

static bool parseTime(int64_t& time, const std::string& s)
{
  if(!util::parseLLIntNoThrow(time, s)) {
    return false;
  }
  if(sizeof(time_t) == 4 && time > INT32_MAX) {
    time = INT32_MAX;
  }
  return true;
}

static int cookieRowMapper(void* data, int columns,
                           char** values, char** names)
{
  if(columns != 7) {
    return 0;
  }
  std::vector<Cookie>& cookies =
    *reinterpret_cast<std::vector<Cookie>*>(data);
  std::string cookieDomain = cookie::removePrecedingDots(toString(values[0]));
  std::string cookieName = toString(values[4]);
  std::string cookiePath = toString(values[1]);
  if(cookieName.empty() || cookieDomain.empty() ||
     !cookie::goodPath(cookiePath)) {
    return 0;
  }
  int64_t expiryTime;
  if(!parseTime(expiryTime, toString(values[3]))) {
    return 0;
  }
  int64_t lastAccessTime;
  if(!parseTime(lastAccessTime, toString(values[6]))) {
    return 0;
  }
  Cookie c(cookieName,
           toString(values[5]), // value
           expiryTime,
           true, // persistent
           cookieDomain,
           util::isNumericHost(cookieDomain) || values[0][0] != '.', // hostOnly
           cookiePath,
           strcmp(toString(values[2]).c_str(), "1") == 0, //secure
           false,
           lastAccessTime // creation time. Set this later.
           );
  cookies.push_back(c);
  return 0;
}

void Sqlite3CookieParser::parse(std::vector<Cookie>& cookies)
{
  if(!db_) {
    throw DL_ABORT_EX(StringFormat("SQLite3 database is not opened.").str());
  }
  std::vector<Cookie> tcookies;
  char* sqlite3ErrMsg = 0;
  int ret = sqlite3_exec(db_, getQuery().c_str(), cookieRowMapper,
                         &tcookies, &sqlite3ErrMsg);
  std::string errMsg;
  if(sqlite3ErrMsg) {
    errMsg = sqlite3ErrMsg;
    sqlite3_free(sqlite3ErrMsg);
  }
  if(SQLITE_OK != ret) {
    throw DL_ABORT_EX(StringFormat("Failed to read SQLite3 database: %s",
                                   errMsg.c_str()).str());
  }
  cookies.swap(tcookies);
}

} // namespace aria2