Merge pull request #37 from hubertmis/add-option-size2

Add Size2 option handling according to RFC7959
This commit is contained in:
Giacomo Tanganelli 2021-01-17 12:08:03 +01:00 committed by GitHub
commit f6a3c25cde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View file

@ -125,7 +125,7 @@ class OptionRegistry(object):
LOCATION_QUERY = OptionItem(20,"Location-Query",STRING, True, None) LOCATION_QUERY = OptionItem(20,"Location-Query",STRING, True, None)
BLOCK2 = OptionItem(23, "Block2", INTEGER, False, None) BLOCK2 = OptionItem(23, "Block2", INTEGER, False, None)
BLOCK1 = OptionItem(27, "Block1", INTEGER, False, None) BLOCK1 = OptionItem(27, "Block1", INTEGER, False, None)
SIZE2 = OptionItem(28, "Size2", INTEGER, False, None) SIZE2 = OptionItem(28, "Size2", INTEGER, False, 0)
PROXY_URI = OptionItem(35, "Proxy-Uri", STRING, False, None) PROXY_URI = OptionItem(35, "Proxy-Uri", STRING, False, None)
PROXY_SCHEME = OptionItem(39, "Proxy-Schema", STRING, False, None) PROXY_SCHEME = OptionItem(39, "Proxy-Schema", STRING, False, None)
SIZE1 = OptionItem(60, "Size1", INTEGER, False, None) SIZE1 = OptionItem(60, "Size1", INTEGER, False, None)

View file

@ -233,6 +233,7 @@ class BlockLayer(object):
self._block2_receive[key_token] = BlockItem(byte, num, m, size) self._block2_receive[key_token] = BlockItem(byte, num, m, size)
# correct m # correct m
m = 0 if ((num * size) + size) > len(transaction.response.payload) else 1 m = 0 if ((num * size) + size) > len(transaction.response.payload) else 1
# add size2 if requested or if payload is bigger than one datagram # add size2 if requested or if payload is bigger than one datagram
@ -240,6 +241,7 @@ class BlockLayer(object):
if (transaction.request.size2 is not None and transaction.request.size2 == 0) or \ if (transaction.request.size2 is not None and transaction.request.size2 == 0) or \
(transaction.response.payload is not None and len(transaction.response.payload) > defines.MAX_PAYLOAD): (transaction.response.payload is not None and len(transaction.response.payload) > defines.MAX_PAYLOAD):
transaction.response.size2 = len(transaction.response.payload) transaction.response.size2 = len(transaction.response.payload)
transaction.response.payload = transaction.response.payload[byte:byte + size] transaction.response.payload = transaction.response.payload[byte:byte + size]
del transaction.response.block2 del transaction.response.block2
transaction.response.block2 = (num, m, size) transaction.response.block2 = (num, m, size)

View file

@ -645,8 +645,7 @@ class Message(object):
Delete the Block2 option. Delete the Block2 option.
""" """
self.del_option_by_number(defines.OptionRegistry.BLOCK2.number) self.del_option_by_number(defines.OptionRegistry.BLOCK2.number)
@property
def size1(self): def size1(self):
value = None value = None
for option in self.options: for option in self.options:
@ -667,14 +666,24 @@ class Message(object):
@property @property
def size2(self): def size2(self):
"""
Get the Size2 option.
:return: the Size2 value
"""
value = None value = None
for option in self.options: for option in self.options:
if option.number == defines.OptionRegistry.SIZE2.number: if option.number == defines.OptionRegistry.SIZE2.number:
value = option.value if option.value is not None else 0 value = option.value
return value return value
@size2.setter @size2.setter
def size2(self, value): def size2(self, value):
"""
Set the Size2 option.
:param value: the Block2 value
"""
option = Option() option = Option()
option.number = defines.OptionRegistry.SIZE2.number option.number = defines.OptionRegistry.SIZE2.number
option.value = value option.value = value
@ -682,6 +691,9 @@ class Message(object):
@size2.deleter @size2.deleter
def size2(self): def size2(self):
"""
Delete the Size2 option.
"""
self.del_option_by_number(defines.OptionRegistry.SIZE2.number) self.del_option_by_number(defines.OptionRegistry.SIZE2.number)
@property @property