From 0a646b110babee1f399035c6ca380a6abc636c73 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 1 Sep 2014 16:22:01 +0200 Subject: [PATCH] Store cookies in binary format. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We saved/opened cookies as UTF-8 which lead to an exception on saving with a cookie with an รค in it. ISO-8859-1 would be more appropriate, but we trust Qt to make the right choice and don't re-encode anything now. --- qutebrowser/browser/cookies.py | 7 ++++--- qutebrowser/config/lineparser.py | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/qutebrowser/browser/cookies.py b/qutebrowser/browser/cookies.py index 398197a08..a8b1ed64a 100644 --- a/qutebrowser/browser/cookies.py +++ b/qutebrowser/browser/cookies.py @@ -33,10 +33,11 @@ class CookieJar(QNetworkCookieJar): def __init__(self, parent=None): super().__init__(parent) datadir = utils.get_standard_dir(QStandardPaths.DataLocation) - self._linecp = lineparser.LineConfigParser(datadir, 'cookies') + self._linecp = lineparser.LineConfigParser(datadir, 'cookies', + binary=True) cookies = [] for line in self._linecp: - cookies += QNetworkCookie.parseCookies(line.encode('utf-8')) + cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies) def purge_old_cookies(self): @@ -72,6 +73,6 @@ class CookieJar(QNetworkCookieJar): lines = [] for cookie in self.allCookies(): if not cookie.isSessionCookie(): - lines.append(bytes(cookie.toRawForm()).decode('utf-8')) + lines.append(cookie.toRawForm()) self._linecp.data = lines self._linecp.save() diff --git a/qutebrowser/config/lineparser.py b/qutebrowser/config/lineparser.py index 78bca1383..c980ec303 100644 --- a/qutebrowser/config/lineparser.py +++ b/qutebrowser/config/lineparser.py @@ -35,19 +35,22 @@ class LineConfigParser: data: A list of lines. _configdir: The directory to read the config from. _configfile: The config file path. + _binary: Whether to open the file in binary mode. """ - def __init__(self, configdir, fname, limit=None): + def __init__(self, configdir, fname, limit=None, binary=False): """Config constructor. Args: configdir: Directory to read the config from. fname: Filename of the config file. limit: Config tuple (section, option) which contains a limit. + binary: Whether to open the file in binary mode. """ self._configdir = configdir self._configfile = os.path.join(self._configdir, fname) self._limit = limit + self._binary = binary if not os.path.isfile(self._configfile): self.data = [] else: @@ -60,8 +63,12 @@ class LineConfigParser: def read(self, filename): """Read the data from a file.""" - with open(filename, 'r', encoding='utf-8') as f: - self.data = [line.rstrip('\n') for line in f.readlines()] + if self._binary: + with open(filename, 'rb') as f: + self.data = [line.rstrip(b'\n') for line in f.readlines()] + else: + with open(filename, 'r', encoding='utf-8') as f: + self.data = [line.rstrip('\n') for line in f.readlines()] def write(self, fp, limit=-1): """Write the data to a file. @@ -74,7 +81,10 @@ class LineConfigParser: data = self.data else: data = self.data[-limit:] - fp.write('\n'.join(data)) + if self._binary: + fp.write(b'\n'.join(data)) + else: + fp.write('\n'.join(data)) def save(self): """Save the config file.""" @@ -89,8 +99,12 @@ class LineConfigParser: if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) log.destroy.debug("Saving config to {}".format(self._configfile)) - with open(self._configfile, 'w', encoding='utf-8') as f: - self.write(f, limit) + if self._binary: + with open(self._configfile, 'wb') as f: + self.write(f, limit) + else: + with open(self._configfile, 'w', encoding='utf-8') as f: + self.write(f, limit) @pyqtSlot(str, str) def on_config_changed(self, section, option):