提交 02c47b1e 编写于 作者: C Cleber Rosa

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: NCleber Rosa <crosa@redhat.com>
上级 61846e0c
......@@ -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'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册