From 354c3c8c9b3e75988b334c1526b654c3e7caf8d9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 18 Oct 2017 09:02:39 +0200 Subject: [PATCH] Handle unknown filetypes with qute://help --- doc/changelog.asciidoc | 1 + qutebrowser/browser/qutescheme.py | 9 ++++++--- tests/unit/browser/test_qutescheme.py | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index 8faf5452a..03eca9485 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -35,6 +35,7 @@ Fixed - More consistent sizing for favicons with vertical tabs. - Using `:home` on pinned tabs is now prevented. +- Fix crash with unknown file types loaded via qute://help Deprecated ~~~~~~~~~~ diff --git a/qutebrowser/browser/qutescheme.py b/qutebrowser/browser/qutescheme.py index b49520002..2498ea7f6 100644 --- a/qutebrowser/browser/qutescheme.py +++ b/qutebrowser/browser/qutescheme.py @@ -29,8 +29,9 @@ import os import time import urllib.parse import textwrap -import pkg_resources +import mimetypes +import pkg_resources from PyQt5.QtCore import QUrlQuery, QUrl import qutebrowser @@ -323,8 +324,10 @@ def qute_help(url): "scripts/asciidoc2html.py.") path = 'html/doc/{}'.format(urlpath) - if urlpath.endswith('.png'): - return 'image/png', utils.read_file(path, binary=True) + if not urlpath.endswith('.html'): + mimetype, _encoding = mimetypes.guess_type(urlpath) + assert mimetype is not None, url + return mimetype, utils.read_file(path, binary=True) try: data = utils.read_file(path) diff --git a/tests/unit/browser/test_qutescheme.py b/tests/unit/browser/test_qutescheme.py index 02e0c484d..6fdaad83c 100644 --- a/tests/unit/browser/test_qutescheme.py +++ b/tests/unit/browser/test_qutescheme.py @@ -146,3 +146,26 @@ class TestHistoryHandler: url = QUrl("qute://history/data?start_time={}".format(now)) _mimetype, data = benchmark(qutescheme.qute_history, url) assert len(json.loads(data)) > 1 + + +class TestHelpHandler: + + """Tests for qute://help.""" + + @pytest.fixture + def data_patcher(self, monkeypatch): + def _patch(path, data): + def _read_file(name, binary=False): + assert path == name + if binary: + return data + return data.decode('utf-8') + + monkeypatch.setattr(qutescheme.utils, 'read_file', _read_file) + return _patch + + def test_unknown_file_type(self, data_patcher): + data_patcher('html/doc/foo.bin', b'\xff') + mimetype, data = qutescheme.qute_help(QUrl('qute://help/foo.bin')) + assert mimetype == 'application/octet-stream' + assert data == b'\xff'