module_utils.urls - Encode the proxy connect as binary (#30811)

* module_utils.urls - Encode the proxy connect as binary

Under Python3 the sendall method expects binary not a string.

Prior to this change the below exception was being thrown;
Traceback (most recent call last):
  File "/tmp/ansible_umxox7_x/ansible_modlib.zip/ansible/module_utils/urls.py", line 1044, in fetch_url
    client_key=client_key, cookies=cookies)
  File "/tmp/ansible_umxox7_x/ansible_modlib.zip/ansible/module_utils/urls.py", line 951, in open_url
    r = urllib_request.urlopen(*urlopen_args)
  File "/opt/blue-python/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/opt/blue-python/3.6/lib/python3.6/urllib/request.py", line 524, in open
    req = meth(req)
  File "/tmp/ansible_umxox7_x/ansible_modlib.zip/ansible/module_utils/urls.py", line 729, in http_request
    s.sendall((self.CONNECT_COMMAND % (self.hostname, self.port)).decode())
AttributeError: 'str' object has no attribute 'decode'

Encoding the value is inline with the lines below (Proxy-Authorization etc) which are being sent as binary.
(cherry picked from commit 92f777e815)
This commit is contained in:
Damian Zaremba 2017-09-29 23:32:29 +02:00 committed by Toshio Kuratomi
parent 8cee2a8d93
commit 1407410362

View file

@ -726,7 +726,7 @@ class SSLValidationHandler(urllib_request.BaseHandler):
port = proxy_parts.get('port') or 443
s = socket.create_connection((proxy_parts.get('hostname'), port))
if proxy_parts.get('scheme') == 'http':
s.sendall(self.CONNECT_COMMAND % (self.hostname, self.port))
s.sendall(to_bytes(self.CONNECT_COMMAND % (self.hostname, self.port), errors='surrogate_or_strict'))
if proxy_parts.get('username'):
credentials = "%s:%s" % (proxy_parts.get('username', ''), proxy_parts.get('password', ''))
s.sendall(b'Proxy-Authorization: Basic %s\r\n' % base64.b64encode(to_bytes(credentials, errors='surrogate_or_strict')).strip())