Skip to content

Commit

Permalink
Request: code=CONTENT is now default for response messages
Browse files Browse the repository at this point in the history
This should make writing servers less convoluted.
  • Loading branch information
chrysn committed Nov 17, 2016
1 parent d0517c2 commit 684ccdd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions aiocoap/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,8 @@ def dispatch_request(self, initial_block):
self.log.error("An exception occurred while rendering a resource: %r"%e)
self.log.exception(e)
else:
if response.code is None:
response.code = CONTENT
if not response.code.is_response():
self.log.warning("Response does not carry response code (%r), application probably violates protocol."%response.code)

Expand Down
6 changes: 3 additions & 3 deletions examples/aiocoap-fileserver
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FileServer(Resource):

async def render_get(self, request):
if request.opt.uri_path == ('.well-known', 'core'):
m = aiocoap.Message(code=codes.CONTENT, payload=b"</>;ct=40")
m = aiocoap.Message(payload=b"</>;ct=40")
m.opt.content_format = 40
return m

Expand All @@ -76,7 +76,7 @@ class FileServer(Resource):
response += "<%s/>;ct=40,"%f
else:
response += "<%s>,"%f
m = aiocoap.Message(code=codes.CONTENT, payload=response[:-1].encode('utf8'))
m = aiocoap.Message(payload=response[:-1].encode('utf8'))
m.opt.content_format = 40
return m

Expand All @@ -90,7 +90,7 @@ class FileServer(Resource):
guessed_type, _ = mimetypes.guess_type(str(path))

block_out = aiocoap.optiontypes.BlockOption.BlockwiseTuple(block_in.block_number, len(data) > block_in.size, block_in.size_exponent)
response = aiocoap.Message(code=codes.CONTENT, payload=data[:block_in.size])
response = aiocoap.Message(payload=data[:block_in.size])
response.opt.block2 = block_out
response.opt.content_format = aiocoap.numbers.media_types_rev.get(guessed_type, 0 if guessed_type is not None and guessed_type.startswith('text/') else 42)
return response
Expand Down
10 changes: 5 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def __init__(self):
"transfer.\n" + "0123456789\n" * 100).encode("ascii")

async def render_get(self, request):
response = aiocoap.Message(code=aiocoap.CONTENT, payload=self.content)
response = aiocoap.Message(payload=self.content)
return response

async def render_put(self, request):
print('PUT payload: %s' % request.payload)
self.content = request.payload
payload = ("I've accepted the new payload. You may inspect it here in "\
"Python's repr format:\n\n%r"%self.content).encode('utf8')
return aiocoap.Message(code=aiocoap.CHANGED, payload=payload)
return aiocoap.Message(payload=payload)


class SeparateLargeResource(resource.Resource):
Expand All @@ -59,7 +59,7 @@ async def render_get(self, request):
"for dwarven lords in their halls of stone, nine rings for"\
"mortal men doomed to die, one ring for the dark lord on his"\
"dark throne.".encode('ascii')
return aiocoap.Message(code=aiocoap.CONTENT, payload=payload)
return aiocoap.Message(payload=payload)

class TimeResource(resource.ObservableResource):
"""
Expand All @@ -84,7 +84,7 @@ def update_observation_count(self, count):

async def render_get(self, request):
payload = datetime.datetime.now().strftime("%Y-%m-%d %H:%M").encode('ascii')
return aiocoap.Message(code=aiocoap.CONTENT, payload=payload)
return aiocoap.Message(payload=payload)

#class CoreResource(resource.Resource):
# """
Expand All @@ -103,7 +103,7 @@ async def render_get(self, request):
# data = []
# self.root.generate_resource_list(data, "")
# payload = ",".join(data).encode('utf-8')
# response = aiocoap.Message(code=aiocoap.CONTENT, payload=payload)
# response = aiocoap.Message(payload=payload)
# response.opt.content_format = 40
# return response

Expand Down

0 comments on commit 684ccdd

Please sign in to comment.