Skip to content

Commit

Permalink
Properly handle UTF strings restful_resource_handler()
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
belyalov committed Jul 28, 2019
1 parent a2a29b0 commit 0b76fc1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ class ResourceGenerator():
async def get(self, data):
yield 'longlongchunkchunk1'
yield 'chunk2'
# unicode support
yield '\u265E'


class ResourceNegative():
Expand Down Expand Up @@ -723,6 +725,10 @@ def testGenerator(self):
'6\r\n',
'chunk2',
'\r\n',
# next chunk is 1 char len UTF-8 string
'3\r\n',
'\u265E',
'\r\n',
'0\r\n\r\n']
self.assertEqual(wrt.history, exp)

Expand Down
3 changes: 2 additions & 1 deletion tinyweb/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ async def restful_resource_handler(req, resp, param=None):
await resp._send_headers()
# Drain generator
for chunk in res:
await resp.send('{:x}\r\n'.format(len(chunk)))
chunk_len = len(chunk.encode('utf-8'))
await resp.send('{:x}\r\n'.format(chunk_len))
await resp.send(chunk)
await resp.send('\r\n')
gc.collect()
Expand Down

0 comments on commit 0b76fc1

Please sign in to comment.