qutebrowser/qutebrowser/browser/qutescheme.py

539 lines
16 KiB
Python
Raw Normal View History

2016-09-14 10:18:25 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2018-02-05 12:19:50 +01:00
# Copyright 2016-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2016-09-14 10:18:25 +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/>.
"""Backend-independent qute://* code.
2016-09-14 10:18:25 +02:00
Module attributes:
pyeval_output: The output of the last :pyeval command.
_HANDLERS: The handlers registered via decorators.
"""
import html
2017-02-26 13:07:30 +01:00
import json
2017-03-04 15:37:48 +01:00
import os
import time
import textwrap
import mimetypes
2017-11-24 17:15:26 +01:00
import urllib
import collections
import base64
try:
import secrets
except ImportError:
# New in Python 3.6
secrets = None
2016-09-14 10:18:25 +02:00
import pkg_resources
from PyQt5.QtCore import QUrlQuery, QUrl
2016-09-14 10:18:25 +02:00
import qutebrowser
2018-09-05 22:51:28 +02:00
from qutebrowser.browser import pdfjs
2017-09-15 18:26:33 +02:00
from qutebrowser.config import config, configdata, configexc, configdiff
2016-09-14 10:18:25 +02:00
from qutebrowser.utils import (version, utils, jinja, log, message, docutils,
2018-01-12 23:35:04 +01:00
objreg, urlutils)
from qutebrowser.misc import objects
from qutebrowser.qt import sip
2016-09-14 10:18:25 +02:00
pyeval_output = ":pyeval was never called"
spawn_output = ":spawn was never called"
csrf_token = None
2016-09-14 10:18:25 +02:00
_HANDLERS = {}
2018-07-10 00:55:29 +02:00
class Error(Exception):
2016-09-14 10:18:25 +02:00
2018-07-10 00:55:29 +02:00
"""Exception for generic errors on a qute:// page."""
2016-09-14 10:18:25 +02:00
pass
2018-07-10 00:55:29 +02:00
class NotFoundError(Error):
2016-09-14 10:18:25 +02:00
2018-07-10 00:55:29 +02:00
"""Raised when the given URL was not found."""
2016-09-14 10:18:25 +02:00
pass
2018-07-10 00:55:29 +02:00
class SchemeOSError(Error):
2016-09-14 11:04:37 +02:00
2018-07-10 00:55:29 +02:00
"""Raised when there was an OSError inside a handler."""
2016-09-14 11:04:37 +02:00
2018-07-10 00:55:29 +02:00
pass
2016-09-14 11:04:37 +02:00
2018-07-10 00:55:29 +02:00
class UrlInvalidError(Error):
"""Raised when an invalid URL was opened."""
pass
class RequestDeniedError(Error):
"""Raised when the request is forbidden."""
pass
2016-09-14 11:04:37 +02:00
class Redirect(Exception):
"""Exception to signal a redirect should happen.
Attributes:
url: The URL to redirect to, as a QUrl.
"""
def __init__(self, url):
super().__init__(url.toDisplayString())
self.url = url
class add_handler: # noqa: N801,N806 pylint: disable=invalid-name
"""Decorator to register a qute://* URL handler.
Attributes:
_name: The 'foo' part of qute://foo
"""
2018-09-05 22:51:58 +02:00
def __init__(self, name):
self._name = name
self._function = None
def __call__(self, function):
self._function = function
_HANDLERS[self._name] = self.wrapper
2016-09-14 10:18:25 +02:00
return function
def wrapper(self, *args, **kwargs):
2017-12-15 13:55:06 +01:00
"""Call the underlying function."""
2018-09-05 22:51:58 +02:00
return self._function(*args, **kwargs)
2016-09-14 10:18:25 +02:00
def data_for_url(url):
"""Get the data to show for the given URL.
Args:
url: The QUrl to show.
Return:
A (mimetype, data) tuple.
"""
norm_url = url.adjusted(QUrl.NormalizePathSegments |
QUrl.StripTrailingSlash)
if norm_url != url:
raise Redirect(norm_url)
2016-09-14 10:18:25 +02:00
path = url.path()
host = url.host()
query = urlutils.query_string(url)
2016-09-14 10:18:25 +02:00
# A url like "qute:foo" is split as "scheme:path", not "scheme:host".
log.misc.debug("url: {}, path: {}, host {}".format(
url.toDisplayString(), path, host))
2017-10-30 12:24:45 +01:00
if not path or not host:
new_url = QUrl()
new_url.setScheme('qute')
2017-10-30 12:24:45 +01:00
# When path is absent, e.g. qute://help (with no trailing slash)
if host:
new_url.setHost(host)
# When host is absent, e.g. qute:help
else:
new_url.setHost(path)
new_url.setPath('/')
if query:
new_url.setQuery(query)
if new_url.host(): # path was a valid host
raise Redirect(new_url)
2016-09-14 10:18:25 +02:00
try:
handler = _HANDLERS[host]
2016-09-14 10:18:25 +02:00
except KeyError:
2018-07-10 00:55:29 +02:00
raise NotFoundError("No handler found for {}".format(
url.toDisplayString()))
2016-09-14 10:18:25 +02:00
try:
mimetype, data = handler(url)
2016-09-14 10:18:25 +02:00
except OSError as e:
2018-07-10 00:55:29 +02:00
raise SchemeOSError(e)
assert mimetype is not None, url
if mimetype == 'text/html' and isinstance(data, str):
# We let handlers return HTML as text
data = data.encode('utf-8', errors='xmlcharrefreplace')
2016-09-14 10:18:25 +02:00
return mimetype, data
@add_handler('bookmarks')
def qute_bookmarks(_url):
"""Handler for qute://bookmarks. Display all quickmarks / bookmarks."""
2016-09-14 10:18:25 +02:00
bookmarks = sorted(objreg.get('bookmark-manager').marks.items(),
key=lambda x: x[1]) # Sort by title
quickmarks = sorted(objreg.get('quickmark-manager').marks.items(),
key=lambda x: x[0]) # Sort by name
2018-06-21 22:28:27 +02:00
src = jinja.render('bookmarks.html',
title='Bookmarks',
bookmarks=bookmarks,
quickmarks=quickmarks)
return 'text/html', src
2016-09-14 10:18:25 +02:00
2017-12-31 14:38:20 +01:00
@add_handler('tabs')
def qute_tabs(_url):
"""Handler for qute://tabs. Display information about all open tabs."""
tabs = collections.defaultdict(list)
2018-01-22 16:11:59 +01:00
for win_id, window in objreg.window_registry.items():
if sip.isdeleted(window):
continue
2017-12-31 14:38:20 +01:00
tabbed_browser = objreg.get('tabbed-browser',
scope='window',
window=win_id)
for tab in tabbed_browser.widgets():
if tab.url() not in [QUrl("qute://tabs/"), QUrl("qute://tabs")]:
urlstr = tab.url().toDisplayString()
tabs[str(win_id)].append((tab.title(), urlstr))
2017-12-31 14:38:20 +01:00
2018-06-21 22:28:27 +02:00
src = jinja.render('tabs.html',
title='Tabs',
tab_list_by_window=tabs)
return 'text/html', src
2017-12-31 14:38:20 +01:00
def history_data(start_time, offset=None):
"""Return history data.
2017-03-28 15:34:47 +02:00
Arguments:
start_time: select history starting from this timestamp.
offset: number of items to skip
2017-03-28 15:34:47 +02:00
"""
# history atimes are stored as ints, ensure start_time is not a float
start_time = int(start_time)
hist = objreg.get('web-history')
if offset is not None:
entries = hist.entries_before(start_time, limit=1000, offset=offset)
else:
2017-03-28 15:34:47 +02:00
# end is 24hrs earlier than start
end_time = start_time - 24*60*60
entries = hist.entries_between(end_time, start_time)
2017-03-28 15:34:47 +02:00
return [{"url": e.url,
"title": html.escape(e.title) or html.escape(e.url),
"time": e.atime} for e in entries]
2017-03-28 15:34:47 +02:00
@add_handler('history')
def qute_history(url):
"""Handler for qute://history. Display and serve history."""
2017-02-26 16:13:05 +01:00
if url.path() == '/data':
try:
offset = QUrlQuery(url).queryItemValue("offset")
offset = int(offset) if offset else None
2018-09-04 23:21:51 +02:00
except ValueError:
2018-07-10 00:55:29 +02:00
raise UrlInvalidError("Query parameter offset is invalid")
2017-02-26 13:07:30 +01:00
# Use start_time in query or current time.
try:
start_time = QUrlQuery(url).queryItemValue("start_time")
start_time = float(start_time) if start_time else time.time()
2018-09-04 23:21:51 +02:00
except ValueError:
2018-07-10 00:55:29 +02:00
raise UrlInvalidError("Query parameter start_time is invalid")
2017-02-06 20:04:32 +01:00
return 'text/html', json.dumps(history_data(start_time, offset))
else:
2017-09-18 21:15:14 +02:00
return 'text/html', jinja.render(
'history.html',
title='History',
gap_interval=config.val.history_gap_interval
)
2017-02-06 20:04:32 +01:00
2017-02-27 18:37:24 +01:00
@add_handler('javascript')
def qute_javascript(url):
"""Handler for qute://javascript.
2017-02-27 18:37:24 +01:00
Return content of file given as query parameter.
"""
path = url.path()
if path:
2017-03-04 15:37:48 +01:00
path = "javascript" + os.sep.join(path.split('/'))
return 'text/html', utils.read_file(path, binary=False)
2017-02-27 18:37:24 +01:00
else:
2018-07-10 00:55:29 +02:00
raise UrlInvalidError("No file specified")
2017-02-27 18:37:24 +01:00
2016-09-14 10:18:25 +02:00
@add_handler('pyeval')
def qute_pyeval(_url):
"""Handler for qute://pyeval."""
2018-06-21 22:28:27 +02:00
src = jinja.render('pre.html', title='pyeval', content=pyeval_output)
return 'text/html', src
2016-09-14 10:18:25 +02:00
@add_handler('spawn-output')
def qute_spawn_output(_url):
"""Handler for qute://spawn-output."""
2018-06-21 22:28:27 +02:00
src = jinja.render('pre.html', title='spawn output', content=spawn_output)
return 'text/html', src
2016-09-14 10:18:25 +02:00
@add_handler('version')
@add_handler('verizon')
def qute_version(_url):
"""Handler for qute://version."""
2018-06-21 22:28:27 +02:00
src = jinja.render('version.html', title='Version info',
version=version.version(),
copyright=qutebrowser.__copyright__)
return 'text/html', src
2016-09-14 10:18:25 +02:00
@add_handler('plainlog')
def qute_plainlog(url):
"""Handler for qute://plainlog.
2016-09-14 10:18:25 +02:00
An optional query parameter specifies the minimum log level to print.
For example, qute://log?level=warning prints warnings and errors.
Level can be one of: vdebug, debug, info, warning, error, critical.
"""
if log.ram_handler is None:
text = "Log output was disabled."
else:
level = QUrlQuery(url).queryItemValue('level')
if not level:
2016-09-14 10:18:25 +02:00
level = 'vdebug'
text = log.ram_handler.dump_log(html=False, level=level)
2018-06-21 22:28:27 +02:00
src = jinja.render('pre.html', title='log', content=text)
return 'text/html', src
2016-09-14 10:18:25 +02:00
@add_handler('log')
def qute_log(url):
"""Handler for qute://log.
2016-09-14 10:18:25 +02:00
An optional query parameter specifies the minimum log level to print.
For example, qute://log?level=warning prints warnings and errors.
Level can be one of: vdebug, debug, info, warning, error, critical.
"""
if log.ram_handler is None:
html_log = None
else:
level = QUrlQuery(url).queryItemValue('level')
if not level:
2016-09-14 10:18:25 +02:00
level = 'vdebug'
html_log = log.ram_handler.dump_log(html=True, level=level)
2018-06-21 22:28:27 +02:00
src = jinja.render('log.html', title='log', content=html_log)
return 'text/html', src
2016-09-14 10:18:25 +02:00
@add_handler('gpl')
def qute_gpl(_url):
"""Handler for qute://gpl. Return HTML content as string."""
2017-11-06 12:13:54 +01:00
return 'text/html', utils.read_file('html/license.html')
2016-09-14 10:18:25 +02:00
@add_handler('help')
def qute_help(url):
"""Handler for qute://help."""
2016-09-14 10:18:25 +02:00
urlpath = url.path()
if not urlpath or urlpath == '/':
urlpath = 'index.html'
else:
urlpath = urlpath.lstrip('/')
if not docutils.docs_up_to_date(urlpath):
2016-09-14 20:52:32 +02:00
message.error("Your documentation is outdated! Please re-run "
"scripts/asciidoc2html.py.")
2016-09-14 10:18:25 +02:00
path = 'html/doc/{}'.format(urlpath)
if not urlpath.endswith('.html'):
try:
bdata = utils.read_file(path, binary=True)
except OSError as e:
2018-07-10 00:55:29 +02:00
raise SchemeOSError(e)
mimetype, _encoding = mimetypes.guess_type(urlpath)
assert mimetype is not None, url
return mimetype, bdata
try:
data = utils.read_file(path)
except OSError:
# No .html around, let's see if we find the asciidoc
asciidoc_path = path.replace('.html', '.asciidoc')
if asciidoc_path.startswith('html/doc/'):
asciidoc_path = asciidoc_path.replace('html/doc/', '../doc/help/')
try:
asciidoc = utils.read_file(asciidoc_path)
except OSError:
asciidoc = None
if asciidoc is None:
raise
preamble = textwrap.dedent("""
There was an error loading the documentation!
This most likely means the documentation was not generated
properly. If you are running qutebrowser from the git repository,
please (re)run scripts/asciidoc2html.py and reload this page.
If you're running a released version this is a bug, please use
:report to report it.
Falling back to the plaintext version.
---------------------------------------------------------------
""")
return 'text/plain', (preamble + asciidoc).encode('utf-8')
else:
return 'text/html', data
@add_handler('backend-warning')
2017-05-30 17:07:31 +02:00
def qute_backend_warning(_url):
"""Handler for qute://backend-warning."""
2018-06-21 22:28:27 +02:00
src = jinja.render('backend-warning.html',
distribution=version.distribution(),
Distribution=version.Distribution,
version=pkg_resources.parse_version,
title="Legacy backend warning")
return 'text/html', src
def _qute_settings_set(url):
"""Handler for qute://settings/set."""
query = QUrlQuery(url)
option = query.queryItemValue('option', QUrl.FullyDecoded)
value = query.queryItemValue('value', QUrl.FullyDecoded)
# https://github.com/qutebrowser/qutebrowser/issues/727
if option == 'content.javascript.enabled' and value == 'false':
msg = ("Refusing to disable javascript via qute://settings "
"as it needs javascript support.")
message.error(msg)
return 'text/html', b'error: ' + msg.encode('utf-8')
try:
config.instance.set_str(option, value, save_yaml=True)
return 'text/html', b'ok'
except configexc.Error as e:
message.error(str(e))
return 'text/html', b'error: ' + str(e).encode('utf-8')
@add_handler('settings')
def qute_settings(url):
"""Handler for qute://settings. View/change qute configuration."""
global csrf_token
if url.path() == '/set':
if url.password() != csrf_token:
message.error("Invalid CSRF token for qute://settings!")
2018-07-10 00:55:29 +02:00
raise RequestDeniedError("Invalid CSRF token!")
return _qute_settings_set(url)
# Requests to qute://settings/set should only be allowed from
# qute://settings. As an additional security precaution, we generate a CSRF
# token to use here.
if secrets:
csrf_token = secrets.token_urlsafe()
else:
# On Python < 3.6, from secrets.py
token = base64.urlsafe_b64encode(os.urandom(32))
csrf_token = token.rstrip(b'=').decode('ascii')
2018-06-21 22:28:27 +02:00
src = jinja.render('settings.html', title='settings',
configdata=configdata,
confget=config.instance.get_str,
csrf_token=csrf_token)
2018-06-21 22:28:27 +02:00
return 'text/html', src
2018-01-12 23:24:20 +01:00
@add_handler('bindings')
def qute_bindings(_url):
"""Handler for qute://bindings. View keybindings."""
2018-01-12 23:24:20 +01:00
bindings = {}
defaults = config.val.bindings.default
modes = set(defaults.keys()).union(config.val.bindings.commands)
2018-01-17 20:25:07 +01:00
modes.remove('normal')
modes = ['normal'] + sorted(list(modes))
for mode in modes:
2018-01-12 23:24:20 +01:00
bindings[mode] = config.key_instance.get_bindings_for(mode)
2018-06-21 22:28:27 +02:00
src = jinja.render('bindings.html', title='Bindings',
bindings=bindings)
return 'text/html', src
2018-01-12 23:24:20 +01:00
2017-11-18 00:31:53 +01:00
@add_handler('back')
def qute_back(url):
"""Handler for qute://back.
2017-11-18 00:31:53 +01:00
Simple page to free ram / lazy load a site, goes back on focusing the tab.
"""
2018-06-21 22:28:27 +02:00
src = jinja.render(
2017-11-30 16:05:01 +01:00
'back.html',
title='Suspended: ' + urllib.parse.unquote(url.fragment()))
2018-06-21 22:28:27 +02:00
return 'text/html', src
2017-09-15 18:26:33 +02:00
2017-11-18 00:48:31 +01:00
2017-09-15 18:26:33 +02:00
@add_handler('configdiff')
def qute_configdiff(url):
2017-09-15 18:26:33 +02:00
"""Handler for qute://configdiff."""
if url.path() == '/old':
try:
return 'text/html', configdiff.get_diff()
except OSError as e:
error = (b'Failed to read old config: ' +
str(e.strerror).encode('utf-8'))
return 'text/plain', error
else:
data = config.instance.dump_userconfig().encode('utf-8')
return 'text/plain', data
@add_handler('pastebin-version')
2018-02-06 20:48:31 +01:00
def qute_pastebin_version(_url):
"""Handler that pastebins the version string."""
version.pastebin_version()
return 'text/plain', b'Paste called.'
2018-09-05 22:51:28 +02:00
@add_handler('pdfjs')
def qute_pdfjs(url):
"""Handler for qute://pdfjs. Return the pdf.js viewer."""
try:
data = pdfjs.get_pdfjs_res(url.path())
except pdfjs.PDFJSNotFound as e:
# Logging as the error might get lost otherwise since we're not showing
# the error page if a single asset is missing. This way we don't lose
# information, as the failed pdfjs requests are still in the log.
log.misc.warning(
"pdfjs resource requested but not found: {}".format(e.path))
raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path))
else:
mimetype, _encoding = mimetypes.guess_type(url.fileName())
assert mimetype is not None, url
return mimetype, data