21 |
21 |
22 class CookieJar(QNetworkCookieJar): |
22 class CookieJar(QNetworkCookieJar): |
23 """ |
23 """ |
24 Class implementing a QNetworkCookieJar subclass with various accept |
24 Class implementing a QNetworkCookieJar subclass with various accept |
25 policies. |
25 policies. |
26 |
26 |
27 @signal cookiesChanged() emitted after the cookies have been changed |
27 @signal cookiesChanged() emitted after the cookies have been changed |
28 """ |
28 """ |
|
29 |
29 cookiesChanged = pyqtSignal() |
30 cookiesChanged = pyqtSignal() |
30 |
31 |
31 AcceptAlways = 0 |
32 AcceptAlways = 0 |
32 AcceptNever = 1 |
33 AcceptNever = 1 |
33 AcceptOnlyFromSitesNavigatedTo = 2 |
34 AcceptOnlyFromSitesNavigatedTo = 2 |
34 AcceptMax = 2 |
35 AcceptMax = 2 |
35 |
36 |
36 KeepUntilExpire = 0 |
37 KeepUntilExpire = 0 |
37 KeepUntilExit = 1 |
38 KeepUntilExit = 1 |
38 KeepMax = 1 |
39 KeepMax = 1 |
39 |
40 |
40 Allow = 0 |
41 Allow = 0 |
41 Block = 1 |
42 Block = 1 |
42 AllowForSession = 2 |
43 AllowForSession = 2 |
43 |
44 |
44 def __init__(self, parent=None): |
45 def __init__(self, parent=None): |
45 """ |
46 """ |
46 Constructor |
47 Constructor |
47 |
48 |
48 @param parent reference to the parent object (QObject) |
49 @param parent reference to the parent object (QObject) |
49 """ |
50 """ |
50 super().__init__(parent) |
51 super().__init__(parent) |
51 |
52 |
52 self.__loaded = False |
53 self.__loaded = False |
53 self.__acceptCookies = self.AcceptOnlyFromSitesNavigatedTo |
54 self.__acceptCookies = self.AcceptOnlyFromSitesNavigatedTo |
54 self.__saveTimer = AutoSaver(self, self.__save) |
55 self.__saveTimer = AutoSaver(self, self.__save) |
55 |
56 |
56 self.__cookiesFile = os.path.join(Utilities.getConfigDir(), |
57 self.__cookiesFile = os.path.join( |
57 "web_browser", "cookies.ini") |
58 Utilities.getConfigDir(), "web_browser", "cookies.ini" |
58 |
59 ) |
|
60 |
59 self.__store = WebBrowserWindow.webProfile().cookieStore() |
61 self.__store = WebBrowserWindow.webProfile().cookieStore() |
60 self.__store.setCookieFilter(self.__cookieFilter) |
62 self.__store.setCookieFilter(self.__cookieFilter) |
61 self.__store.cookieAdded.connect(self.__cookieAdded) |
63 self.__store.cookieAdded.connect(self.__cookieAdded) |
62 self.__store.cookieRemoved.connect(self.__cookieRemoved) |
64 self.__store.cookieRemoved.connect(self.__cookieRemoved) |
63 |
65 |
64 self.__load() |
66 self.__load() |
65 self.__store.loadAllCookies() |
67 self.__store.loadAllCookies() |
66 |
68 |
67 def close(self): |
69 def close(self): |
68 """ |
70 """ |
69 Public slot to close the cookie jar. |
71 Public slot to close the cookie jar. |
70 """ |
72 """ |
71 if not self.__loaded: |
73 if not self.__loaded: |
72 self.__load() |
74 self.__load() |
73 |
75 |
74 if self.__keepCookies == self.KeepUntilExit: |
76 if self.__keepCookies == self.KeepUntilExit: |
75 self.clear() |
77 self.clear() |
76 self.__saveTimer.saveIfNeccessary() |
78 self.__saveTimer.saveIfNeccessary() |
77 |
79 |
78 def clear(self): |
80 def clear(self): |
79 """ |
81 """ |
80 Public method to clear all cookies. |
82 Public method to clear all cookies. |
81 """ |
83 """ |
82 if not self.__loaded: |
84 if not self.__loaded: |
83 self.__load() |
85 self.__load() |
84 |
86 |
85 self.setAllCookies([]) |
87 self.setAllCookies([]) |
86 self.__store.deleteAllCookies() |
88 self.__store.deleteAllCookies() |
87 self.cookiesChanged.emit() |
89 self.cookiesChanged.emit() |
88 |
90 |
89 def removeCookies(self, cookies): |
91 def removeCookies(self, cookies): |
90 """ |
92 """ |
91 Public method to remove a list of cookies. |
93 Public method to remove a list of cookies. |
92 |
94 |
93 @param cookies list of cookies to be removed |
95 @param cookies list of cookies to be removed |
94 @type list of QNetworkCookie |
96 @type list of QNetworkCookie |
95 """ |
97 """ |
96 wasBlocked = self.blockSignals(True) |
98 wasBlocked = self.blockSignals(True) |
97 for cookie in cookies: |
99 for cookie in cookies: |
98 self.__store.deleteCookie(cookie) |
100 self.__store.deleteCookie(cookie) |
99 self.blockSignals(wasBlocked) |
101 self.blockSignals(wasBlocked) |
100 |
102 |
101 self.cookiesChanged.emit() |
103 self.cookiesChanged.emit() |
102 |
104 |
103 def removeCookie(self, cookie): |
105 def removeCookie(self, cookie): |
104 """ |
106 """ |
105 Public method to remove a cookie. |
107 Public method to remove a cookie. |
106 |
108 |
107 @param cookie cookie to be removed |
109 @param cookie cookie to be removed |
108 @type QNetworkCookie |
110 @type QNetworkCookie |
109 """ |
111 """ |
110 self.__store.deleteCookie(cookie) |
112 self.__store.deleteCookie(cookie) |
111 self.cookiesChanged.emit() |
113 self.cookiesChanged.emit() |
112 |
114 |
113 def __load(self): |
115 def __load(self): |
114 """ |
116 """ |
115 Private method to load the cookies settings. |
117 Private method to load the cookies settings. |
116 """ |
118 """ |
117 if self.__loaded: |
119 if self.__loaded: |
118 return |
120 return |
119 |
121 |
120 cookieSettings = QSettings(self.__cookiesFile, |
122 cookieSettings = QSettings(self.__cookiesFile, QSettings.Format.IniFormat) |
121 QSettings.Format.IniFormat) |
123 |
122 |
|
123 # load exceptions |
124 # load exceptions |
124 self.__exceptionsBlock = Preferences.toList( |
125 self.__exceptionsBlock = Preferences.toList( |
125 cookieSettings.value("Exceptions/block")) |
126 cookieSettings.value("Exceptions/block") |
|
127 ) |
126 self.__exceptionsAllow = Preferences.toList( |
128 self.__exceptionsAllow = Preferences.toList( |
127 cookieSettings.value("Exceptions/allow")) |
129 cookieSettings.value("Exceptions/allow") |
|
130 ) |
128 self.__exceptionsAllowForSession = Preferences.toList( |
131 self.__exceptionsAllowForSession = Preferences.toList( |
129 cookieSettings.value("Exceptions/allowForSession")) |
132 cookieSettings.value("Exceptions/allowForSession") |
|
133 ) |
130 self.__exceptionsBlock.sort() |
134 self.__exceptionsBlock.sort() |
131 self.__exceptionsAllow.sort() |
135 self.__exceptionsAllow.sort() |
132 self.__exceptionsAllowForSession.sort() |
136 self.__exceptionsAllowForSession.sort() |
133 |
137 |
134 self.__acceptCookies = Preferences.getWebBrowser("AcceptCookies") |
138 self.__acceptCookies = Preferences.getWebBrowser("AcceptCookies") |
135 self.__keepCookies = Preferences.getWebBrowser("KeepCookiesUntil") |
139 self.__keepCookies = Preferences.getWebBrowser("KeepCookiesUntil") |
136 if self.__keepCookies == self.KeepUntilExit: |
140 if self.__keepCookies == self.KeepUntilExit: |
137 self.clear() |
141 self.clear() |
138 |
142 |
139 self.__filterTrackingCookies = Preferences.toBool( |
143 self.__filterTrackingCookies = Preferences.toBool( |
140 Preferences.getWebBrowser("FilterTrackingCookies")) |
144 Preferences.getWebBrowser("FilterTrackingCookies") |
141 |
145 ) |
|
146 |
142 self.__loaded = True |
147 self.__loaded = True |
143 self.cookiesChanged.emit() |
148 self.cookiesChanged.emit() |
144 |
149 |
145 def __save(self): |
150 def __save(self): |
146 """ |
151 """ |
147 Private method to save the cookies settings. |
152 Private method to save the cookies settings. |
148 """ |
153 """ |
149 if not self.__loaded: |
154 if not self.__loaded: |
150 return |
155 return |
151 |
156 |
152 cookieSettings = QSettings(self.__cookiesFile, |
157 cookieSettings = QSettings(self.__cookiesFile, QSettings.Format.IniFormat) |
153 QSettings.Format.IniFormat) |
158 |
154 |
|
155 cookieSettings.setValue("Exceptions/block", self.__exceptionsBlock) |
159 cookieSettings.setValue("Exceptions/block", self.__exceptionsBlock) |
156 cookieSettings.setValue("Exceptions/allow", self.__exceptionsAllow) |
160 cookieSettings.setValue("Exceptions/allow", self.__exceptionsAllow) |
157 cookieSettings.setValue("Exceptions/allowForSession", |
161 cookieSettings.setValue( |
158 self.__exceptionsAllowForSession) |
162 "Exceptions/allowForSession", self.__exceptionsAllowForSession |
159 |
163 ) |
|
164 |
160 Preferences.setWebBrowser("AcceptCookies", self.__acceptCookies) |
165 Preferences.setWebBrowser("AcceptCookies", self.__acceptCookies) |
161 Preferences.setWebBrowser("KeepCookiesUntil", self.__keepCookies) |
166 Preferences.setWebBrowser("KeepCookiesUntil", self.__keepCookies) |
162 Preferences.setWebBrowser("FilterTrackingCookies", |
167 Preferences.setWebBrowser("FilterTrackingCookies", self.__filterTrackingCookies) |
163 self.__filterTrackingCookies) |
168 |
164 |
|
165 @pyqtSlot(QNetworkCookie) |
169 @pyqtSlot(QNetworkCookie) |
166 def __cookieAdded(self, cookie): |
170 def __cookieAdded(self, cookie): |
167 """ |
171 """ |
168 Private slot handling the addition of a cookie. |
172 Private slot handling the addition of a cookie. |
169 |
173 |
170 @param cookie cookie which was added |
174 @param cookie cookie which was added |
171 @type QNetworkCookie |
175 @type QNetworkCookie |
172 """ |
176 """ |
173 if self.__rejectCookie(cookie, cookie.domain()): |
177 if self.__rejectCookie(cookie, cookie.domain()): |
174 self.__store.deleteCookie(cookie) |
178 self.__store.deleteCookie(cookie) |
175 return |
179 return |
176 |
180 |
177 self.insertCookie(cookie) |
181 self.insertCookie(cookie) |
178 self.cookiesChanged.emit() |
182 self.cookiesChanged.emit() |
179 |
183 |
180 @pyqtSlot(QNetworkCookie) |
184 @pyqtSlot(QNetworkCookie) |
181 def __cookieRemoved(self, cookie): |
185 def __cookieRemoved(self, cookie): |
182 """ |
186 """ |
183 Private slot handling the removal of a cookie. |
187 Private slot handling the removal of a cookie. |
184 |
188 |
185 @param cookie cookie which was removed |
189 @param cookie cookie which was removed |
186 @type QNetworkCookie |
190 @type QNetworkCookie |
187 """ |
191 """ |
188 if self.deleteCookie(cookie): |
192 if self.deleteCookie(cookie): |
189 self.cookiesChanged.emit() |
193 self.cookiesChanged.emit() |
190 |
194 |
191 def __cookieFilter(self, request): |
195 def __cookieFilter(self, request): |
192 """ |
196 """ |
193 Private method to filter cookies. |
197 Private method to filter cookies. |
194 |
198 |
195 Note: This method is used for Qt 5.11+ only. |
199 Note: This method is used for Qt 5.11+ only. |
196 |
200 |
197 @param request reference to the cookie filter request object |
201 @param request reference to the cookie filter request object |
198 @type QWebEngineCookieStore.FilterRequest |
202 @type QWebEngineCookieStore.FilterRequest |
199 @return flag indicating cookie access is allowed |
203 @return flag indicating cookie access is allowed |
200 @rtype bool |
204 @rtype bool |
201 """ |
205 """ |
202 if not self.__loaded: |
206 if not self.__loaded: |
203 self.__load() |
207 self.__load() |
204 |
208 |
205 if self.__acceptCookies == self.AcceptNever: |
209 if self.__acceptCookies == self.AcceptNever: |
206 res = self.__isOnDomainList(self.__exceptionsAllow, |
210 res = self.__isOnDomainList(self.__exceptionsAllow, request.origin.host()) |
207 request.origin.host()) |
|
208 if not res: |
211 if not res: |
209 return False |
212 return False |
210 |
213 |
211 if self.__acceptCookies == self.AcceptAlways: |
214 if self.__acceptCookies == self.AcceptAlways: |
212 res = self.__isOnDomainList(self.__exceptionsBlock, |
215 res = self.__isOnDomainList(self.__exceptionsBlock, request.origin.host()) |
213 request.origin.host()) |
|
214 if res: |
216 if res: |
215 return False |
217 return False |
216 |
218 |
217 if ( |
219 if ( |
218 self.__acceptCookies == self.AcceptOnlyFromSitesNavigatedTo and |
220 self.__acceptCookies == self.AcceptOnlyFromSitesNavigatedTo |
219 request.thirdParty |
221 and request.thirdParty |
220 ): |
222 ): |
221 return False |
223 return False |
222 |
224 |
223 return True |
225 return True |
224 |
226 |
225 def __rejectCookie(self, cookie, cookieDomain): |
227 def __rejectCookie(self, cookie, cookieDomain): |
226 """ |
228 """ |
227 Private method to test, if a cookie shall be rejected. |
229 Private method to test, if a cookie shall be rejected. |
228 |
230 |
229 @param cookie cookie to be tested |
231 @param cookie cookie to be tested |
230 @type QNetworkCookie |
232 @type QNetworkCookie |
231 @param cookieDomain domain of the cookie |
233 @param cookieDomain domain of the cookie |
232 @type str |
234 @type str |
233 @return flag indicating the cookie shall be rejected |
235 @return flag indicating the cookie shall be rejected |
234 @rtype bool |
236 @rtype bool |
235 """ |
237 """ |
236 if not self.__loaded: |
238 if not self.__loaded: |
237 self.__load() |
239 self.__load() |
238 |
240 |
239 if self.__acceptCookies == self.AcceptNever: |
241 if self.__acceptCookies == self.AcceptNever: |
240 res = self.__isOnDomainList(self.__exceptionsAllow, cookieDomain) |
242 res = self.__isOnDomainList(self.__exceptionsAllow, cookieDomain) |
241 if not res: |
243 if not res: |
242 return True |
244 return True |
243 |
245 |
244 if self.__acceptCookies == self.AcceptAlways: |
246 if self.__acceptCookies == self.AcceptAlways: |
245 res = self.__isOnDomainList(self.__exceptionsBlock, cookieDomain) |
247 res = self.__isOnDomainList(self.__exceptionsBlock, cookieDomain) |
246 if res: |
248 if res: |
247 return True |
249 return True |
248 |
250 |
249 if self.__acceptCookies == self.AcceptOnlyFromSitesNavigatedTo: |
251 if self.__acceptCookies == self.AcceptOnlyFromSitesNavigatedTo: |
250 mainWindow = WebBrowserWindow.mainWindow() |
252 mainWindow = WebBrowserWindow.mainWindow() |
251 if mainWindow is not None: |
253 if mainWindow is not None: |
252 browser = mainWindow.getWindow().currentBrowser() |
254 browser = mainWindow.getWindow().currentBrowser() |
253 if browser is not None: |
255 if browser is not None: |
257 else: |
259 else: |
258 host = "" |
260 host = "" |
259 res = self.__matchDomain(cookieDomain, host) |
261 res = self.__matchDomain(cookieDomain, host) |
260 if not res: |
262 if not res: |
261 return True |
263 return True |
262 |
264 |
263 if self.__filterTrackingCookies and cookie.name().startsWith(b"__utm"): |
265 if self.__filterTrackingCookies and cookie.name().startsWith(b"__utm"): |
264 return True |
266 return True |
265 |
267 |
266 return False |
268 return False |
267 |
269 |
268 def acceptPolicy(self): |
270 def acceptPolicy(self): |
269 """ |
271 """ |
270 Public method to get the accept policy. |
272 Public method to get the accept policy. |
271 |
273 |
272 @return current accept policy |
274 @return current accept policy |
273 """ |
275 """ |
274 if not self.__loaded: |
276 if not self.__loaded: |
275 self.__load() |
277 self.__load() |
276 |
278 |
277 return self.__acceptCookies |
279 return self.__acceptCookies |
278 |
280 |
279 def setAcceptPolicy(self, policy): |
281 def setAcceptPolicy(self, policy): |
280 """ |
282 """ |
281 Public method to set the accept policy. |
283 Public method to set the accept policy. |
282 |
284 |
283 @param policy accept policy to be set |
285 @param policy accept policy to be set |
284 """ |
286 """ |
285 if not self.__loaded: |
287 if not self.__loaded: |
286 self.__load() |
288 self.__load() |
287 |
289 |
288 if policy > self.AcceptMax: |
290 if policy > self.AcceptMax: |
289 return |
291 return |
290 if policy == self.__acceptCookies: |
292 if policy == self.__acceptCookies: |
291 return |
293 return |
292 |
294 |
293 self.__acceptCookies = policy |
295 self.__acceptCookies = policy |
294 self.__saveTimer.changeOccurred() |
296 self.__saveTimer.changeOccurred() |
295 |
297 |
296 def keepPolicy(self): |
298 def keepPolicy(self): |
297 """ |
299 """ |
298 Public method to get the keep policy. |
300 Public method to get the keep policy. |
299 |
301 |
300 @return keep policy |
302 @return keep policy |
301 """ |
303 """ |
302 if not self.__loaded: |
304 if not self.__loaded: |
303 self.__load() |
305 self.__load() |
304 |
306 |
305 return self.__keepCookies |
307 return self.__keepCookies |
306 |
308 |
307 def setKeepPolicy(self, policy): |
309 def setKeepPolicy(self, policy): |
308 """ |
310 """ |
309 Public method to set the keep policy. |
311 Public method to set the keep policy. |
310 |
312 |
311 @param policy keep policy to be set |
313 @param policy keep policy to be set |
312 """ |
314 """ |
313 if not self.__loaded: |
315 if not self.__loaded: |
314 self.__load() |
316 self.__load() |
315 |
317 |
316 if policy > self.KeepMax: |
318 if policy > self.KeepMax: |
317 return |
319 return |
318 if policy == self.__keepCookies: |
320 if policy == self.__keepCookies: |
319 return |
321 return |
320 |
322 |
321 self.__keepCookies = policy |
323 self.__keepCookies = policy |
322 self.__saveTimer.changeOccurred() |
324 self.__saveTimer.changeOccurred() |
323 |
325 |
324 def blockedCookies(self): |
326 def blockedCookies(self): |
325 """ |
327 """ |
326 Public method to return the list of blocked domains. |
328 Public method to return the list of blocked domains. |
327 |
329 |
328 @return list of blocked domains (list of strings) |
330 @return list of blocked domains (list of strings) |
329 """ |
331 """ |
330 if not self.__loaded: |
332 if not self.__loaded: |
331 self.__load() |
333 self.__load() |
332 |
334 |
333 return self.__exceptionsBlock |
335 return self.__exceptionsBlock |
334 |
336 |
335 def allowedCookies(self): |
337 def allowedCookies(self): |
336 """ |
338 """ |
337 Public method to return the list of allowed domains. |
339 Public method to return the list of allowed domains. |
338 |
340 |
339 @return list of allowed domains (list of strings) |
341 @return list of allowed domains (list of strings) |
340 """ |
342 """ |
341 if not self.__loaded: |
343 if not self.__loaded: |
342 self.__load() |
344 self.__load() |
343 |
345 |
344 return self.__exceptionsAllow |
346 return self.__exceptionsAllow |
345 |
347 |
346 def allowForSessionCookies(self): |
348 def allowForSessionCookies(self): |
347 """ |
349 """ |
348 Public method to return the list of allowed session cookie domains. |
350 Public method to return the list of allowed session cookie domains. |
349 |
351 |
350 @return list of allowed session cookie domains (list of strings) |
352 @return list of allowed session cookie domains (list of strings) |
351 """ |
353 """ |
352 if not self.__loaded: |
354 if not self.__loaded: |
353 self.__load() |
355 self.__load() |
354 |
356 |
355 return self.__exceptionsAllowForSession |
357 return self.__exceptionsAllowForSession |
356 |
358 |
357 def setBlockedCookies(self, list_): |
359 def setBlockedCookies(self, list_): |
358 """ |
360 """ |
359 Public method to set the list of blocked domains. |
361 Public method to set the list of blocked domains. |
360 |
362 |
361 @param list_ list of blocked domains (list of strings) |
363 @param list_ list of blocked domains (list of strings) |
362 """ |
364 """ |
363 if not self.__loaded: |
365 if not self.__loaded: |
364 self.__load() |
366 self.__load() |
365 |
367 |
366 self.__exceptionsBlock = list_[:] |
368 self.__exceptionsBlock = list_[:] |
367 self.__exceptionsBlock.sort() |
369 self.__exceptionsBlock.sort() |
368 self.__saveTimer.changeOccurred() |
370 self.__saveTimer.changeOccurred() |
369 |
371 |
370 def setAllowedCookies(self, list_): |
372 def setAllowedCookies(self, list_): |
371 """ |
373 """ |
372 Public method to set the list of allowed domains. |
374 Public method to set the list of allowed domains. |
373 |
375 |
374 @param list_ list of allowed domains (list of strings) |
376 @param list_ list of allowed domains (list of strings) |
375 """ |
377 """ |
376 if not self.__loaded: |
378 if not self.__loaded: |
377 self.__load() |
379 self.__load() |
378 |
380 |
379 self.__exceptionsAllow = list_[:] |
381 self.__exceptionsAllow = list_[:] |
380 self.__exceptionsAllow.sort() |
382 self.__exceptionsAllow.sort() |
381 self.__saveTimer.changeOccurred() |
383 self.__saveTimer.changeOccurred() |
382 |
384 |
383 def setAllowForSessionCookies(self, list_): |
385 def setAllowForSessionCookies(self, list_): |
384 """ |
386 """ |
385 Public method to set the list of allowed session cookie domains. |
387 Public method to set the list of allowed session cookie domains. |
386 |
388 |
387 @param list_ list of allowed session cookie domains (list of strings) |
389 @param list_ list of allowed session cookie domains (list of strings) |
388 """ |
390 """ |
389 if not self.__loaded: |
391 if not self.__loaded: |
390 self.__load() |
392 self.__load() |
391 |
393 |
392 self.__exceptionsAllowForSession = list_[:] |
394 self.__exceptionsAllowForSession = list_[:] |
393 self.__exceptionsAllowForSession.sort() |
395 self.__exceptionsAllowForSession.sort() |
394 self.__saveTimer.changeOccurred() |
396 self.__saveTimer.changeOccurred() |
395 |
397 |
396 def filterTrackingCookies(self): |
398 def filterTrackingCookies(self): |
397 """ |
399 """ |
398 Public method to get the filter tracking cookies flag. |
400 Public method to get the filter tracking cookies flag. |
399 |
401 |
400 @return filter tracking cookies flag (boolean) |
402 @return filter tracking cookies flag (boolean) |
401 """ |
403 """ |
402 return self.__filterTrackingCookies |
404 return self.__filterTrackingCookies |
403 |
405 |
404 def setFilterTrackingCookies(self, filterTrackingCookies): |
406 def setFilterTrackingCookies(self, filterTrackingCookies): |
405 """ |
407 """ |
406 Public method to set the filter tracking cookies flag. |
408 Public method to set the filter tracking cookies flag. |
407 |
409 |
408 @param filterTrackingCookies filter tracking cookies flag (boolean) |
410 @param filterTrackingCookies filter tracking cookies flag (boolean) |
409 """ |
411 """ |
410 if filterTrackingCookies == self.__filterTrackingCookies: |
412 if filterTrackingCookies == self.__filterTrackingCookies: |
411 return |
413 return |
412 |
414 |
413 self.__filterTrackingCookies = filterTrackingCookies |
415 self.__filterTrackingCookies = filterTrackingCookies |
414 self.__saveTimer.changeOccurred() |
416 self.__saveTimer.changeOccurred() |
415 |
417 |
416 def __isOnDomainList(self, rules, domain): |
418 def __isOnDomainList(self, rules, domain): |
417 """ |
419 """ |
418 Private method to check, if either the rule matches the domain exactly |
420 Private method to check, if either the rule matches the domain exactly |
419 or the domain ends with ".rule". |
421 or the domain ends with ".rule". |
420 |
422 |
421 @param rules list of rules (list of strings) |
423 @param rules list of rules (list of strings) |
422 @param domain domain name to check (string) |
424 @param domain domain name to check (string) |
423 @return flag indicating a match (boolean) |
425 @return flag indicating a match (boolean) |
424 """ |
426 """ |
425 for rule in rules: |
427 for rule in rules: |
426 if rule.startswith("."): |
428 if rule.startswith("."): |
427 if domain.endswith(rule): |
429 if domain.endswith(rule): |
428 return True |
430 return True |
429 |
431 |
430 withoutDot = rule[1:] |
432 withoutDot = rule[1:] |
431 if domain == withoutDot: |
433 if domain == withoutDot: |
432 return True |
434 return True |
433 else: |
435 else: |
434 domainEnding = domain[-(len(rule) + 1):] |
436 domainEnding = domain[-(len(rule) + 1) :] |
435 if ( |
437 if domainEnding and domainEnding[0] == "." and domain.endswith(rule): |
436 domainEnding and |
|
437 domainEnding[0] == "." and |
|
438 domain.endswith(rule) |
|
439 ): |
|
440 return True |
438 return True |
441 |
439 |
442 if rule == domain: |
440 if rule == domain: |
443 return True |
441 return True |
444 |
442 |
445 return False |
443 return False |
446 |
444 |
447 def __matchDomain(self, cookieDomain, siteDomain): |
445 def __matchDomain(self, cookieDomain, siteDomain): |
448 """ |
446 """ |
449 Private method to check, if a URLs host matches a cookie domain |
447 Private method to check, if a URLs host matches a cookie domain |
450 according to RFC 6265. |
448 according to RFC 6265. |
451 |
449 |
452 @param cookieDomain domain of the cookie |
450 @param cookieDomain domain of the cookie |
453 @type str |
451 @type str |
454 @param siteDomain domain or host of an URL |
452 @param siteDomain domain or host of an URL |
455 @type str |
453 @type str |
456 @return flag indicating a match |
454 @return flag indicating a match |
457 @rtype bool |
455 @rtype bool |
458 """ |
456 """ |
459 if not siteDomain: |
457 if not siteDomain: |
460 # empty URLs always match |
458 # empty URLs always match |
461 return True |
459 return True |
462 |
460 |
463 if cookieDomain.startswith("."): |
461 if cookieDomain.startswith("."): |
464 cookieDomain = cookieDomain[1:] |
462 cookieDomain = cookieDomain[1:] |
465 if siteDomain.startswith("."): |
463 if siteDomain.startswith("."): |
466 siteDomain = siteDomain[1:] |
464 siteDomain = siteDomain[1:] |
467 |
465 |
468 if cookieDomain == siteDomain: |
466 if cookieDomain == siteDomain: |
469 return True |
467 return True |
470 |
468 |
471 if not siteDomain.endswith(cookieDomain): |
469 if not siteDomain.endswith(cookieDomain): |
472 return False |
470 return False |
473 |
471 |
474 index = siteDomain.find(cookieDomain) |
472 index = siteDomain.find(cookieDomain) |
475 return index > 0 and siteDomain[index - 1] == "." |
473 return index > 0 and siteDomain[index - 1] == "." |
476 |
474 |
477 def cookies(self): |
475 def cookies(self): |
478 """ |
476 """ |
479 Public method to get the cookies of the cookie jar. |
477 Public method to get the cookies of the cookie jar. |
480 |
478 |
481 @return list of all cookies (list of QNetworkCookie) |
479 @return list of all cookies (list of QNetworkCookie) |
482 """ |
480 """ |
483 if not self.__loaded: |
481 if not self.__loaded: |
484 self.__load() |
482 self.__load() |
485 |
483 |
486 return self.allCookies() |
484 return self.allCookies() |
487 |
485 |
488 def cookieDomains(self): |
486 def cookieDomains(self): |
489 """ |
487 """ |
490 Public method to get a list of all domains used by the cookies. |
488 Public method to get a list of all domains used by the cookies. |
491 |
489 |
492 @return list of domain names |
490 @return list of domain names |
493 @rtype list of str |
491 @rtype list of str |
494 """ |
492 """ |
495 domains = [] |
493 domains = [] |
496 for cookie in self.cookies(): |
494 for cookie in self.cookies(): |
497 domain = cookie.domain() |
495 domain = cookie.domain() |
498 if domain not in domains: |
496 if domain not in domains: |
499 domains.append(domain) |
497 domains.append(domain) |
500 |
498 |
501 return domains |
499 return domains |