openc2lib.transfers.http.message
HTTP Message
This module defines the HTTP definition of the abstract OpenC2 Message data. See Sec. 3.3.2 of the HTTP Specification.
1""" HTTP Message 2 3 This module defines the HTTP definition of the abstract OpenC2 Message data. 4 See Sec. 3.3.2 of the HTTP Specification. 5""" 6import dataclasses 7import logging 8import copy 9 10import openc2lib as oc2 11 12 13#logger = logging.getLogger('openc2lib') 14#""" The logging facility in openc2lib """ 15 16class Headers(oc2.Map): 17 """ HTTP Message Headers (see Sec. 3.3.2 of the Specification) 18 19 Note: the Specification defines `to` as "String [0..*]", but it should be an `ArrayOf(str)`. Using 20 a plain Python list does not work with the current openc2lib implementation. 21 """ 22 fieldtypes = {'request_id': str, 'created': oc2.DateTime, 'from': str, 'to': oc2.ArrayOf(str)} 23 extend = None 24 regext = {} 25 26 27OpenC2Contents = oc2.Register() 28""" List allowed OpenC2-Content (see Sec. 3.3.2 of the Specification) """ 29OpenC2Contents.add('request', oc2.Command, 1) 30OpenC2Contents.add('response', oc2.Response, 2) 31# Event is not currently defined in the Language Specification 32# and there is not indication how to manage it. 33# OpenC2Contents.add('notification', oc2.Event, 3) 34 35class OpenC2Content(oc2.Choice): 36 """ HTTP Message OpenC2-Content (see Sec. 3.3.2 of the Specification) """ 37 register = OpenC2Contents 38 39Bodies = oc2.Register() 40""" List allowed objects in Body (see Sec. 3.3.2 of the Specification) """ 41Bodies.add('openc2',OpenC2Content, 1) 42 43class Body(oc2.Choice): 44 """ HTTP Message Body (see Sec. 3.3.2 of the Specification) """ 45 register = Bodies 46 47@dataclasses.dataclass 48class Message(oc2.Record): 49 """ HTTP Message representation 50 51 This class implements the HTTP-specific representation of the 52 OpenC2 Message metadata. The OpenC2 Message metadata are described in 53 Table 3.1 of the Language Specification as message elements, but they are not 54 framed in a concrete structure. The HTTP Specification defines such structure 55 in Sec. 3.3.2, and this class is its implementation. 56 57 The methods of this class are meant to translate back and for the openc2lib 58 `Message` class. 59 """ 60 headers: Headers = None 61 """ Contains the `Message` metadata """ 62 body: Body = None # This is indeed not optional, but the default argument is set to preserve ordering 63 """ Contains the `Content` """ 64 signature: str = None 65 """ Not used (the Specification does not define its usage """ 66 67 def set(self, msg: oc2.Message): 68 """ Create HTTP `Message` from openc2lib `Message` 69 70 :param msg: An openc2lib `Message`. 71 :return: An HTTP `Message` 72 """ 73 self.headers = {} 74 self.headers['request_id'] = msg.request_id 75 self.headers['created'] = msg.created 76 self.headers['from'] = msg.from_ 77 self.headers['to'] = msg.to 78 79 self.body = Body(OpenC2Content(msg.content)) 80 81 82 def get(self): 83 """ Create an openc2lib `Message` from HTTP `Message` 84 85 :param msg: An openc2lib `Message`. 86 :return: An HTTP `Message` 87 """ 88 msg = oc2.Message(self.body.getObj().getObj()) 89 msg.request_id = self.headers['request_id'] if 'request_id' in self.headers.keys() else None 90 msg.created = self.headers['created'] if 'created' in self.headers.keys() else None 91 msg.from_ = self.headers['from'] if 'from' in self.headers.keys() else None 92 msg.to = self.headers['to'] if 'to' in self.headers.keys() else None 93 msg.msg_type = msg.content.getType() 94 95 return msg
17class Headers(oc2.Map): 18 """ HTTP Message Headers (see Sec. 3.3.2 of the Specification) 19 20 Note: the Specification defines `to` as "String [0..*]", but it should be an `ArrayOf(str)`. Using 21 a plain Python list does not work with the current openc2lib implementation. 22 """ 23 fieldtypes = {'request_id': str, 'created': oc2.DateTime, 'from': str, 'to': oc2.ArrayOf(str)} 24 extend = None 25 regext = {}
HTTP Message Headers (see Sec. 3.3.2 of the Specification)
Note: the Specification defines to as "String [0..*]", but it should be an ArrayOf(str). Using
a plain Python list does not work with the current openc2lib implementation.
Field types
A dictionary which keys are field names and which values are the corresponding classes.
Must be provided by any derived class.
Base class
Data types defined in the Language Specification shall not set this field. Data types defined in Profiles that extends a Data Type defined in the Language Specification, must set this field to the corresponding class of the base Data Type.
Note: Extensions defined in the openc2lib context are recommended to use the same name of the base Data Type, and to distinguish them through appropriate usage of the namespacing mechanism.
Registered extensions
Classes that implement a Data Type defined in the Language Specification will use this field to register extensions defined by external Profiles. Classes that define extensions within Profiles shall register themselves according to the specific documentation of the base type class, but shall not modify this field.
Inherited Members
- builtins.dict
- get
- setdefault
- pop
- popitem
- keys
- items
- values
- update
- fromkeys
- clear
- copy
List allowed OpenC2-Content (see Sec. 3.3.2 of the Specification)
36class OpenC2Content(oc2.Choice): 37 """ HTTP Message OpenC2-Content (see Sec. 3.3.2 of the Specification) """ 38 register = OpenC2Contents
HTTP Message OpenC2-Content (see Sec. 3.3.2 of the Specification)
List of registered name/class options available
List allowed objects in Body (see Sec. 3.3.2 of the Specification)
44class Body(oc2.Choice): 45 """ HTTP Message Body (see Sec. 3.3.2 of the Specification) """ 46 register = Bodies
HTTP Message Body (see Sec. 3.3.2 of the Specification)
48@dataclasses.dataclass 49class Message(oc2.Record): 50 """ HTTP Message representation 51 52 This class implements the HTTP-specific representation of the 53 OpenC2 Message metadata. The OpenC2 Message metadata are described in 54 Table 3.1 of the Language Specification as message elements, but they are not 55 framed in a concrete structure. The HTTP Specification defines such structure 56 in Sec. 3.3.2, and this class is its implementation. 57 58 The methods of this class are meant to translate back and for the openc2lib 59 `Message` class. 60 """ 61 headers: Headers = None 62 """ Contains the `Message` metadata """ 63 body: Body = None # This is indeed not optional, but the default argument is set to preserve ordering 64 """ Contains the `Content` """ 65 signature: str = None 66 """ Not used (the Specification does not define its usage """ 67 68 def set(self, msg: oc2.Message): 69 """ Create HTTP `Message` from openc2lib `Message` 70 71 :param msg: An openc2lib `Message`. 72 :return: An HTTP `Message` 73 """ 74 self.headers = {} 75 self.headers['request_id'] = msg.request_id 76 self.headers['created'] = msg.created 77 self.headers['from'] = msg.from_ 78 self.headers['to'] = msg.to 79 80 self.body = Body(OpenC2Content(msg.content)) 81 82 83 def get(self): 84 """ Create an openc2lib `Message` from HTTP `Message` 85 86 :param msg: An openc2lib `Message`. 87 :return: An HTTP `Message` 88 """ 89 msg = oc2.Message(self.body.getObj().getObj()) 90 msg.request_id = self.headers['request_id'] if 'request_id' in self.headers.keys() else None 91 msg.created = self.headers['created'] if 'created' in self.headers.keys() else None 92 msg.from_ = self.headers['from'] if 'from' in self.headers.keys() else None 93 msg.to = self.headers['to'] if 'to' in self.headers.keys() else None 94 msg.msg_type = msg.content.getType() 95 96 return msg
HTTP Message representation
This class implements the HTTP-specific representation of the OpenC2 Message metadata. The OpenC2 Message metadata are described in Table 3.1 of the Language Specification as message elements, but they are not framed in a concrete structure. The HTTP Specification defines such structure in Sec. 3.3.2, and this class is its implementation.
The methods of this class are meant to translate back and for the openc2lib
Message class.
68 def set(self, msg: oc2.Message): 69 """ Create HTTP `Message` from openc2lib `Message` 70 71 :param msg: An openc2lib `Message`. 72 :return: An HTTP `Message` 73 """ 74 self.headers = {} 75 self.headers['request_id'] = msg.request_id 76 self.headers['created'] = msg.created 77 self.headers['from'] = msg.from_ 78 self.headers['to'] = msg.to 79 80 self.body = Body(OpenC2Content(msg.content))
83 def get(self): 84 """ Create an openc2lib `Message` from HTTP `Message` 85 86 :param msg: An openc2lib `Message`. 87 :return: An HTTP `Message` 88 """ 89 msg = oc2.Message(self.body.getObj().getObj()) 90 msg.request_id = self.headers['request_id'] if 'request_id' in self.headers.keys() else None 91 msg.created = self.headers['created'] if 'created' in self.headers.keys() else None 92 msg.from_ = self.headers['from'] if 'from' in self.headers.keys() else None 93 msg.to = self.headers['to'] if 'to' in self.headers.keys() else None 94 msg.msg_type = msg.content.getType() 95 96 return msg