Пример #1
0
def encode_short_string(pieces, value):
    """Encode a string value as short string and append it to pieces list
    returning the size of the encoded value.

    :param list pieces: Already encoded values
    :param value: String value to encode
    :type value: str or unicode
    :rtype: int

    """
    encoded_value = as_bytes(value)
    length = len(encoded_value)

    # 4.2.5.3
    # Short strings, stored as an 8-bit unsigned integer length followed by zero
    # or more octets of data. Short strings can carry up to 255 octets of UTF-8
    # data, but may not contain binary zero octets.
    # ...
    # 4.2.5.5
    # The server SHOULD validate field names and upon receiving an invalid field
    # name, it SHOULD signal a connection exception with reply code 503 (syntax
    # error).
    # -> validate length (avoid truncated utf-8 / corrupted data), but skip null
    # byte check.
    if length > 255:
        raise exceptions.ShortStringTooLong(encoded_value)

    pieces.append(struct.pack('B', length))
    pieces.append(encoded_value)
    return 1 + length
Пример #2
0
def encode_short_string(pieces, value):
    """Encode a string value as short string and append it to pieces list
    returning the size of the encoded value.

    :param list pieces: Already encoded values
    :param value: String value to encode
    :type value: str or unicode
    :rtype: int

    """
    encoded_value = as_bytes(value)
    length = len(encoded_value)

    # 4.2.5.3
    # Short strings, stored as an 8-bit unsigned integer length followed by zero
    # or more octets of data. Short strings can carry up to 255 octets of UTF-8
    # data, but may not contain binary zero octets.
    # ...
    # 4.2.5.5
    # The server SHOULD validate field names and upon receiving an invalid field
    # name, it SHOULD signal a connection exception with reply code 503 (syntax
    # error).
    # -> validate length (avoid truncated utf-8 / corrupted data), but skip null
    # byte check.
    if length > 255:
        raise exceptions.ShortStringTooLong(encoded_value)

    pieces.append(struct.pack('B', length))
    pieces.append(encoded_value)
    return 1 + length
Пример #3
0
 def begin(self, channel):
     self.name = self.__class__.__name__ + ':' + str(id(self))
     channel.exchange_declare(self.on_exchange_declared,
                              as_bytes(self.name),
                              exchange_type=self.X_TYPE,
                              passive=False,
                              durable=False,
                              auto_delete=True)
Пример #4
0
 def begin(self, channel):
     self.name = self.__class__.__name__ + ':' + str(id(self))
     self.channel.add_on_close_callback(self.on_channel_closed)
     channel.exchange_declare(self.on_exchange_declared,
                              as_bytes(self.name),
                              exchange_type=self.X_TYPE1,
                              passive=False,
                              durable=False,
                              auto_delete=True)
Пример #5
0
 def on_get(self, channel, method, header, body):
     self.assertIsInstance(method, spec.Basic.GetOk)
     self.assertEqual(body, as_bytes(self.msg_body))
     self.channel.basic_ack(method.delivery_tag)
     self.stop()
Пример #6
0
 def on_message(self, channel, method, header, body):
     self.assertIsInstance(method, spec.Basic.Deliver)
     self.assertEqual(body, as_bytes(self.msg_body))
     self.channel.basic_ack(method.delivery_tag)
     self.channel.basic_cancel(self.ctag, callback=self.on_cancelled)
Пример #7
0
 def on_get(self, channel, method, header, body):
     self.assertIsInstance(method, spec.Basic.GetOk)
     self.assertEqual(body, as_bytes(self.msg_body))
     self.channel.basic_ack(method.delivery_tag)
     self.stop()
Пример #8
0
 def on_message(self, channel, method, header, body):
     self.assertIsInstance(method, spec.Basic.Deliver)
     self.assertEqual(body, as_bytes(self.msg_body))
     self.channel.basic_ack(method.delivery_tag)
     self.channel.basic_cancel(self.on_cancelled, self.ctag)