LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
customcookiejar.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "customcookiejar.h"
31 #include <memory>
32 #include <QNetworkCookie>
33 #include <QtDebug>
34 #include <QDateTime>
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
41  : QNetworkCookieJar (parent)
42  , FilterTrackingCookies_ (false)
43  , Enabled_ (true)
44  , MatchDomainExactly_ (false)
45  {
46  }
47 
49  {
50  }
51 
53  {
54  FilterTrackingCookies_ = filter;
55  }
56 
57  void CustomCookieJar::SetEnabled (bool enabled)
58  {
59  Enabled_ = enabled;
60  }
61 
63  {
64  MatchDomainExactly_ = enabled;
65  }
66 
68  {
69  WL_ = list;
70  }
71 
73  {
74  BL_ = list;
75  }
76 
77  QByteArray CustomCookieJar::Save () const
78  {
79  QList<QNetworkCookie> cookies = allCookies ();
80  QByteArray result;
81  for (const auto& cookie : cookies)
82  {
83  if (cookie.isSessionCookie ())
84  continue;
85 
86  result += cookie.toRawForm ();
87  result += "\n";
88  }
89  return result;
90  }
91 
92  void CustomCookieJar::Load (const QByteArray& data)
93  {
94  QList<QNetworkCookie> cookies, filteredCookies;
95  for (const auto& ba : data.split ('\n'))
96  cookies << QNetworkCookie::parseCookies (ba);
97 
98  const auto& now = QDateTime::currentDateTime ();
99  for (const auto& cookie : cookies)
100  {
101  if (FilterTrackingCookies_ &&
102  cookie.name ().startsWith ("__utm"))
103  continue;
104 
105  if (cookie.expirationDate () < now)
106  continue;
107 
108  filteredCookies << cookie;
109  }
110  setAllCookies (filteredCookies);
111  }
112 
114  {
115  const auto& cookies = allCookies ();
116  QList<QNetworkCookie> result;
117  const auto& now = QDateTime::currentDateTime ();
118  for (const auto& cookie : cookies)
119  {
120  if (!cookie.isSessionCookie () &&
121  cookie.expirationDate () < now)
122  continue;
123 
124  if (result.contains (cookie))
125  continue;
126 
127  result << cookie;
128  }
129  qDebug () << Q_FUNC_INFO << cookies.size () << result.size ();
130  setAllCookies (result);
131  }
132 
134  {
135  if (!Enabled_)
136  return {};
137 
138  QList<QNetworkCookie> filtered;
139  for (const auto& cookie : QNetworkCookieJar::cookiesForUrl (url))
140  if (!filtered.contains (cookie))
141  filtered << cookie;
142  return filtered;
143  }
144 
145  namespace
146  {
147  bool MatchDomain (QString domain, QString cookieDomain)
148  {
149  auto normalize = [] (QString& s)
150  {
151  if (s.startsWith ('.'))
152  s = s.mid (1);
153  };
154  normalize (domain);
155  normalize (cookieDomain);
156 
157  if (domain == cookieDomain)
158  return true;
159 
160  const auto idx = domain.indexOf (cookieDomain);
161  return idx > 0 && domain.at (idx - 1) == '.';
162  }
163 
164  bool Check (const QList<QRegExp>& list, const QString& str)
165  {
166  for (auto& rx : list)
167  if (str == rx.pattern () || rx.exactMatch (str))
168  return true;
169 
170  return false;
171  }
172  }
173 
174  bool CustomCookieJar::setCookiesFromUrl (const QList<QNetworkCookie>& cookieList, const QUrl& url)
175  {
176  if (!Enabled_)
177  return false;
178 
179  QList<QNetworkCookie> filtered;
180  filtered.reserve (cookieList.size ());
181  for (auto cookie : cookieList)
182  {
183  if (cookie.domain ().isEmpty ())
184  cookie.setDomain (url.host ());
185 
186  bool checkWhitelist = false;
187  std::shared_ptr<void> wlGuard (nullptr, [&] (void*)
188  {
189  if (checkWhitelist && Check (WL_, cookie.domain ()))
190  filtered << cookie;
191  });
192 
193  if (MatchDomainExactly_ && !MatchDomain (url.host (), cookie.domain ()))
194  {
195  checkWhitelist = true;
196  continue;
197  }
198 
199  if (FilterTrackingCookies_ &&
200  cookie.name ().startsWith ("__utm"))
201  {
202  checkWhitelist = true;
203  continue;
204  }
205 
206  if (!Check (BL_, cookie.domain ()))
207  filtered << cookie;
208  }
209 
210  return QNetworkCookieJar::setCookiesFromUrl (filtered, url);
211  }
212 }
213 }