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

This commit is contained in:
Jie (Link) Zheng 2019-11-19 17:21:56 +08:00 committed by Sumit Jaiswal
parent 3252665a94
commit bc479fcafc

View file

@ -84,12 +84,21 @@ class HttpApi(HttpApiBase):
""" """
headers = {} headers = {}
resp_raw_headers = []
for attr, val in response.getheaders(): if hasattr(response.headers, '_headers'):
if attr == 'Set-Cookie' and 'APSCOOKIE_' in val: 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 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) csrftoken_search = re.search('\"(.*)\"', val)
if csrftoken_search: if csrftoken_search:
self._ccsrftoken = csrftoken_search.group(1) self._ccsrftoken = csrftoken_search.group(1)
@ -119,7 +128,11 @@ class HttpApi(HttpApiBase):
try: try:
response, response_data = self.connection.send(url, data, method=method) response, response_data = self.connection.send(url, data, method=method)
response_status = None
return response.status, to_text(response_data.getvalue()) 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: except Exception as err:
raise Exception(err) raise Exception(err)