2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-05-05 17:56:35 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser 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 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser 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 qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Handling of HTTP cookies."""
|
|
|
|
|
2014-05-08 22:33:24 +02:00
|
|
|
from PyQt5.QtNetwork import QNetworkCookie, QNetworkCookieJar
|
2015-02-22 19:13:51 +01:00
|
|
|
from PyQt5.QtCore import pyqtSignal, QDateTime
|
2014-05-08 22:33:24 +02:00
|
|
|
|
2014-12-13 17:28:50 +01:00
|
|
|
from qutebrowser.config import config
|
2014-10-18 19:50:10 +02:00
|
|
|
from qutebrowser.utils import utils, standarddir, objreg
|
2015-03-02 20:07:26 +01:00
|
|
|
from qutebrowser.misc import lineparser
|
2014-05-05 17:56:35 +02:00
|
|
|
|
|
|
|
|
2014-12-10 13:12:53 +01:00
|
|
|
class RAMCookieJar(QNetworkCookieJar):
|
2014-05-05 17:56:35 +02:00
|
|
|
|
2015-01-31 22:56:23 +01:00
|
|
|
"""An in-RAM cookie jar.
|
|
|
|
|
|
|
|
Signals:
|
|
|
|
changed: Emitted when the cookie store was changed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
changed = pyqtSignal()
|
|
|
|
|
2014-12-10 13:12:53 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return utils.get_repr(self, count=len(self.allCookies()))
|
|
|
|
|
|
|
|
def setCookiesFromUrl(self, cookies, url):
|
|
|
|
"""Add the cookies in the cookies list to this cookie jar.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cookies: A list of QNetworkCookies.
|
|
|
|
url: The URL to set the cookies for.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if one or more cookies are set for 'url', otherwise False.
|
|
|
|
"""
|
|
|
|
if config.get('content', 'cookies-accept') == 'never':
|
|
|
|
return False
|
|
|
|
else:
|
2015-01-31 22:56:23 +01:00
|
|
|
self.changed.emit()
|
2014-12-10 13:12:53 +01:00
|
|
|
return super().setCookiesFromUrl(cookies, url)
|
|
|
|
|
|
|
|
|
|
|
|
class CookieJar(RAMCookieJar):
|
|
|
|
|
|
|
|
"""A cookie jar saving cookies to disk.
|
|
|
|
|
|
|
|
Attributes:
|
2015-03-02 20:07:26 +01:00
|
|
|
_lineparser: The LineParser managing the cookies file.
|
2014-12-10 13:12:53 +01:00
|
|
|
"""
|
2014-05-05 17:56:35 +02:00
|
|
|
|
2015-09-04 11:44:59 +02:00
|
|
|
def __init__(self, parent=None, *, line_parser=None):
|
2014-06-17 07:17:21 +02:00
|
|
|
super().__init__(parent)
|
2015-09-03 17:24:01 +02:00
|
|
|
|
|
|
|
if line_parser:
|
|
|
|
self._lineparser = line_parser
|
|
|
|
else:
|
|
|
|
self._lineparser = lineparser.LineParser(
|
|
|
|
standarddir.data(), 'cookies', binary=True, parent=self)
|
|
|
|
self.parse_cookies()
|
2014-10-18 19:50:10 +02:00
|
|
|
objreg.get('config').changed.connect(self.cookies_store_changed)
|
2015-01-31 22:56:23 +01:00
|
|
|
objreg.get('save-manager').add_saveable(
|
|
|
|
'cookies', self.save, self.changed,
|
|
|
|
config_opt=('content', 'cookies-store'))
|
2014-05-05 17:56:35 +02:00
|
|
|
|
2015-09-03 17:24:01 +02:00
|
|
|
def parse_cookies(self):
|
|
|
|
"""Parse cookies from lineparser and store them."""
|
|
|
|
cookies = []
|
|
|
|
for line in self._lineparser:
|
|
|
|
cookies += QNetworkCookie.parseCookies(line)
|
|
|
|
self.setAllCookies(cookies)
|
|
|
|
|
2014-05-26 16:09:49 +02:00
|
|
|
def purge_old_cookies(self):
|
|
|
|
"""Purge expired cookies from the cookie jar."""
|
|
|
|
# Based on:
|
2015-06-12 16:59:33 +02:00
|
|
|
# http://doc.qt.io/qt-5/qtwebkitexamples-webkitwidgets-browser-cookiejar-cpp.html
|
2014-05-26 16:09:49 +02:00
|
|
|
now = QDateTime.currentDateTime()
|
|
|
|
cookies = [c for c in self.allCookies()
|
|
|
|
if c.isSessionCookie() or c.expirationDate() >= now]
|
|
|
|
self.setAllCookies(cookies)
|
|
|
|
|
2014-05-05 17:56:35 +02:00
|
|
|
def save(self):
|
|
|
|
"""Save cookies to disk."""
|
2014-05-26 16:09:49 +02:00
|
|
|
self.purge_old_cookies()
|
2014-05-05 17:56:35 +02:00
|
|
|
lines = []
|
|
|
|
for cookie in self.allCookies():
|
|
|
|
if not cookie.isSessionCookie():
|
2014-09-01 16:22:01 +02:00
|
|
|
lines.append(cookie.toRawForm())
|
2015-03-02 20:07:26 +01:00
|
|
|
self._lineparser.data = lines
|
|
|
|
self._lineparser.save()
|
2014-10-14 21:24:09 +02:00
|
|
|
|
2014-11-26 21:16:27 +01:00
|
|
|
@config.change_filter('content', 'cookies-store')
|
2014-10-14 21:24:09 +02:00
|
|
|
def cookies_store_changed(self):
|
|
|
|
"""Delete stored cookies if cookies-store changed."""
|
2014-11-26 21:16:27 +01:00
|
|
|
if not config.get('content', 'cookies-store'):
|
2015-03-02 20:07:26 +01:00
|
|
|
self._lineparser.data = []
|
|
|
|
self._lineparser.save()
|
2015-01-31 22:56:23 +01:00
|
|
|
self.changed.emit()
|