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

(cherry picked from commit bc479fcafc)
pull/65015/head^2
Jie (Link) Zheng 6 years ago committed by Matt Davis
parent 68b8e0e797
commit 0cec26f4eb

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

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

Loading…
Cancel
Save