From 384baea03898d5fa39e8fd4c35e7c631cb06d586 Mon Sep 17 00:00:00 2001 From: Jarrod Johnon Date: Mon, 19 Jan 2015 14:45:24 -0500 Subject: [PATCH] Handle non-ascii unicode better Clarify that the data is in UTF-8 where applicable. It is expected that clients are capable of handling UTF-8 for now. Additionally, the HTML api explorer handling of numeric data is fixed. --- confluent_client/bin/confetty | 3 ++- confluent_common/confluent/tlvdata.py | 3 ++- confluent_server/confluent/httpapi.py | 5 +++-- confluent_server/confluent/main.py | 1 + confluent_server/confluent/messages.py | 16 +++++++++------- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/confluent_client/bin/confetty b/confluent_client/bin/confetty index 9751e001..fd3f69b0 100755 --- a/confluent_client/bin/confetty +++ b/confluent_client/bin/confetty @@ -2,6 +2,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -108,7 +109,7 @@ def updatestatus(stateinfo={}): def recurse_format(datum, levels=0): ret = '' import json - return json.dumps(datum, indent=1) + return json.dumps(datum, ensure_ascii=False, indent=1) if isinstance(datum, dict): for key in datum.iterkeys(): if datum[key] is None: diff --git a/confluent_common/confluent/tlvdata.py b/confluent_common/confluent/tlvdata.py index 74582c27..c67453cd 100644 --- a/confluent_common/confluent/tlvdata.py +++ b/confluent_common/confluent/tlvdata.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -32,7 +33,7 @@ def send(handle, data): handle.sendall(data) elif isinstance(data, dict): # JSON currently only goes to 4 bytes # Some structured message, like what would be seen in http responses - sdata = json.dumps(data, separators=(',', ':')) + sdata = json.dumps(data, ensure_ascii=False, separators=(',', ':')) tl = len(sdata) if tl > 16777215: raise Exception("JSON data exceeds protocol limits") diff --git a/confluent_server/confluent/httpapi.py b/confluent_server/confluent/httpapi.py index f826f722..da5eeb6b 100644 --- a/confluent_server/confluent/httpapi.py +++ b/confluent_server/confluent/httpapi.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -395,7 +396,7 @@ def resourcehandler_backend(env, start_response): yield '501 Not Implemented' def _assemble_html(responses, resource, querydict, url, extension): - yield '' \ + yield '<html><head><meta charset="UTF-8"><title>' \ 'Confluent REST Explorer: ' + url + '' \ '
' @@ -486,7 +487,7 @@ def _assemble_json(responses, resource, url, extension): else: rspdata[dk] = rsp[dk] rspdata["_links"] = links - yield json.dumps(rspdata, sort_keys=True, indent=4) + yield json.dumps(rspdata, sort_keys=True, indent=4, ensure_ascii=False) def serve(): diff --git a/confluent_server/confluent/main.py b/confluent_server/confluent/main.py index 54fa4c68..b03f639d 100644 --- a/confluent_server/confluent/main.py +++ b/confluent_server/confluent/main.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/confluent_server/confluent/messages.py b/confluent_server/confluent/messages.py index 55e356f1..3b631d8e 100644 --- a/confluent_server/confluent/messages.py +++ b/confluent_server/confluent/messages.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2014 IBM Corporation +# Copyright 2015 Lenovo # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -26,16 +27,17 @@ def _htmlify_structure(indict): if isinstance(indict, dict): for key in indict.iterkeys(): ret += "
  • {0}: ".format(key) - if type(indict[key]) in (str, unicode): - ret += indict[key] + if type(indict[key]) in (str, unicode, float, int): + ret += str(indict[key]) else: ret += _htmlify_structure(indict[key]) elif isinstance(indict, list): - if type(indict[0]) in (str, unicode): - ret += ",".join(indict) - else: - for v in indict: - ret += _htmlify_structure(v) + if len(indict) > 0: + if type(indict[0]) in (str, unicode): + ret += ",".join(indict) + else: + for v in indict: + ret += _htmlify_structure(v) return ret + ''