def get_message_from_urllib_addinfourl(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ taxii_content_type = http_response.info().getheader('X-TAXII-Content-Type') response_message = http_response.read() if taxii_content_type is None: # Treat it as a Failure Status Message, per the spec message = [] header_dict = http_response.info().dict.iteritems() for k, v in header_dict: message.append(k + ': ' + v + '\r\n') message.append('\r\n') message.append(response_message) m = ''.join(message) return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type) return None
def get_message_from_urllib_addinfourl(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ info = http_response.info() if hasattr(info, 'getheader'): taxii_content_type = info.getheader('X-TAXII-Content-Type') _, params = cgi.parse_header(info.getheader('Content-Type')) else: taxii_content_type = info.get('X-TAXII-Content-Type') _, params = cgi.parse_header(info.get('Content-Type')) encoding = params.get('charset', 'utf-8') response_message = http_response.read() if taxii_content_type is None: # Treat it as a Failure Status Message, per the spec message = [] header_dict = six.iteritems(http_response.info().dict) for k, v in header_dict: message.append(k + ': ' + v + '\r\n') message.append('\r\n') message.append(response_message) m = ''.join(message) return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message, encoding) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
def round_trip_message(taxii_message): if not isinstance(taxii_message, tm10.TAXIIMessage): raise ValueError('taxii_message was not an instance of TAXIIMessage') print '***** Message type = %s; id = %s' % (taxii_message.message_type, taxii_message.message_id) xml_string = taxii_message.to_xml() valid = tm10.validate_xml(xml_string) if valid is not True: raise Exception('\tFailure of test #1 - XML not schema valid: %s' % valid) msg_from_xml = tm10.get_message_from_xml(xml_string) dictionary = taxii_message.to_dict() msg_from_dict = tm10.get_message_from_dict(dictionary) if taxii_message != msg_from_xml: print '\t Failure of test #2 - running equals w/ debug:' taxii_message.__eq__(msg_from_xml, True) raise Exception('Test #2 failed - taxii_message != msg_from_xml') if taxii_message != msg_from_dict: print '\t Failure of test #3 - running equals w/ debug:' taxii_message.__eq__(msg_from_dict, True) raise Exception('Test #3 failed - taxii_message != msg_from_dict') if msg_from_xml != msg_from_dict: print '\t Failure of test #4 - running equals w/ debug:' msg_from_xml.__eq__(msg_from_dict, True) raise Exception('Test #4 failed - msg_from_xml != msg_from_dict') print '***** All tests completed!'
def get_message_from_httplib_http_response(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ if hasattr(http_response, 'getheader'): taxii_content_type = http_response.getheader('X-TAXII-Content-Type') _, params = cgi.parse_header(http_response.getheader('Content-Type')) else: taxii_content_type = http_response.get('X-TAXII-Content-Type') _, params = cgi.parse_header(http_response.get('Content-Type')) encoding = params.get('charset', 'utf-8') response_message = http_response.read() if taxii_content_type is None: # Treat it as a Failure Status Message, per the spec message = [] header_tuples = http_response.getheaders() for k, v in header_tuples: message.append(k + ': ' + v + '\r\n') message.append('\r\n') message.append(response_message) m = ''.join(message) return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message, encoding) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
def get_message_from_httplib_http_response(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ taxii_content_type = http_response.getheader('X-TAXII-Content-Type') response_message = http_response.read() if taxii_content_type is None: # Treat it as a Failure Status Message, per the spec message = [] header_tuples = http_response.getheaders() for k, v in header_tuples: message.append(k + ': ' + v + '\r\n') message.append('\r\n') message.append(response_message) m = ''.join(message) return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=tm11.ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type) return None
def get_message_from_urllib2_httperror(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ info = http_response.info() if hasattr(info, 'getheader'): taxii_content_type = info.getheader('X-TAXII-Content-Type') _, params = cgi.parse_header(info.getheader('Content-Type')) else: taxii_content_type = info.get('X-TAXII-Content-Type') _, params = cgi.parse_header(info.get('Content-Type')) encoding = params.get('charset', 'utf-8') response_message = http_response.read() if taxii_content_type is None: m = str(http_response) + '\r\n' + str( http_response.info()) + '\r\n' + response_message return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message, encoding) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
def get_message_from_urllib2_httperror(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ info = http_response.info() if hasattr(info, 'getheader'): taxii_content_type = info.getheader('X-TAXII-Content-Type') _, params = cgi.parse_header(info.getheader('Content-Type')) else: taxii_content_type = info.get('X-TAXII-Content-Type') _, params = cgi.parse_header(info.get('Content-Type')) encoding = params.get('charset', 'utf-8') response_message = http_response.read() if taxii_content_type is None: m = str(http_response) + '\r\n' + str(http_response.info()) + '\r\n' + response_message return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message, encoding) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
def test_01(self): """ Test all the encodings for TAXII 1.0 XML """ for encoding in PYTHON_ENCODINGS: if encoding in ('cp720', 'cp858', 'iso8859_11') and (sys.version_info[0] == 2 and sys.version_info[1] == 6): continue # This encoding is not supported in Python 2.6 encoded_doc = xml_taxii_message_10.encode(encoding, 'strict') try: msg = tm10.get_message_from_xml(encoded_doc, encoding) except Exception as e: print('Bad codec was: %s' % encoding) raise
def round_trip_message(taxii_message): if not isinstance(taxii_message, tm10.TAXIIMessage): raise ValueError('taxii_message was not an instance of TAXIIMessage') print('***** Message type = %s; id = %s' % (taxii_message.message_type, taxii_message.message_id)) xml_string = taxii_message.to_xml() # This is the old, deprecated way of validating with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) valid = tm10.validate_xml(xml_string) if valid is not True: raise Exception('\tFailure of test #1 - XML not schema valid: %s' % valid) # The new way of validating sv = SchemaValidator(SchemaValidator.TAXII_10_SCHEMA) try: result = sv.validate_string(xml_string) except XMLSyntaxError: raise if not result.valid: errors = [item for item in result.error_log] raise Exception('\tFailure of test #1 - XML not schema valid: %s' % errors) msg_from_xml = tm10.get_message_from_xml(xml_string) dictionary = taxii_message.to_dict() msg_from_dict = tm10.get_message_from_dict(dictionary) taxii_message.to_text() if taxii_message != msg_from_xml: print('\t Failure of test #2 - running equals w/ debug:') taxii_message.__eq__(msg_from_xml, True) raise Exception('Test #2 failed - taxii_message != msg_from_xml') if taxii_message != msg_from_dict: print('\t Failure of test #3 - running equals w/ debug:') taxii_message.__eq__(msg_from_dict, True) raise Exception('Test #3 failed - taxii_message != msg_from_dict') if msg_from_xml != msg_from_dict: print('\t Failure of test #4 - running equals w/ debug:') msg_from_xml.__eq__(msg_from_dict, True) raise Exception('Test #4 failed - msg_from_xml != msg_from_dict') print('***** All tests completed!')
def get_message_from_urllib2_httperror(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ taxii_content_type = http_response.info().getheader('X-TAXII-Content-Type') response_message = http_response.read() if taxii_content_type is None: m = str(http_response.info()) + '\r\n' + response_message return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=tm11.ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message) elif taxii_content_type == VID_TAXII_XML_11: #It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type) return None
def get_message_from_urllib2_httperror(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ taxii_content_type = http_response.info().getheader('X-TAXII-Content-Type') response_message = http_response.read() if taxii_content_type is None: m = str(http_response.info()) + '\r\n' + response_message return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type) return None
def get_message_from_client_response(resp, in_response_to): """ helper func""" taxii_content_type = resp.get('X-TAXII-Content-Type', None) response_message = resp.content if taxii_content_type is None: m = str(resp) + '\r\n' + response_message return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type) return None
def get_message_from_urllib_addinfourl(http_response, in_response_to): """ This function should not be called by libtaxii users directly. """ info = http_response.info() if hasattr(info, 'getheader'): taxii_content_type = info.getheader('X-TAXII-Content-Type') _, params = cgi.parse_header(info.getheader('Content-Type')) else: taxii_content_type = info.get('X-TAXII-Content-Type') _, params = cgi.parse_header(info.get('Content-Type')) encoding = params.get('charset', 'utf-8') response_message = six.ensure_text(http_response.read(), errors='replace') if taxii_content_type is None: # Treat it as a Failure Status Message, per the spec message = [] header_dict = six.iteritems(http_response.info().dict) for k, v in header_dict: message.append(k + ': ' + v + '\r\n') message.append('\r\n') message.append(response_message) m = ''.join(message) return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m) elif taxii_content_type == VID_TAXII_XML_10: # It's a TAXII XML 1.0 message return tm10.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_TAXII_XML_11: # It's a TAXII XML 1.1 message return tm11.get_message_from_xml(response_message, encoding) elif taxii_content_type == VID_CERT_EU_JSON_10: return tm10.get_message_from_json(response_message, encoding) else: raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
def get_sent_message(): body = httpretty.last_request().body print(body) return tm10.get_message_from_xml(body)
def get_sent_message(): body = responses.calls[-1].request.body print(repr(body)) return tm10.get_message_from_xml(body)