Fix py2/py3 compability issue for httpapi plugin fortios (#64982) (#65091)

(cherry picked from commit bc479fcafc)
This commit is contained in:
Jie (Link) Zheng 2019-12-04 05:33:12 +08:00 committed by Matt Davis
parent 68b8e0e797
commit 0cec26f4eb
2 changed files with 21 additions and 6 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- fortios httpapi plugin - fix the issue that fortios httpapi plugin does not support python2

View file

@ -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)