From 02c47b1eade667d18fb0adef3293d86a6b5fd2e9 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Tue, 18 Sep 2018 20:47:09 -0400 Subject: [PATCH] avocado.utils.vmimage: add support for web page encoding The HTMLParser class on Python 3 expects to receive text, and urlopen returns bytes. To convert bytes into text, an encoding is needed. Most providers utf-8 encoding on the responses, but Fedora's servers use ISO-8859-1: HTTP/1.1 200 OK Date: Wed, 19 Sep 2018 00:46:43 GMT Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips ... Content-Type: text/html;charset=ISO-8859-1 ... We could get away with utf-8, but let's be thorough here. This fixes a "must be str, not bytes" error on Python 3. Signed-off-by: Cleber Rosa --- avocado/utils/vmimage.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/avocado/utils/vmimage.py b/avocado/utils/vmimage.py index fcee9274..6240d8d6 100644 --- a/avocado/utils/vmimage.py +++ b/avocado/utils/vmimage.py @@ -28,6 +28,7 @@ from six.moves.html_parser import HTMLParser from . import archive from . import asset +from . import astring from . import path as utils_path from . import process @@ -65,6 +66,8 @@ class ImageProviderBase(object): image. Intended to be sub-classed by the specific image providers. """ + HTML_ENCODING = 'utf-8' + def __init__(self, version, build, arch): self.url_versions = None @@ -87,7 +90,8 @@ class ImageProviderBase(object): pattern = '^%s/$' % self._version parser = VMImageHtmlParser(pattern) try: - parser.feed(urlopen(self.url_versions).read()) + data = urlopen(self.url_versions).read() + parser.feed(astring.to_text(data, self.HTML_ENCODING)) except HTTPError: raise ImageProviderError('Cannot open %s' % self.url_versions) if parser.items: @@ -124,7 +128,7 @@ class ImageProviderBase(object): parser = VMImageHtmlParser(image) try: content = urlopen(url_images).read() - parser.feed(content) + parser.feed(astring.to_text(content, self.HTML_ENCODING)) except HTTPError: raise ImageProviderError('Cannot open %s' % url_images) @@ -140,6 +144,8 @@ class FedoraImageProviderBase(ImageProviderBase): Base Fedora Image Provider """ + HTML_ENCODING = 'iso-8859-1' + def get_image_url(self): if int(self.version) >= 28: cloud = 'Cloud' -- GitLab