summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2016-10-24 18:43:16 -0700
committerToshio Kuratomi <a.badger@gmail.com>2016-10-24 18:43:16 -0700
commit3290d3ec7bdd93f1126bcb490c218d9cd265af31 (patch)
treeac36145ffd1c68564e1126356d80b6f30ea513ae
parent8bb01d4c29e4abd5f628dbc15ab8d5cda90cdad2 (diff)
downloadansible-py3-bytes-urls-fix.tar.gz
A few fixes for python3py3-bytes-urls-fix
* socket interfaces take bytes so convert text strings to bytes when using them. * Use b64encode() instead of str.encode('base64')
-rw-r--r--lib/ansible/module_utils/urls.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py
index 3df5ce0341..bef950f05f 100644
--- a/lib/ansible/module_utils/urls.py
+++ b/lib/ansible/module_utils/urls.py
@@ -105,8 +105,6 @@ import platform
import tempfile
import base64
-from ansible.module_utils.basic import get_distribution, get_exception
-
try:
import httplib
except ImportError:
@@ -115,7 +113,9 @@ except ImportError:
import ansible.module_utils.six.moves.urllib.request as urllib_request
import ansible.module_utils.six.moves.urllib.error as urllib_error
+from ansible.module_utils.basic import get_distribution, get_exception
from ansible.module_utils.six import b
+from ansible.module_utils._text import to_bytes
try:
# python3
@@ -672,10 +672,10 @@ class SSLValidationHandler(urllib_request.BaseHandler):
s.sendall(self.CONNECT_COMMAND % (self.hostname, self.port))
if proxy_parts.get('username'):
credentials = "%s:%s" % (proxy_parts.get('username',''), proxy_parts.get('password',''))
- s.sendall('Proxy-Authorization: Basic %s\r\n' % credentials.encode('base64').strip())
- s.sendall('\r\n')
- connect_result = ""
- while connect_result.find("\r\n\r\n") <= 0:
+ s.sendall(b('Proxy-Authorization: Basic %s\r\n') % base64.b64encode(to_bytes(credentials, errors='surrogate_or_strict')).strip())
+ s.sendall(b('\r\n'))
+ connect_result = b("")
+ while connect_result.find(b("\r\n\r\n")) <= 0:
connect_result += s.recv(4096)
# 128 kilobytes of headers should be enough for everyone.
if len(connect_result) > 131072:
@@ -884,7 +884,10 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True,
def basic_auth_header(username, password):
- return "Basic %s" % base64.b64encode("%s:%s" % (username, password))
+ """Takes a username and password and returns a byte string suitable for
+ using as value of an Authorization header to do basic auth.
+ """
+ return b("Basic %s") % base64.b64encode(to_bytes("%s:%s" % (username, password), errors='surrogate_or_strict'))
def url_argument_spec():