From bc479fcafc3e73cb660577c6a4ef1480277bbd4f Mon Sep 17 00:00:00 2001 From: "Jie (Link) Zheng" Date: Tue, 19 Nov 2019 17:21:56 +0800 Subject: [PATCH] Fix py2/py3 compability issue for httpapi plugin fortios (#64982) --- lib/ansible/plugins/httpapi/fortios.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/ansible/plugins/httpapi/fortios.py b/lib/ansible/plugins/httpapi/fortios.py index 925367d780..3126fc2aad 100644 --- a/lib/ansible/plugins/httpapi/fortios.py +++ b/lib/ansible/plugins/httpapi/fortios.py @@ -84,12 +84,21 @@ class HttpApi(HttpApiBase): """ headers = {} - - for attr, val in response.getheaders(): - if attr == 'Set-Cookie' and 'APSCOOKIE_' in val: + resp_raw_headers = [] + if hasattr(response.headers, '_headers'): + resp_raw_headers = response.headers._headers + else: + resp_raw_headers = [(attr, response.headers[attr]) for attr in response.headers] + for attr, val in resp_raw_headers: + if attr.lower() == 'set-cookie' and 'APSCOOKIE_' in val: headers['Cookie'] = val + # XXX: In urllib2 all the 'set-cookie' headers are coalesced into one + x_ccsrftoken_position = val.find('ccsrftoken=') + if x_ccsrftoken_position != -1: + token_string = val[x_ccsrftoken_position + len('ccsrftoken='):].split('\"')[1] + self._ccsrftoken = token_string - elif attr == 'Set-Cookie' and 'ccsrftoken=' in val: + elif attr.lower() == 'set-cookie' and 'ccsrftoken=' in val: csrftoken_search = re.search('\"(.*)\"', val) if csrftoken_search: self._ccsrftoken = csrftoken_search.group(1) @@ -119,7 +128,11 @@ class HttpApi(HttpApiBase): try: response, response_data = self.connection.send(url, data, method=method) - - return response.status, to_text(response_data.getvalue()) + response_status = None + if hasattr(response, 'status'): + response_status = response.status + else: + response_status = response.headers.status + return response_status, to_text(response_data.getvalue()) except Exception as err: raise Exception(err)