Beispiel #1
0
    def _forward_request(transaction, destination, path):
        """
        Forward requests.

        :type transaction: Transaction
        :param transaction: the transaction that owns the request
        :param destination: the destination of the request (IP, port)
        :param path: the path of the request.
        :rtype : Transaction
        :return: the edited transaction
        """
        client = HelperClient(destination)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = path
        request.destination = destination
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        return transaction
Beispiel #2
0
    def _test_with_client_delayed(self, message_list):  # pragma: no cover
        client = HelperClient(self.server_address)
        start_etag = None
        for message, expected in message_list:
            if message is not None:
                received_message = client.send_request(message)
                time.sleep(5)
            if received_message.etag is not None:
                if start_etag is None:
                    start_etag = received_message.etag
            if expected is not None:
                if expected.etag is not None:
                    if expected.etag == 1:
                        self.assertEqual(received_message, start_etag)
                    else:
                        self.assertNotEqual(received_message, start_etag)
                if expected.type is not None:
                    self.assertEqual(received_message.type, expected.type)
                if expected.mid is not None:
                    self.assertEqual(received_message.mid, expected.mid)
                self.assertEqual(received_message.code, expected.code)
                if expected.source is not None:
                    self.assertEqual(received_message.source,
                                     self.server_address)

        client.stop()
 def _test_with_client_observe(self, message_list):  # pragma: no cover
     client = HelperClient(self.server_address)
     for message, expected in message_list:
         if message is not None:
             client.send_request(message, self.client_callback)
         if expected is not None:
             received_message = self.queue.get()
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source,
                                  self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload,
                                  expected.payload)
             if expected.options:
                 self.assertEqual(len(received_message.options),
                                  len(expected.options))
                 for o in expected.options:
                     assert isinstance(o, Option)
                     option_value = getattr(
                         expected,
                         o.name.lower().replace("-", "_"))
                     option_value_rec = getattr(
                         received_message,
                         o.name.lower().replace("-", "_"))
                     self.assertEqual(option_value, option_value_rec)
     client.stop()
Beispiel #4
0
    def _test_with_client_delayed(self, message_list):  # pragma: no cover
        client = HelperClient(self.server_address)
        for message, expected in message_list:
            if message is not None:
                received_message = client.send_request(message)
                time.sleep(5)

            if expected is not None:
                if expected.etag is not None:
                    self.assertEqual(received_message.etag, expected.etag)
                if expected.type is not None:
                    self.assertEqual(received_message.type, expected.type)
                if expected.mid is not None:
                    self.assertEqual(received_message.mid, expected.mid)
                self.assertEqual(received_message.code, expected.code)
                if expected.source is not None:
                    self.assertEqual(received_message.source, self.server_address)
                if expected.token is not None:
                    self.assertEqual(received_message.token, expected.token)
                if expected.payload is not None:
                    self.assertEqual(received_message.payload, expected.payload)
                if expected.max_age is not None:
                    if expected.max_age != 60:
                        self.assertNotEqual(received_message.max_age, 60)
                    else:
                        self.assertEqual(received_message.max_age, expected.max_age)
                if expected.options:
                    self.assertEqual(len(received_message.options), len(expected.options))
                    for o in expected.options:
                        assert isinstance(o, Option)
                        if o.name != defines.OptionRegistry.MAX_AGE.name and o.name != defines.OptionRegistry.ETAG.name:
                            option_value = getattr(expected, o.name.lower().replace("-", "_"))
                            option_value_rec = getattr(received_message, o.name.lower().replace("-", "_"))
                            self.assertEqual(option_value, option_value_rec)
        client.stop()
Beispiel #5
0
 def _test_with_client(self, message_list):  # pragma: no cover
     client = HelperClient(self.server_address)
     for message, expected in message_list:
         if message is not None:
             received_message = client.send_request(message)
         if expected is not None:
             # print(received_message.payload)
             # print(expected.payload)
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source,
                                  self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload,
                                  expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options,
                                  expected.options)
                 for o in expected.options:
                     option_value = getattr(
                         expected,
                         o.name.lower().replace("-", "_"))
                     option_value_rec = getattr(
                         received_message,
                         o.name.lower().replace("-", "_"))
                     self.assertEqual(option_value, option_value_rec)
     client.stop()
Beispiel #6
0
 def _test_with_client_observe(self, message_list):  # pragma: no cover
     client = HelperClient(self.server_address)
     for message, expected in message_list:
         if message is not None:
             client.send_request(message, self.client_callback)
         if expected is not None:
             received_message = self.queue.get()
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload, expected.payload)
             if expected.options:
                 self.assertEqual(len(received_message.options), len(expected.options))
                 for o in expected.options:
                     assert isinstance(o, Option)
                     option_value = getattr(expected, o.name.lower().replace("-", "_"))
                     option_value_rec = getattr(received_message, o.name.lower().replace("-", "_"))
                     self.assertEqual(option_value, option_value_rec)
     client.stop()
Beispiel #7
0
    def _test_with_client_delayed(self, message_list):  # pragma: no cover
        client = HelperClient(self.server_address)
        for message, expected in message_list:
            if message is not None:
                received_message = client.send_request(message)
                time.sleep(5)

            if expected is not None:
                if expected.etag is not None:
                    self.assertEqual(received_message.etag, expected.etag)
                if expected.type is not None:
                    self.assertEqual(received_message.type, expected.type)
                if expected.mid is not None:
                    self.assertEqual(received_message.mid, expected.mid)
                self.assertEqual(received_message.code, expected.code)
                if expected.source is not None:
                    self.assertEqual(received_message.source, self.server_address)
                if expected.token is not None:
                    self.assertEqual(received_message.token, expected.token)
                if expected.payload is not None:
                    self.assertEqual(received_message.payload, expected.payload)
                if expected.max_age is not None:
                    if expected.max_age != 60:
                        self.assertNotEqual(received_message.max_age, 60)
                    else:
                        self.assertEqual(received_message.max_age, expected.max_age)
                if expected.options:
                    self.assertEqual(len(received_message.options), len(expected.options))
                    for o in expected.options:
                        assert isinstance(o, Option)
                        if o.name != defines.OptionRegistry.MAX_AGE.name and o.name != defines.OptionRegistry.ETAG.name:
                            option_value = getattr(expected, o.name.lower().replace("-", "_"))
                            option_value_rec = getattr(received_message, o.name.lower().replace("-", "_"))
                            self.assertEqual(option_value, option_value_rec)
        client.stop()
Beispiel #8
0
 def _test_with_client_observe(self, message_list, callback):
     client = HelperClient(self.server_address)
     token = None
     last_mid = 0
     for message, expected in message_list:
         if message is not None:
             token = message.token
             client.send_request(message, callback)
         received_message = self.queue.get()
         if expected is not None:
             last_mid = expected.mid
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload, expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options, expected.options)
     message = Message()
     message.type = defines.Types["RST"]
     message.token = token
     message._mid = last_mid
     message.destination = self.server_address
     client.send_empty(message)
     client.stop()
Beispiel #9
0
 def _test_with_client_observe(self, message_list,
                               callback):  # pragma: no cover
     client = HelperClient(self.server_address)
     token = None
     last_mid = 0
     for message, expected in message_list:
         if message is not None:
             token = message.token
             client.send_request(message, callback)
         received_message = self.queue.get()
         if expected is not None:
             last_mid = expected.mid
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source,
                                  self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload,
                                  expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options,
                                  expected.options)
     message = Message()
     message.type = defines.Types["RST"]
     message.token = token
     message._mid = last_mid
     message.destination = self.server_address
     client.send_empty(message)
     client.stop()
Beispiel #10
0
 def discover_remote(self, destination, name):
     assert (isinstance(destination, str))
     split = destination.split(":", 1)
     host = split[0]
     port = int(split[1])
     server = (host, port)
     client = HelperClient(server)
     response = client.discover()
     client.stop()
     self.discover_remote_results(response, name)
Beispiel #11
0
 def discover_remote(self, destination, name):
     assert isinstance(destination, str)
     split = destination.split(":", 1)
     host = split[0]
     port = int(split[1])
     server = (host, port)
     client = HelperClient(server)
     response = client.discover()
     client.stop()
     self.discover_remote_results(response, name)
Beispiel #12
0
    def _handle_request(self, transaction, new_resource):
        """
        Forward requests. Used by reverse proxies to also create new virtual resources on the proxy 
        in case of created resources
        
        :type new_resource: bool
        :type transaction: Transaction
        :param transaction: the transaction that owns the request
        :rtype : Transaction
        :param new_resource: if the request will generate a new resource 
        :return: the edited transaction
        """
        client = HelperClient(transaction.resource.remote_server)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = "/".join(
            transaction.request.uri_path.split("/")[1:])
        request.destination = transaction.resource.remote_server
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        logger.info("forward_request - " + str(request))
        response = client.send_request(request)
        client.stop()
        logger.info("forward_response - " + str(response))
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        if response.code == defines.Codes.CREATED.number:
            lp = transaction.response.location_path
            del transaction.response.location_path
            transaction.response.location_path = transaction.request.uri_path.split(
                "/")[0] + "/" + lp
            # TODO handle observing
            if new_resource:
                resource = RemoteResource('server',
                                          transaction.resource.remote_server,
                                          lp,
                                          coap_server=self,
                                          visible=True,
                                          observable=False,
                                          allow_children=True)
                self._server.add_resource(transaction.response.location_path,
                                          resource)
        if response.code == defines.Codes.DELETED.number:
            del self._server.root["/" + transaction.request.uri_path]
        return transaction
Beispiel #13
0
    def _handle_request(self, transaction, new_resource):
        """
        Forward requests. Used by reverse proxies to also create new virtual resources on the proxy 
        in case of created resources
        
        :type new_resource: bool
        :type transaction: Transaction
        :param transaction: the transaction that owns the request
        :rtype : Transaction
        :param new_resource: if the request will generate a new resource 
        :return: the edited transaction
        """
        client = HelperClient(transaction.resource.remote_server)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:])
        request.destination = transaction.resource.remote_server
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        if response.code == defines.Codes.CREATED.number:
            lp = transaction.response.location_path
            del transaction.response.location_path
            transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp
            # TODO handle observing
            if new_resource:
                resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self,
                                          visible=True,
                                          observable=False,
                                          allow_children=True)
                self._server.add_resource(transaction.response.location_path, resource)
        if response.code == defines.Codes.DELETED.number:
            del self._server.root["/" + transaction.request.uri_path]
        return transaction
Beispiel #14
0
    def _handle_request(self, transaction, new_resource):
        client = HelperClient(transaction.resource.remote_server)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = "/".join(
            transaction.request.uri_path.split("/")[1:])
        request.destination = transaction.resource.remote_server
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        if response.code == defines.Codes.CREATED.number:
            lp = transaction.response.location_path
            del transaction.response.location_path
            transaction.response.location_path = transaction.request.uri_path.split(
                "/")[0] + "/" + lp
            # TODO handle observing
            if new_resource:
                resource = RemoteResource('server',
                                          transaction.resource.remote_server,
                                          lp,
                                          coap_server=self,
                                          visible=True,
                                          observable=False,
                                          allow_children=True)
                self._server.add_resource(transaction.response.location_path,
                                          resource)
        if response.code == defines.Codes.DELETED.number:
            del self._server.root["/" + transaction.request.uri_path]
        return transaction
Beispiel #15
0
 def _test_with_client(self, message_list):
     client = HelperClient(self.server_address)
     for message, expected in message_list:
         if message is not None:
             received_message = client.send_request(message)
         if expected is not None:
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, self.server_address)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload, expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options, expected.options)
     client.stop()
Beispiel #16
0
    def _forward_request(transaction, destination, path):
        client = HelperClient(destination)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = path
        request.destination = destination
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        return transaction
Beispiel #17
0
    def _forward_request(transaction, destination, path):
        client = HelperClient(destination)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = path
        request.destination = destination
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        return transaction
Beispiel #18
0
    def _forward_request(transaction, destination, path):
        """
        Forward requests.

        :type transaction: Transaction
        :param transaction: the transaction that owns the request
        :param destination: the destination of the request (IP, port)
        :param path: the path of the request.
        :rtype : Transaction
        :return: the edited transaction
        """
        client = HelperClient(destination)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = path
        request.destination = destination
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        if response is not None:
            transaction.response.payload = response.payload
            transaction.response.code = response.code
            transaction.response.options = response.options
        else:
            transaction.response.code = defines.Codes.SERVICE_UNAVAILABLE.number

        return transaction
Beispiel #19
0
    def _handle_request(self, transaction, new_resource):
        client = HelperClient(transaction.resource.remote_server)
        request = Request()
        request.options = copy.deepcopy(transaction.request.options)
        del request.block2
        del request.block1
        del request.uri_path
        del request.proxy_uri
        del request.proxy_schema
        # TODO handle observing
        del request.observe
        # request.observe = transaction.request.observe

        request.uri_path = "/".join(transaction.request.uri_path.split("/")[1:])
        request.destination = transaction.resource.remote_server
        request.payload = transaction.request.payload
        request.code = transaction.request.code
        response = client.send_request(request)
        client.stop()
        transaction.response.payload = response.payload
        transaction.response.code = response.code
        transaction.response.options = response.options
        if response.code == defines.Codes.CREATED.number:
            lp = transaction.response.location_path
            del transaction.response.location_path
            transaction.response.location_path = transaction.request.uri_path.split("/")[0] + "/" + lp
            # TODO handle observing
            if new_resource:
                resource = RemoteResource('server', transaction.resource.remote_server, lp, coap_server=self,
                                          visible=True,
                                          observable=False,
                                          allow_children=True)
                self._server.add_resource(transaction.response.location_path, resource)
        if response.code == defines.Codes.DELETED.number:
            del self._server.root["/" + transaction.request.uri_path]
        return transaction