@@ -307,7 +307,11 @@ def _call_network(self, method, headers, parts, identifiers, params, data_str, d
307307 response_code = response .status_code
308308 response_data = response .content
309309 if not isinstance (response_data , (str , bytes , bytearray )):
310- response_data = response_data .decode ('utf-8' )
310+ # the more I think about it; then less likely this will ever be called
311+ try :
312+ response_data = response_data .decode ('utf-8' )
313+ except UnicodeDecodeError as e :
314+ pass
311315
312316 if self .logger :
313317 if 'text/' == response_type [0 :5 ] or response_type in ['application/javascript' , 'application/json' ]:
@@ -399,7 +403,7 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
399403 identifiers ,
400404 params , data_str , data_json , files )
401405
402- if response_code != requests_codes .ok :
406+ if response_code not in [ requests_codes .ok , requests_codes . created , requests_codes . accepted ] :
403407 # 3xx & 4xx errors (5xx's handled above)
404408 response_data = {'success' : False ,
405409 'errors' : [{'code' : response_code , 'message' :'HTTP response code %d' % response_code }],
@@ -412,7 +416,14 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
412416 # API says it's JSON; so it better be parsable as JSON
413417 # NDJSON is returned by Enterprise Log Share i.e. /zones/:id/logs/received
414418 if hasattr (response_data , 'decode' ):
415- response_data = response_data .decode ('utf-8' )
419+ try :
420+ response_data = response_data .decode ('utf-8' )
421+ except UnicodeDecodeError as e :
422+ # clearly not a string that can be decoded!
423+ if self .logger :
424+ self .logger .debug ('Response: decode(utf-8) failed, reverting to binary response' )
425+ # return binary
426+ return {'success' : True , 'result' : response_data }
416427 try :
417428 if response_data == '' :
418429 # This should really be 'null' but it isn't. Even then, it's wrong!
@@ -438,10 +449,17 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
438449 # if it's not a dict then it's not going to have 'success'
439450 return {'success' : True , 'result' : response_data }
440451
441- if response_type in ['text/plain' , 'text/csv' , ' application/octet-stream' ]:
452+ if response_type in ['text/plain' , 'application/octet-stream' ]:
442453 # API says it's text; but maybe it's actually JSON? - should be fixed in API
443454 if hasattr (response_data , 'decode' ):
444- response_data = response_data .decode ('utf-8' )
455+ try :
456+ response_data = response_data .decode ('utf-8' )
457+ except UnicodeDecodeError as e :
458+ # clearly not a string that can be decoded!
459+ if self .logger :
460+ self .logger .debug ('Response: decode(utf-8) failed, reverting to binary response' )
461+ # return binary
462+ return {'success' : True , 'result' : response_data }
445463 try :
446464 response_data = json .loads (response_data )
447465 except ValueError :
@@ -451,11 +469,17 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
451469 return response_data
452470 return {'success' : True , 'result' : response_data }
453471
454- if response_type in ['text/javascript' , 'application/javascript' , 'text/html' ]:
455- # used by Cloudflare workers
456-
472+ if response_type in ['text/javascript' , 'application/javascript' , 'text/html' , 'text/css' , 'text/csv' ]:
473+ # used by Cloudflare workers etc
457474 if hasattr (response_data , 'decode' ):
458- response_data = response_data .decode ('utf-8' )
475+ try :
476+ response_data = response_data .decode ('utf-8' )
477+ except UnicodeDecodeError as e :
478+ # clearly not a string that can be decoded!
479+ if self .logger :
480+ self .logger .debug ('Response: decode(utf-8) failed, reverting to binary response' )
481+ # return binary
482+ return {'success' : True , 'result' : response_data }
459483 return {'success' : True , 'result' : str (response_data )}
460484
461485 if response_type == 'application/octet-stream' and isinstance (response_data , (int , float )):
@@ -465,7 +489,14 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
465489 if response_type == 'application/octet-stream' and isinstance (response_data , (bytes , bytearray )):
466490 # API says it's text; but maybe it's actually JSON? - should be fixed in API
467491 if hasattr (response_data , 'decode' ):
468- response_data = response_data .decode ('utf-8' )
492+ try :
493+ response_data = response_data .decode ('utf-8' )
494+ except UnicodeDecodeError as e :
495+ # clearly not a string that can be decoded!
496+ if self .logger :
497+ self .logger .debug ('Response: decode(utf-8) failed, reverting to binary response' )
498+ # return binary
499+ return {'success' : True , 'result' : response_data }
469500 try :
470501 response_data = json .loads (response_data )
471502 except ValueError :
@@ -476,13 +507,20 @@ def _raw(self, method, headers, parts, identifiers, params, data_str, data_json,
476507 return response_data
477508 return {'success' : True , 'result' : response_data }
478509
479- if response_type [0 :6 ] in ['audio/' , 'image/' , 'video/' ]:
510+ if response_type in [ 'application/pdf' , 'application/zip' ] or response_type [0 :6 ] in ['audio/' , 'image/' , 'video/' ]:
480511 # it's raw/binary - just pass thru
481512 return {'success' : True , 'result' : response_data }
482513
483514 # Assuming nothing - but continuing anyway as if its a string
484515 if hasattr (response_data , 'decode' ):
485- response_data = response_data .decode ('utf-8' )
516+ try :
517+ response_data = response_data .decode ('utf-8' )
518+ except UnicodeDecodeError as e :
519+ # clearly not a string that can be decoded!
520+ if self .logger :
521+ self .logger .debug ('Response: decode(utf-8) failed, reverting to binary response' )
522+ # return binary
523+ return {'success' : True , 'result' : response_data }
486524 return {'success' : True , 'result' : str (response_data )}
487525
488526 def _call (self , method , parts , identifiers , params , data_str , data_json , files ):
0 commit comments