Browse Source

Fix encoding issues in HTTP chunked responses

tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
02db87eee6
2 changed files with 7 additions and 6 deletions
  1. +2
    -2
      nilmdb/client/httpclient.py
  2. +5
    -4
      nilmdb/server/server.py

+ 2
- 2
nilmdb/client/httpclient.py View File

@@ -165,11 +165,11 @@ class HTTPClient(object):
yield chunk
elif isjson:
for line in lines(response.iter_content(chunk_size = 1),
ending = '\r\n'):
ending = b'\r\n'):
yield json.loads(line)
else:
for line in lines(response.iter_content(chunk_size = 65536),
ending = '\n'):
ending = b'\n'):
yield line

def get_gen(self, url, params = None, binary = False):


+ 5
- 4
nilmdb/server/server.py View File

@@ -285,7 +285,8 @@ class Stream(NilmApp):
# Note: disable chunked responses to see tracebacks from here.
while True:
(removed, restart) = self.db.stream_remove(path, start, end)
yield json.dumps(removed) + "\r\n"
response = json.dumps(removed) + "\r\n"
yield response.encode('utf-8')
if restart is None:
break
start = restart
@@ -326,7 +327,7 @@ class Stream(NilmApp):
(ints, restart) = self.db.stream_intervals(path, start, end,
diffpath)
response = ''.join([ json.dumps(i) + "\r\n" for i in ints ])
yield response
yield response.encode('utf-8')
if restart is None:
break
start = restart
@@ -377,7 +378,7 @@ class Stream(NilmApp):
if count:
matched = self.db.stream_extract(path, start, end,
count = True)
yield sprintf("%d\n", matched)
yield sprintf(b"%d\n", matched)
return

while True:
@@ -397,7 +398,7 @@ class Exiter(object):
def index(self):
cherrypy.response.headers['Content-Type'] = 'text/plain'
def content():
yield 'Exiting by request'
yield b'Exiting by request'
raise SystemExit
return content()
index._cp_config = { 'response.stream': True }


Loading…
Cancel
Save