Skip to content

Commit 7f329cd

Browse files
committed
Merge branch 'master' into autofocus
2 parents f818019 + 966b3e3 commit 7f329cd

File tree

2 files changed

+18
-52
lines changed

2 files changed

+18
-52
lines changed

bin/panwfapi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ def validate_hash(hash):
280280
if debug > 0:
281281
return
282282

283-
if not (len(hash) == 32 or len(hash) == 64):
284-
print('hash length must be 32 (MD5) or 64 (SHA256)',
283+
x = len(hash)
284+
if not (x in [32, 64]):
285+
print('hash length (%d) must be 32 (MD5) or 64 (SHA256)' % x,
285286
file=sys.stderr)
286287
sys.exit(1)
287288

lib/pan/wfapi.py

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
from __future__ import print_function
3131
import socket
3232
import sys
33-
import re
3433
import os
3534
from io import BytesIO
35+
import email
36+
import email.errors
3637
import email.utils
3738
import logging
3839
try:
@@ -189,51 +190,24 @@ def __clear_response(self):
189190
self.xml_element_root = None
190191
self.attachment = None
191192

192-
def __get_header(self, response, name):
193-
"""use getheader() method depending or urllib in use"""
194-
195-
s = None
196-
body = set()
197-
198-
if hasattr(response, 'getheader'):
199-
# 3.2, http.client.HTTPResponse
200-
s = response.getheader(name)
201-
elif hasattr(response.info(), 'getheader'):
202-
# 2.7, httplib.HTTPResponse
203-
s = response.info().getheader(name)
204-
else:
205-
raise PanWFapiError('no getheader() method found in ' +
206-
'urllib response')
207-
208-
if s is not None:
209-
body = [x.lower() for x in s.split(';')]
210-
body = [x.lstrip() for x in body]
211-
body = [x.rstrip() for x in body]
212-
body = set(body)
213-
214-
self._log(DEBUG3, '__get_header(%s): %s', name, s)
215-
self._log(DEBUG3, '__get_header: %s', body)
216-
217-
return body
218-
219193
def __set_response(self, response):
220194
message_body = response.read()
221195

222-
content_type = self.__get_header(response, 'content-type')
196+
content_type = self._message.get_content_type()
223197
if not content_type:
224198
if self._msg is None:
225199
self._msg = 'no content-type response header'
226200
return False
227201

228-
if 'application/octet-stream' in content_type:
202+
if content_type == 'application/octet-stream':
229203
return self.__set_stream_response(response, message_body)
230204

231205
# XXX text/xml RFC 3023
232-
elif ('application/xml' in content_type or
233-
'text/xml' in content_type):
206+
elif (content_type == 'application/xml' or
207+
content_type == 'text/xml'):
234208
return self.__set_xml_response(message_body)
235209

236-
elif 'text/html' in content_type:
210+
elif content_type == 'text/html':
237211
return self.__set_html_response(message_body)
238212

239213
else:
@@ -242,25 +216,11 @@ def __set_response(self, response):
242216
return False
243217

244218
def __set_stream_response(self, response, message_body):
245-
content_disposition = self.__get_header(response,
246-
'content-disposition')
247-
if not content_disposition:
219+
filename = self._message.get_filename()
220+
if not filename:
248221
self._msg = 'no content-disposition response header'
249222
return False
250223

251-
if 'attachment' not in content_disposition:
252-
msg = 'no handler for content-disposition: %s' % \
253-
content_disposition
254-
self._msg = msg
255-
return False
256-
257-
filename = None
258-
for type in content_disposition:
259-
result = re.search(r'^filename=([-\w\.]+)$', type)
260-
if result:
261-
filename = result.group(1)
262-
break
263-
264224
attachment = {}
265225
attachment['filename'] = filename
266226
attachment['content'] = message_body
@@ -384,10 +344,15 @@ def __api_request(self, request_uri, body, headers={}):
384344
elif self.http_code in responses:
385345
self.http_reason = responses[self.http_code]
386346

347+
try:
348+
self._message = email.message_from_string(str(response.info()))
349+
except (TypeError, email.errors.MessageError) as e:
350+
raise PanWFapiError('email.message_from_string() %s' % e)
351+
387352
self._log(DEBUG2, 'HTTP response code: %s', self.http_code)
388353
self._log(DEBUG2, 'HTTP response reason: %s', self.http_reason)
389354
self._log(DEBUG2, 'HTTP response headers:')
390-
self._log(DEBUG2, '%s', response.info())
355+
self._log(DEBUG2, '%s', self._message)
391356

392357
if not (200 <= self.http_code < 300):
393358
self._msg = 'HTTP Error %s: %s' % (self.http_code,

0 commit comments

Comments
 (0)