30
30
from __future__ import print_function
31
31
import socket
32
32
import sys
33
- import re
34
33
import os
35
34
from io import BytesIO
35
+ import email
36
+ import email .errors
36
37
import email .utils
37
38
import logging
38
39
try :
@@ -189,51 +190,24 @@ def __clear_response(self):
189
190
self .xml_element_root = None
190
191
self .attachment = None
191
192
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
-
219
193
def __set_response (self , response ):
220
194
message_body = response .read ()
221
195
222
- content_type = self .__get_header ( response , 'content-type' )
196
+ content_type = self ._message . get_content_type ( )
223
197
if not content_type :
224
198
if self ._msg is None :
225
199
self ._msg = 'no content-type response header'
226
200
return False
227
201
228
- if 'application/octet-stream' in content_type :
202
+ if content_type == 'application/octet-stream' :
229
203
return self .__set_stream_response (response , message_body )
230
204
231
205
# 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' ):
234
208
return self .__set_xml_response (message_body )
235
209
236
- elif 'text/html' in content_type :
210
+ elif content_type == 'text/html' :
237
211
return self .__set_html_response (message_body )
238
212
239
213
else :
@@ -242,25 +216,11 @@ def __set_response(self, response):
242
216
return False
243
217
244
218
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 :
248
221
self ._msg = 'no content-disposition response header'
249
222
return False
250
223
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
-
264
224
attachment = {}
265
225
attachment ['filename' ] = filename
266
226
attachment ['content' ] = message_body
@@ -384,10 +344,15 @@ def __api_request(self, request_uri, body, headers={}):
384
344
elif self .http_code in responses :
385
345
self .http_reason = responses [self .http_code ]
386
346
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
+
387
352
self ._log (DEBUG2 , 'HTTP response code: %s' , self .http_code )
388
353
self ._log (DEBUG2 , 'HTTP response reason: %s' , self .http_reason )
389
354
self ._log (DEBUG2 , 'HTTP response headers:' )
390
- self ._log (DEBUG2 , '%s' , response . info () )
355
+ self ._log (DEBUG2 , '%s' , self . _message )
391
356
392
357
if not (200 <= self .http_code < 300 ):
393
358
self ._msg = 'HTTP Error %s: %s' % (self .http_code ,
0 commit comments