2015-11-25 10:38:01 +03:00
|
|
|
|
# coding: utf-8
|
2012-03-22 02:19:27 +04:00
|
|
|
|
"""
|
|
|
|
|
weasyprint.utils
|
|
|
|
|
----------------
|
2011-06-30 00:34:01 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
Various utility functions and classes.
|
2011-08-16 17:11:35 +04:00
|
|
|
|
|
2014-01-10 18:27:02 +04:00
|
|
|
|
:copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
|
2012-03-22 02:19:27 +04:00
|
|
|
|
:license: BSD, see LICENSE for details.
|
2011-08-19 18:53:05 +04:00
|
|
|
|
|
|
|
|
|
"""
|
2011-06-30 00:34:01 +04:00
|
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
|
from __future__ import division, unicode_literals
|
|
|
|
|
|
2014-04-22 02:52:58 +04:00
|
|
|
|
import io
|
2012-05-23 16:43:02 +04:00
|
|
|
|
import re
|
2012-07-27 20:55:19 +04:00
|
|
|
|
import sys
|
2012-07-30 14:01:20 +04:00
|
|
|
|
import codecs
|
2012-05-23 16:43:02 +04:00
|
|
|
|
import os.path
|
2012-07-18 16:31:55 +04:00
|
|
|
|
import mimetypes
|
2013-06-20 15:58:24 +04:00
|
|
|
|
import contextlib
|
2014-04-22 02:52:58 +04:00
|
|
|
|
import gzip
|
|
|
|
|
import zlib
|
2014-04-27 14:00:02 +04:00
|
|
|
|
import traceback
|
2011-06-30 00:34:01 +04:00
|
|
|
|
|
2012-05-15 15:40:36 +04:00
|
|
|
|
from . import VERSION_STRING
|
2012-05-24 18:06:58 +04:00
|
|
|
|
from .logger import LOGGER
|
2012-02-17 21:49:58 +04:00
|
|
|
|
from .compat import (
|
2014-04-23 18:24:14 +04:00
|
|
|
|
urljoin, urlsplit, quote, unquote, unquote_to_bytes, urlopen,
|
|
|
|
|
urllib_get_content_type, urllib_get_charset, urllib_get_filename, Request,
|
2014-04-27 20:56:02 +04:00
|
|
|
|
parse_email, pathname2url, unicode, base64_decode, StreamingGzipFile)
|
2012-02-17 21:49:58 +04:00
|
|
|
|
|
|
|
|
|
|
2012-07-29 17:13:45 +04:00
|
|
|
|
# Unlinke HTML, CSS and PNG, the SVG MIME type is not always builtin
|
|
|
|
|
# in some Python version and therefore not reliable.
|
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
|
mimetypes.add_type('image/svg+xml', '.svg')
|
|
|
|
|
else:
|
|
|
|
|
# Native strings required.
|
|
|
|
|
mimetypes.add_type(b'image/svg+xml', b'.svg')
|
2012-07-29 00:38:44 +04:00
|
|
|
|
|
|
|
|
|
|
2012-07-30 14:01:20 +04:00
|
|
|
|
# getfilesystemencoding() on Linux is sometimes stupid...
|
|
|
|
|
FILESYSTEM_ENCODING = sys.getfilesystemencoding() or 'utf-8'
|
|
|
|
|
try:
|
|
|
|
|
if codecs.lookup(FILESYSTEM_ENCODING).name == 'ascii':
|
|
|
|
|
FILESYSTEM_ENCODING = 'utf-8'
|
|
|
|
|
except LookupError:
|
|
|
|
|
FILESYSTEM_ENCODING = 'utf-8'
|
|
|
|
|
|
|
|
|
|
|
2012-07-27 19:19:17 +04:00
|
|
|
|
# See http://stackoverflow.com/a/11687993/1162888
|
2012-06-02 10:04:24 +04:00
|
|
|
|
# Both are needed in Python 3 as the re module does not like to mix
|
2013-04-12 11:32:25 +04:00
|
|
|
|
# http://tools.ietf.org/html/rfc3986#section-3.1
|
|
|
|
|
UNICODE_SCHEME_RE = re.compile('^([a-zA-Z][a-zA-Z0-9.+-]+):')
|
|
|
|
|
BYTES_SCHEME_RE = re.compile(b'^([a-zA-Z][a-zA-Z0-9.+-]+):')
|
2012-06-02 10:04:24 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
|
2012-05-18 11:12:50 +04:00
|
|
|
|
def iri_to_uri(url):
|
2015-12-16 15:50:04 +03:00
|
|
|
|
"""Turn an IRI that can contain any Unicode character into an ASCII-only
|
2012-05-18 11:12:50 +04:00
|
|
|
|
URI that conforms to RFC 3986.
|
2012-05-15 15:40:36 +04:00
|
|
|
|
"""
|
2012-10-04 21:12:34 +04:00
|
|
|
|
if url.startswith('data:'):
|
|
|
|
|
# Data URIs can be huge, but don’t need this anyway.
|
|
|
|
|
return url
|
2012-07-27 20:55:19 +04:00
|
|
|
|
# Use UTF-8 as per RFC 3987 (IRI), except for file://
|
2012-07-30 14:01:20 +04:00
|
|
|
|
url = url.encode(FILESYSTEM_ENCODING
|
|
|
|
|
if url.startswith('file:') else 'utf-8')
|
2012-05-18 11:12:50 +04:00
|
|
|
|
# This is a full URI, not just a component. Only %-encode characters
|
|
|
|
|
# that are not allowed at all in URIs. Everthing else is "safe":
|
|
|
|
|
# * Reserved characters: /:?#[]@!$&'()*+,;=
|
|
|
|
|
# * Unreserved characters: ASCII letters, digits and -._~
|
|
|
|
|
# Of these, only '~' is not in urllib’s "always safe" list.
|
|
|
|
|
# * '%' to avoid double-encoding
|
2012-05-18 19:54:10 +04:00
|
|
|
|
return quote(url, safe=b"/:?#[]@!$&'()*+,;=~%")
|
2011-12-16 15:19:10 +04:00
|
|
|
|
|
|
|
|
|
|
2012-03-24 16:39:31 +04:00
|
|
|
|
def path2url(path):
|
|
|
|
|
"""Return file URL of `path`"""
|
2012-06-02 10:04:24 +04:00
|
|
|
|
path = os.path.abspath(path)
|
2012-08-02 19:19:34 +04:00
|
|
|
|
if os.path.isdir(path):
|
|
|
|
|
# Make sure directory names have a trailing slash.
|
|
|
|
|
# Otherwise relative URIs are resolved from the parent directory.
|
|
|
|
|
path += os.path.sep
|
2012-06-02 10:04:24 +04:00
|
|
|
|
if isinstance(path, unicode):
|
2012-07-30 14:01:20 +04:00
|
|
|
|
path = path.encode(FILESYSTEM_ENCODING)
|
2012-07-27 20:08:05 +04:00
|
|
|
|
path = pathname2url(path)
|
|
|
|
|
if path.startswith('///'):
|
|
|
|
|
# On Windows pathname2url(r'C:\foo') is apparently '///C:/foo'
|
|
|
|
|
# That enough slashes already.
|
|
|
|
|
return 'file:' + path
|
|
|
|
|
else:
|
|
|
|
|
return 'file://' + path
|
2012-03-24 16:39:31 +04:00
|
|
|
|
|
|
|
|
|
|
2012-05-21 20:43:08 +04:00
|
|
|
|
def url_is_absolute(url):
|
2012-06-02 10:04:24 +04:00
|
|
|
|
return bool(
|
|
|
|
|
(UNICODE_SCHEME_RE if isinstance(url, unicode) else BYTES_SCHEME_RE)
|
|
|
|
|
.match(url))
|
2012-05-21 20:43:08 +04:00
|
|
|
|
|
|
|
|
|
|
2012-11-06 16:55:39 +04:00
|
|
|
|
def element_base_url(element):
|
|
|
|
|
"""Return the URL associated with a lxml document.
|
|
|
|
|
|
|
|
|
|
This is the same as the HtmlElement.base_url property, but dont’t want
|
|
|
|
|
to require HtmlElement.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return element.getroottree().docinfo.URL
|
|
|
|
|
|
|
|
|
|
|
2012-05-24 18:06:58 +04:00
|
|
|
|
def get_url_attribute(element, attr_name):
|
|
|
|
|
"""Get the URI corresponding to the ``attr_name`` attribute.
|
2012-05-21 20:43:08 +04:00
|
|
|
|
|
|
|
|
|
Return ``None`` if:
|
|
|
|
|
|
|
|
|
|
* the attribute is empty or missing or,
|
|
|
|
|
* the value is a relative URI but the document has no base URI.
|
2011-08-19 18:53:05 +04:00
|
|
|
|
|
2012-05-21 20:43:08 +04:00
|
|
|
|
Otherwise, return an absolute URI.
|
2011-08-19 18:53:05 +04:00
|
|
|
|
|
2011-08-09 14:45:51 +04:00
|
|
|
|
"""
|
2012-09-25 18:01:12 +04:00
|
|
|
|
value = element.get(attr_name, '').strip()
|
|
|
|
|
if value:
|
2012-11-06 16:55:39 +04:00
|
|
|
|
return url_join(element_base_url(element), value,
|
|
|
|
|
'<%s %s="%s"> at line %s', element.tag, attr_name,
|
|
|
|
|
value, element.sourceline)
|
2012-09-25 18:01:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def url_join(base_url, url, context, *args):
|
|
|
|
|
"""Like urllib.urljoin, but issue a warning and return None if base_url
|
|
|
|
|
is required but missing.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if url_is_absolute(url):
|
2012-10-04 21:12:34 +04:00
|
|
|
|
return iri_to_uri(url)
|
2012-09-25 18:01:12 +04:00
|
|
|
|
elif base_url:
|
2012-10-04 21:12:34 +04:00
|
|
|
|
return iri_to_uri(urljoin(base_url, url))
|
2012-09-25 18:01:12 +04:00
|
|
|
|
else:
|
2013-08-19 16:38:09 +04:00
|
|
|
|
LOGGER.warning('Relative URI reference without a base URI: ' + context,
|
2014-04-27 15:29:55 +04:00
|
|
|
|
*args)
|
2012-09-25 18:01:12 +04:00
|
|
|
|
return None
|
2011-08-09 14:45:51 +04:00
|
|
|
|
|
2011-08-05 13:16:44 +04:00
|
|
|
|
|
2012-05-30 22:06:44 +04:00
|
|
|
|
def get_link_attribute(element, attr_name):
|
|
|
|
|
"""Return ('external', absolute_uri) or
|
|
|
|
|
('internal', unquoted_fragment_id) or None.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
attr_value = element.get(attr_name, '').strip()
|
2013-03-13 15:12:12 +04:00
|
|
|
|
if attr_value.startswith('#') and len(attr_value) > 1:
|
2012-05-30 22:06:44 +04:00
|
|
|
|
# Do not require a base_url when the value is just a fragment.
|
|
|
|
|
return 'internal', unquote(attr_value[1:])
|
2013-03-13 15:12:12 +04:00
|
|
|
|
uri = get_url_attribute(element, attr_name)
|
|
|
|
|
if uri:
|
2012-11-06 16:55:39 +04:00
|
|
|
|
document_url = element_base_url(element)
|
2013-03-13 15:12:12 +04:00
|
|
|
|
if document_url:
|
2012-05-30 22:06:44 +04:00
|
|
|
|
parsed = urlsplit(uri)
|
|
|
|
|
# Compare with fragments removed
|
2012-11-06 16:55:39 +04:00
|
|
|
|
if parsed[:-1] == urlsplit(document_url)[:-1]:
|
2012-05-30 22:06:44 +04:00
|
|
|
|
return 'internal', unquote(parsed.fragment)
|
2013-03-13 15:12:12 +04:00
|
|
|
|
return 'external', uri
|
2012-05-30 22:06:44 +04:00
|
|
|
|
|
|
|
|
|
|
2011-08-19 18:53:05 +04:00
|
|
|
|
def ensure_url(string):
|
|
|
|
|
"""Get a ``scheme://path`` URL from ``string``.
|
|
|
|
|
|
|
|
|
|
If ``string`` looks like an URL, return it unchanged. Otherwise assume a
|
|
|
|
|
filename and convert it to a ``file://`` URL.
|
|
|
|
|
|
2011-08-09 14:45:51 +04:00
|
|
|
|
"""
|
2012-07-27 20:55:19 +04:00
|
|
|
|
return string if url_is_absolute(string) else path2url(string)
|
2011-08-16 17:11:35 +04:00
|
|
|
|
|
|
|
|
|
|
2012-07-29 20:38:59 +04:00
|
|
|
|
def safe_base64_decode(data):
|
2012-03-21 19:07:49 +04:00
|
|
|
|
"""Decode base64, padding being optional.
|
|
|
|
|
|
|
|
|
|
"From a theoretical point of view, the padding character is not needed,
|
|
|
|
|
since the number of missing bytes can be calculated from the number
|
|
|
|
|
of Base64 digits."
|
|
|
|
|
|
|
|
|
|
https://en.wikipedia.org/wiki/Base64#Padding
|
|
|
|
|
|
|
|
|
|
:param data: Base64 data as an ASCII byte string
|
|
|
|
|
:returns: The decoded byte string.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
missing_padding = 4 - len(data) % 4
|
|
|
|
|
if missing_padding:
|
2012-11-06 16:55:39 +04:00
|
|
|
|
data += b'=' * missing_padding
|
2012-07-29 20:38:59 +04:00
|
|
|
|
return base64_decode(data)
|
2012-03-21 19:07:49 +04:00
|
|
|
|
|
|
|
|
|
|
2012-05-23 16:43:02 +04:00
|
|
|
|
def open_data_url(url):
|
2012-05-30 22:06:44 +04:00
|
|
|
|
"""Decode URLs with the 'data' scheme. urllib can handle them
|
2012-02-17 21:49:58 +04:00
|
|
|
|
in Python 2, but that is broken in Python 3.
|
|
|
|
|
|
2012-05-30 22:06:44 +04:00
|
|
|
|
Inspired from Python 2.7.2’s urllib.py.
|
2012-02-17 21:49:58 +04:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# syntax of data URLs:
|
|
|
|
|
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
|
|
|
|
|
# mediatype := [ type "/" subtype ] *( ";" parameter )
|
|
|
|
|
# data := *urlchar
|
|
|
|
|
# parameter := attribute "=" value
|
|
|
|
|
try:
|
|
|
|
|
header, data = url.split(',', 1)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise IOError('bad data URL')
|
|
|
|
|
header = header[5:] # len('data:') == 5
|
|
|
|
|
if header:
|
|
|
|
|
semi = header.rfind(';')
|
|
|
|
|
if semi >= 0 and '=' not in header[semi:]:
|
|
|
|
|
content_type = header[:semi]
|
2012-11-06 16:55:39 +04:00
|
|
|
|
encoding = header[semi + 1:]
|
2012-02-17 21:49:58 +04:00
|
|
|
|
else:
|
|
|
|
|
content_type = header
|
|
|
|
|
encoding = ''
|
|
|
|
|
message = parse_email('Content-type: ' + content_type)
|
|
|
|
|
mime_type = message.get_content_type()
|
|
|
|
|
charset = message.get_content_charset()
|
|
|
|
|
else:
|
|
|
|
|
mime_type = 'text/plain'
|
|
|
|
|
charset = 'US-ASCII'
|
|
|
|
|
encoding = ''
|
|
|
|
|
|
2012-03-21 19:07:49 +04:00
|
|
|
|
data = unquote_to_bytes(data)
|
2012-02-17 21:49:58 +04:00
|
|
|
|
if encoding == 'base64':
|
2012-07-29 20:38:59 +04:00
|
|
|
|
data = safe_base64_decode(data)
|
2012-02-17 21:49:58 +04:00
|
|
|
|
|
2012-07-18 16:31:55 +04:00
|
|
|
|
return dict(string=data, mime_type=mime_type, encoding=charset,
|
|
|
|
|
redirected_url=url)
|
2011-08-19 18:53:05 +04:00
|
|
|
|
|
|
|
|
|
|
2014-04-22 02:52:58 +04:00
|
|
|
|
HTTP_HEADERS = {
|
|
|
|
|
'User-Agent': VERSION_STRING,
|
|
|
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-27 15:29:55 +04:00
|
|
|
|
|
2012-07-13 14:24:55 +04:00
|
|
|
|
def default_url_fetcher(url):
|
2012-09-19 19:37:52 +04:00
|
|
|
|
"""Fetch an external resource such as an image or stylesheet.
|
|
|
|
|
|
2012-10-08 21:51:18 +04:00
|
|
|
|
Another callable with the same signature can be given as the
|
|
|
|
|
:obj:`url_fetcher` argument to :class:`HTML` or :class:`CSS`.
|
|
|
|
|
(See :ref:`url-fetchers`.)
|
|
|
|
|
|
|
|
|
|
:type url: Unicode string
|
2012-09-19 19:37:52 +04:00
|
|
|
|
:param url: The URL of the resource to fetch
|
|
|
|
|
:raises: any exception to indicate failure. Failures are logged
|
|
|
|
|
as warnings, with the string representation of the exception
|
|
|
|
|
in the message.
|
|
|
|
|
:returns: In case of success, a dict with the following keys:
|
|
|
|
|
|
2012-11-06 16:55:39 +04:00
|
|
|
|
* One of ``string`` (a byte string) or ``file_obj``
|
|
|
|
|
(a file-like object)
|
2012-09-19 19:37:52 +04:00
|
|
|
|
* Optionally: ``mime_type``, a MIME type extracted eg. from a
|
|
|
|
|
*Content-Type* header. If not provided, the type is guessed from the
|
|
|
|
|
file extension in the URL.
|
|
|
|
|
* Optionally: ``encoding``, a character encoding extracted eg. from a
|
|
|
|
|
*charset* parameter in a *Content-Type* header
|
2015-07-01 02:01:43 +03:00
|
|
|
|
* Optionally: ``redirected_url``, the actual URL of the resource
|
2012-11-06 16:55:39 +04:00
|
|
|
|
in case there were eg. HTTP redirects.
|
2014-04-18 18:39:54 +04:00
|
|
|
|
* Optionally: ``filename``, the filename of the resource. Usually
|
|
|
|
|
derived from the *filename* parameter in a *Content-Disposition*
|
|
|
|
|
header
|
2012-09-19 19:37:52 +04:00
|
|
|
|
|
2012-11-06 16:55:39 +04:00
|
|
|
|
If a ``file_obj`` key is given, it is the caller’s responsability
|
|
|
|
|
to call ``file_obj.close()``.
|
2011-12-08 19:31:03 +04:00
|
|
|
|
|
2011-08-16 17:11:35 +04:00
|
|
|
|
"""
|
2013-03-28 16:33:28 +04:00
|
|
|
|
if url.lower().startswith('data:'):
|
2012-07-13 14:24:55 +04:00
|
|
|
|
return open_data_url(url)
|
|
|
|
|
elif UNICODE_SCHEME_RE.match(url):
|
2012-05-18 11:12:50 +04:00
|
|
|
|
url = iri_to_uri(url)
|
2014-04-27 20:56:02 +04:00
|
|
|
|
response = urlopen(Request(url, headers=HTTP_HEADERS))
|
2014-04-22 02:52:58 +04:00
|
|
|
|
result = dict(redirected_url=response.geturl(),
|
2014-04-27 20:56:02 +04:00
|
|
|
|
mime_type=urllib_get_content_type(response),
|
|
|
|
|
encoding=urllib_get_charset(response),
|
|
|
|
|
filename=urllib_get_filename(response))
|
2014-04-22 02:52:58 +04:00
|
|
|
|
content_encoding = response.info().get('Content-Encoding')
|
|
|
|
|
if content_encoding == 'gzip':
|
|
|
|
|
if StreamingGzipFile is None:
|
|
|
|
|
result['string'] = gzip.GzipFile(
|
|
|
|
|
fileobj=io.BytesIO(response.read())).read()
|
|
|
|
|
response.close()
|
|
|
|
|
else:
|
|
|
|
|
result['file_obj'] = StreamingGzipFile(fileobj=response)
|
|
|
|
|
elif content_encoding == 'deflate':
|
|
|
|
|
data = response.read()
|
|
|
|
|
try:
|
|
|
|
|
result['string'] = zlib.decompress(data)
|
|
|
|
|
except zlib.error:
|
|
|
|
|
# Try without zlib header or checksum
|
|
|
|
|
result['string'] = zlib.decompress(data, -15)
|
|
|
|
|
else:
|
|
|
|
|
result['file_obj'] = response
|
|
|
|
|
return result
|
2012-07-13 14:24:55 +04:00
|
|
|
|
else:
|
|
|
|
|
raise ValueError('Not an absolute URI: %r' % url)
|
2012-07-18 16:31:55 +04:00
|
|
|
|
|
|
|
|
|
|
2013-06-21 00:32:28 +04:00
|
|
|
|
class URLFetchingError(IOError):
|
|
|
|
|
"""Some error happened when fetching an URL."""
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 15:58:24 +04:00
|
|
|
|
@contextlib.contextmanager
|
2013-06-20 15:17:03 +04:00
|
|
|
|
def fetch(url_fetcher, url):
|
2013-06-20 15:58:24 +04:00
|
|
|
|
"""Call an url_fetcher, fill in optional data, and clean up."""
|
2013-06-21 00:32:28 +04:00
|
|
|
|
try:
|
|
|
|
|
result = url_fetcher(url)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
name = type(exc).__name__
|
|
|
|
|
value = str(exc)
|
|
|
|
|
raise URLFetchingError('%s: %s' % (name, value) if value else name)
|
2013-06-20 15:17:03 +04:00
|
|
|
|
result.setdefault('redirected_url', url)
|
2013-06-20 15:26:12 +04:00
|
|
|
|
result.setdefault('mime_type', None)
|
2013-07-14 12:17:40 +04:00
|
|
|
|
if 'file_obj' in result:
|
2013-06-20 15:58:24 +04:00
|
|
|
|
try:
|
|
|
|
|
yield result
|
|
|
|
|
finally:
|
|
|
|
|
try:
|
|
|
|
|
result['file_obj'].close()
|
2014-04-27 14:00:02 +04:00
|
|
|
|
except Exception:
|
2013-06-20 15:58:24 +04:00
|
|
|
|
# May already be closed or something.
|
2014-04-27 14:00:02 +04:00
|
|
|
|
# This is just cleanup anyway: log but make it non-fatal.
|
|
|
|
|
LOGGER.warning('Error when closing stream for %s:\n%s',
|
|
|
|
|
url, traceback.format_exc())
|
2013-06-20 15:58:24 +04:00
|
|
|
|
else:
|
|
|
|
|
yield result
|