Use a data: URL for the broken qutebrowser logo

It blows our HTML up, but we use error.html from various places with
various security policies, so we can't rely on being able to load
file:// URLs.
This commit is contained in:
Florian Bruhin 2016-11-10 08:37:12 +01:00
parent f1bba45db5
commit bddda6b778
3 changed files with 46 additions and 3 deletions

View File

@ -73,13 +73,13 @@ function searchFor(uri) {
<table>
<tr>
<td style="width: 10%; vertical-align: top;">
<img style="width: 100%; display: block; max-width: 256px;" src="{{ resource_url('img/broken_qutebrowser_logo.png') }}" />
<img style="width: 100%; display: block; max-width: 256px;" src="{{ data_url('img/broken_qutebrowser_logo.png') }}" />
</td>
<td style="padding-left: 40px;">
<h1>Unable to load page</h1>
Error while opening {{ url }}: <br>
<p id="error-message-text" style="color: #a31a1a;">{{ error }}</p><br><br>
<form name="bl">
<input type="button" value="Try again" onclick="javascript:tryagain()" />
</form>

View File

@ -22,6 +22,8 @@
import os
import os.path
import traceback
import mimetypes
import base64
import jinja2
import jinja2.exceptions
@ -74,6 +76,16 @@ def resource_url(path):
return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded)
def data_url(path):
"""Get a data: url for the broken qutebrowser logo."""
data = utils.read_file(path, binary=True)
filename = utils.resource_filename(path)
mimetype = mimetypes.guess_type(filename)
assert mimetype is not None, path
b64 = base64.b64encode(data).decode('ascii')
return 'data:{};charset=utf-8;base64,{}'.format(mimetype[0], b64)
def render(template, **kwargs):
"""Render the given template and pass the given arguments to it."""
try:
@ -89,3 +101,4 @@ def render(template, **kwargs):
_env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)
_env.globals['resource_url'] = resource_url
_env.globals['file_url'] = urlutils.file_url
_env.globals['data_url'] = data_url

View File

@ -34,21 +34,42 @@ from qutebrowser.utils import utils, jinja
def patch_read_file(monkeypatch):
"""pytest fixture to patch utils.read_file."""
real_read_file = utils.read_file
real_resource_filename = utils.resource_filename
def _read_file(path):
def _read_file(path, binary=False):
"""A read_file which returns a simple template if the path is right."""
if path == os.path.join('html', 'test.html'):
assert not binary
return """Hello {{var}}"""
elif path == os.path.join('html', 'test2.html'):
assert not binary
return """{{ resource_url('utils/testfile') }}"""
elif path == os.path.join('html', 'test3.html'):
assert not binary
return """{{ data_url('testfile.txt') }}"""
elif path == 'testfile.txt':
assert binary
return b'foo'
elif path == os.path.join('html', 'undef.html'):
assert not binary
return """{{ does_not_exist() }}"""
elif path == os.path.join('html', 'undef_error.html'):
assert not binary
return real_read_file(path)
else:
raise IOError("Invalid path {}!".format(path))
def _resource_filename(path):
if path == 'utils/testfile':
return real_resource_filename(path)
elif path == 'testfile.txt':
return path
else:
raise IOError("Invalid path {}!".format(path))
monkeypatch.setattr('qutebrowser.utils.jinja.utils.read_file', _read_file)
monkeypatch.setattr('qutebrowser.utils.jinja.utils.resource_filename',
_resource_filename)
def test_simple_template():
@ -75,6 +96,15 @@ def test_resource_url():
assert f.read().splitlines()[0] == "Hello World!"
def test_data_url():
"""Test data_url() which can be used from templates."""
data = jinja.render('test3.html')
print(data)
url = QUrl(data)
assert url.isValid()
assert data == 'data:text/plain;charset=utf-8;base64,Zm9v' # 'foo'
def test_not_found():
"""Test with a template which does not exist."""
with pytest.raises(jinja2.TemplateNotFound) as excinfo: