def post(self): habrachat_cookie = self.get_cookie("habrachat") if not habrachat_cookie: habrachat_cookie = _session_id() self.set_cookie("habrachat", habrachat_cookie) token = self.get_argument("token", None) if not token: log.warning("Not have Token") self.finish() return client = httpclient.AsyncHTTPClient() response = yield client.fetch( "http://u-login.com/token.php?token=%s&host=%s://%s" % (token, self.request.protocol, self.request.host), use_gzip=True ) if response.code != 200: log.warning("Not have access to u-login") self.finish() return json_response = json_decode(response.body) if "error_type" in json_response: log.warning("Error auth: %s" % json_response["error_message"]) self.finish() return json_response = json_decode(response.body) if "error" in json_response: log.warning("Error auth: %s" % json_response["error"]) self.finish() return identity = json_response.get("identity") if not identity: log.error("Not have indentity! json: %s" % json_response) log.info("New user indetity: %s" % identity) user_id = hashlib.md5(utf8(identity)).hexdigest() new_user = {"id": user_id, "name": None} new_user_name = "" if "nickname" in json_response: new_user_name = json_response.get("nickname", "") if not new_user["name"] and "first_name" in json_response: new_user_name = json_response.get("first_name", "") new_user["name"] = new_user_name[:20].replace("[", "{").replace("]", "}").encode('UTF-8') new_user["avatar"] = json_response.get("photo") new_user["ismoderator"] = identity in options.moderators old_user_settings = yield tornado.gen.Task(self.redis.get, "setting_"+user_id) if not old_user_settings: new_user_settings = { "revert_chat_order": False, "send_message_enter": False } yield tornado.gen.Task(self.redis.set, "setting_"+user_id, json_encode(recursive_unicode(new_user_settings))) yield tornado.gen.Task(self.redis.set, habrachat_cookie, json_encode(recursive_unicode(new_user))) self.redirect("/")
def _assertJsonStructuresEqualsRecursive(self, a, b, path, msg): a_type = unicode if type(a) == str else type(a) b_type = unicode if type(b) == str else type(b) self.assertEqual( a_type, b_type, self._format_msg_and_path('Types are not equal: {} != {}'.format(type(a), type(b)), msg, path) ) if isinstance(a, list): self.assertEqual( len(a), len(b), self._format_msg_and_path('Lists lengths are not equal: {} != {}'.format(len(a), len(b)), msg, path) ) for i in xrange(len(a)): self._assertJsonStructuresEqualsRecursive(a[i], b[i], path + '[{}]'.format(i), msg) elif isinstance(a, dict): a_keys = sorted(a.keys()) b_keys = sorted(b.keys()) self.assertEqual( a_keys, b_keys, self._format_msg_and_path('Dict keys are not equal: {} != {}'.format(a_keys, b_keys), msg, path) ) for key in a_keys: self._assertJsonStructuresEqualsRecursive(a[key], b[key], '.'.join(filter(None, (path, key))), msg) else: self.assertEqual( recursive_unicode(a), recursive_unicode(b), self._format_msg_and_path('Values are not equal: {!r} != {!r}'.format(a, b), msg, path) )
def get(self): if self.get_argument('code', False): token = yield self.get_authenticated_user( redirect_uri='%s://%s/google_auth' % (self.request.protocol, self.request.host), code=self.get_argument('code')) client = httpclient.AsyncHTTPClient() response = yield client.fetch( "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=%s" % (token["access_token"]), use_gzip=True ) if response.code != 200: log.warning("Not have access to google") self.finish() return user = json_decode(response.body) habrachat_cookie = self.get_cookie("habrachat") if not habrachat_cookie: habrachat_cookie = _session_id() self.set_cookie("habrachat", habrachat_cookie) identity = user.get("link") if not identity: log.error("Not have indentity! json: %s" % user) log.info("New user indetity: %s" % identity) user_id = hashlib.md5(utf8(identity)).hexdigest() new_user = {"id": user_id, "name": None} if "username" in user: new_user["name"] = user.get("username").encode('UTF-8') if not new_user["name"] and "name" in user: new_user["name"] = user.get("name").encode('UTF-8') new_user["name"] = new_user["name"][:20].replace("[", "{").replace("]", "}") new_user["avatar"] = user.get("picture", "") new_user["ismoderator"] = identity in options.moderators redis_client = self.redis() old_user_settings = yield tornado.gen.Task(redis_client.get, "setting_"+user_id) if not old_user_settings: new_user_settings = { "revert_chat_order": False, "send_message_enter": False } yield tornado.gen.Task(redis_client.set, "setting_"+user_id, json_encode(recursive_unicode(new_user_settings))) yield tornado.gen.Task(redis_client.set, habrachat_cookie, json_encode(recursive_unicode(new_user))) self.redirect("/") else: yield self.authorize_redirect( redirect_uri='%s://%s/google_auth' % (self.request.protocol, self.request.host), client_id=self.settings['google_oauth']['key'], scope=['profile', 'email'], response_type='code', extra_params={'approval_prompt': 'auto'})
def test_recursive_unicode(self): tests = { 'dict': {b"foo": b"bar"}, 'list': [b"foo", b"bar"], 'tuple': (b"foo", b"bar"), 'bytes': b"foo" } self.assertEqual(recursive_unicode(tests['dict']), {u"foo": u"bar"}) self.assertEqual(recursive_unicode(tests['list']), [u"foo", u"bar"]) self.assertEqual(recursive_unicode(tests['tuple']), (u"foo", u"bar")) self.assertEqual(recursive_unicode(tests['bytes']), u"foo")
def CreateMessage(token, alert=None, badge=None, sound=None, identifier=0, expiry=None, extra=None, allow_truncate=True): token = TokenToBinary(token) if len(token) != 32: raise ValueError, u'Token must be a 32-byte binary string.' if (alert is not None) and (not isinstance(alert, (basestring, dict))): raise ValueError, u'Alert message must be a string or a dictionary.' if expiry is None: expiry = long(time.time() + 365 * 86400) # Start by determining the length of the UTF-8 encoded JSON with no alert text. This allows us to # determine how much space is left for the message. # 'content-available': 1 is necessary to trigger iOS 7's background download processing. aps = {'alert': '', 'content-available': 1} if badge is not None: aps['badge'] = badge if sound is not None: aps['sound'] = sound data = {'aps': aps} if extra is not None: data.update(extra) # Create compact JSON representation with no extra space and no escaping of non-ascii chars (i.e. use # direct UTF-8 representation rather than "\u1234" escaping). This maximizes the amount of space that's # left for the alert text. encoded = escape.utf8( json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False)) bytes_left = _MAX_PAYLOAD_BYTES - len(encoded) if allow_truncate and isinstance(alert, basestring): alert = _TruncateAlert(alert, bytes_left) elif alert and len(escape.utf8(alert)) > bytes_left: raise ValueError, u'max payload(%d) exceeded: %d' % ( _MAX_PAYLOAD_BYTES, len(escape.utf8(alert))) # Now re-encode including the alert text. aps['alert'] = alert encoded = escape.utf8( json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False)) length = len(encoded) assert length <= _MAX_PAYLOAD_BYTES, (encoded, length) return struct.pack('!bIIH32sH%(length)ds' % {'length': length}, 1, identifier, expiry, 32, token, length, encoded)
def test_recursive_unicode(self): tests = { "dict": {b"foo": b"bar"}, "list": [b"foo", b"bar"], "tuple": (b"foo", b"bar"), "bytes": b"foo", } self.assertEqual(recursive_unicode(tests["dict"]), {u"foo": u"bar"}) self.assertEqual(recursive_unicode(tests["list"]), [u"foo", u"bar"]) self.assertEqual(recursive_unicode(tests["tuple"]), (u"foo", u"bar")) self.assertEqual(recursive_unicode(tests["bytes"]), u"foo")
def test_recursive_unicode(self): tests = { "dict": { b"foo": b"bar" }, "list": [b"foo", b"bar"], "tuple": (b"foo", b"bar"), "bytes": b"foo", } self.assertEqual(recursive_unicode(tests["dict"]), {u"foo": u"bar"}) self.assertEqual(recursive_unicode(tests["list"]), [u"foo", u"bar"]) self.assertEqual(recursive_unicode(tests["tuple"]), (u"foo", u"bar")) self.assertEqual(recursive_unicode(tests["bytes"]), u"foo")
def tornado_log_function(handler): """Assigned when creating a :py:class:`tornado.web.Application` instance by passing the method as the ``log_function`` argument: .. code:: python app = tornado.web.Application([('/', RequestHandler)], log_function=tornado_log_function) :type handler: :py:class:`tornado.web.RequestHandler` """ status_code = handler.get_status() if status_code < 400: log_method = log.access_log.info elif status_code < 500: log_method = log.access_log.warning else: log_method = log.access_log.error correlation_id = (getattr(handler, 'correlation_id', None) or handler.request.headers.get('Correlation-ID', None)) log_method( '', { 'correlation_id': correlation_id, 'duration': 1000.0 * handler.request.request_time(), 'headers': dict(handler.request.headers), 'method': handler.request.method, 'path': handler.request.path, 'protocol': handler.request.protocol, 'query_args': escape.recursive_unicode( handler.request.query_arguments), 'remote_ip': handler.request.remote_ip, 'status_code': status_code, 'environment': os.environ.get('ENVIRONMENT') })
def add_client(self, client_id, wsconn): """ 添加client_ws_conn 1. 删除pending中的client 2. 在room中实际添加连接 3. 在client中实际添加信息 4. 向房间所有人广播信息,同时更新房间人数列表 :param client_id: :param wsconn: :return: """ with rlock: # 在临时中取出client信息,并添加到clients_info中 client_info = self.temp_conns.pop(client_id) client_info['conn'] = wsconn self.clients_info[client_id] = client_info # 在room中添加上连接信息 self.rooms_info[client_info['room']].append( dict(nick=client_info['nick'], conn=wsconn, client_id=client_id)) # 用户列表放入redis缓存中 room_md5 = hashlib.md5( client_info['room'].encode('utf-8')).hexdigest() self.redis.rpush(room_md5, client_info['nick']) nicks = recursive_unicode( self.redis.lrange(room_md5, 0, self.redis.llen(room_md5))) return { "room": client_info['room'], "nick": client_info['nick'], "nicks": nicks }
def validate_arguments(self, schema=None, **kwargs): if schema == None: schema = getattr(self, '__arguments_schema__', None) if schema == None: raise Exception('missing validation schema') return self._validate(escape.recursive_unicode(self.request.arguments), schema, **kwargs)
def on_finish(self): logger = logging.getLogger('request') ctx = { 'time_served': self.request.request_time(), 'http_user_agent': self.request.headers.get('User-Agent', ''), 'remote_ip': self.request.remote_ip, 'method': self.request.method, 'path': self.request.path, 'arguments': { k: v[0] if len(v) == 1 else v for k, v in recursive_unicode(self.request.arguments).items() }, 'current_uid': self.current_uid, } if g.DEBUG: if self.request.headers.get('Content-Type', '').startswith('application/json'): try: json_args = json_decode(self.request.body) except Exception: json_args = {} ctx['json_args'] = json_args log_method = logger.info log_method(json.dumps(ctx))
def _TestTruncate(alert, max_bytes, expected): truncated = _TruncateAlert(alert, max_bytes) truncated_json = escape.utf8( json.dumps(escape.recursive_unicode(truncated), ensure_ascii=False)[1:-1]) self.assertEqual(truncated_json, expected) self.assertTrue(len(truncated_json) <= max_bytes)
def get(self): global id p = user.userbase_reader.get_proxy(id)#.ice_ping() p.ice_ping() print os.getpid(), id id += 1 self.set_header("Content-Type", "text/html; charset=UTF-8") self.write('%s' % recursive_unicode(self.request.arguments))
def json_encode(value, **kwargs): """JSON-encodes the given Python object.""" # JSON permits but does not require forward slashes to be escaped. # This is useful when json data is emitted in a <script> tag # in HTML, as it prevents </script> tags from prematurely terminating # the javscript. Some json libraries do this escaping by default, # although python's standard library does not, so we do it here. # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped return json.dumps(recursive_unicode(value), **kwargs).replace("</", "<\\/")
def unpack_bytes(self, obj_bytes, encoding=None): """Unpack a byte stream into a dictionary.""" assert self.bytes_to_dict or self.string_to_dict encoding = encoding or self.default_encoding LOGGER.debug('%r decoding %d bytes with encoding of %s', self, len(obj_bytes), encoding) if self.bytes_to_dict: return escape.recursive_unicode(self.bytes_to_dict(obj_bytes)) return self.string_to_dict(obj_bytes.decode(encoding))
def add_room(self, room, nick): """ 添加Room: 1. 校验room相关信息 2. 初始化room队列,并在pending中增加room信息,待到实际连接时再添加到room中 3. 返回client_id,用于前台使用 :param room: 房间名字 :param nick: 用户名字 :return: client_id """ #: 校验房间和用户昵称的规则 roomvalid = re.match(r'[\w-]+$', room) nickvalid = re.match(r'[\w-]+$', nick) if roomvalid == None: raise RuntimeError( "The room name provided was invalid. It can only contain letters, numbers, - and _.\nPlease try again." ) if nickvalid == None: raise RuntimeError( "The nickname provided was invalid. It can only contain letters, numbers, - and _.\nPlease try again." ) # 校验是否满足需求(数量限制,名字限制) with rlock: #: 校验是否单台服务器已经超限 if len(self.rooms_info) > settings.MAX_ROOMS: raise RuntimeError( "The maximum number of rooms (%d) has been reached.\n\nPlease try again later." % settings.MAX_ROOMS) #: 校验是否但房间已经超限 if room in self.rooms_info and len( self.rooms_info[room]) >= settings.MAX_USERS_PER_ROOM: raise RuntimeError( "The maximum number of users in this room (%d) has been reached.\n\nPlease try again later." % settings.MAX_USERS_PER_ROOM) # 添加到房间中 client_id = str(uuid.uuid4().int) if not self.rooms_info.get(room): self.rooms_info[room] = [] # 验证nick是否已经重复, 应该从redis中获取用户昵称来验证 # nicks = list(map(lambda x: x['nick'], self.rooms_info[room])) room_md5 = hashlib.md5(room.encode('utf-8')).hexdigest() nicks = recursive_unicode( self.redis.lrange(room_md5, 0, self.redis.llen(room_md5))) suffix = 1 while True: if nick in nicks: nick += str(1) else: break suffix += 1 # 将客户端的信息保存至temp_conns中 self.temp_conns[client_id] = dict(room=room, nick=nick) return client_id
def json_encode(value: JSONType, indent: Optional[Union[int, str]] = None) -> str: """JSON-encodes the given Python object.""" string = json.dumps( recursive_unicode(value), default=objToJSON, separators=(",", ":"), indent=indent, ) return string.replace("</", "<\\/")
def CreateMessage(token, alert=None, badge=None, sound=None, identifier=0, expiry=None, extra=None, allow_truncate=True): token = TokenToBinary(token) if len(token) != 32: raise ValueError, u'Token must be a 32-byte binary string.' if (alert is not None) and (not isinstance(alert, (basestring, dict))): raise ValueError, u'Alert message must be a string or a dictionary.' if expiry is None: expiry = long(time.time() + 365 * 86400) # Start by determining the length of the UTF-8 encoded JSON with no alert text. This allows us to # determine how much space is left for the message. # 'content-available': 1 is necessary to trigger iOS 7's background download processing. aps = { 'alert' : '', 'content-available': 1 } if badge is not None: aps['badge'] = badge if sound is not None: aps['sound'] = sound data = { 'aps' : aps } if extra is not None: data.update(extra) # Create compact JSON representation with no extra space and no escaping of non-ascii chars (i.e. use # direct UTF-8 representation rather than "\u1234" escaping). This maximizes the amount of space that's # left for the alert text. encoded = escape.utf8(json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False)) bytes_left = _MAX_PAYLOAD_BYTES - len(encoded) if allow_truncate and isinstance(alert, basestring): alert = _TruncateAlert(alert, bytes_left) elif alert and len(escape.utf8(alert)) > bytes_left: raise ValueError, u'max payload(%d) exceeded: %d' % (_MAX_PAYLOAD_BYTES, len(escape.utf8(alert))) # Now re-encode including the alert text. aps['alert'] = alert encoded = escape.utf8(json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False)) length = len(encoded) assert length <= _MAX_PAYLOAD_BYTES, (encoded, length) return struct.pack('!bIIH32sH%(length)ds' % { 'length' : length }, 1, identifier, expiry, 32, token, length, encoded)
def _assertJsonStructuresEqualsRecursive(self, a, b, path, msg): a_type = unicode if type(a) == str else type(a) b_type = unicode if type(b) == str else type(b) self.assertEqual( a_type, b_type, self._format_msg_and_path( 'Types are not equal: {} != {}'.format(type(a), type(b)), msg, path)) if isinstance(a, list): self.assertEqual( len(a), len(b), self._format_msg_and_path( 'Lists lengths are not equal: {} != {}'.format( len(a), len(b)), msg, path)) for i in xrange(len(a)): self._assertJsonStructuresEqualsRecursive( a[i], b[i], path + '[{}]'.format(i), msg) elif isinstance(a, dict): a_keys = sorted(a.keys()) b_keys = sorted(b.keys()) self.assertEqual( a_keys, b_keys, self._format_msg_and_path( 'Dict keys are not equal: {} != {}'.format(a_keys, b_keys), msg, path)) for key in a_keys: self._assertJsonStructuresEqualsRecursive( a[key], b[key], '.'.join(filter(None, (path, key))), msg) else: self.assertEqual( recursive_unicode(a), recursive_unicode(b), self._format_msg_and_path( 'Values are not equal: {!r} != {!r}'.format(a, b), msg, path))
def get(self, path): # Type checks: web.py interfaces convert argument values to # unicode strings (by default, but see also decode_argument). # In httpserver.py (i.e. self.request.arguments), they're left # as bytes. Keys are always native strings. for key in self.request.arguments: assert type(key) == str, repr(key) for value in self.request.arguments[key]: assert type(value) == bytes_type, repr(value) for value in self.get_arguments(key): assert type(value) == str, repr(value) assert type(path) == str, repr(path) self.write(dict(path=path, args=recursive_unicode(self.request.arguments)))
def get(self, path): # Type checks: web.py interfaces convert argument values to # unicode strings (by default, but see also decode_argument). # In httpserver.py (i.e. self.request.arguments), they're left # as bytes. Keys are always native strings. for key in self.request.arguments: assert type(key) == str, repr(key) for value in self.request.arguments[key]: assert type(value) == bytes_type, repr(value) for value in self.get_arguments(key): assert type(value) == unicode, repr(value) assert type(path) == unicode, repr(path) self.write( dict(path=path, args=recursive_unicode(self.request.arguments)))
def to_bytes(self, inst_data, encoding=None): """ Transform an object into :class:`bytes`. :param object inst_data: object to encode :param str encoding: character set used to encode the bytes returned from the ``dumps`` function. This defaults to :attr:`default_encoding` :returns: :class:`tuple` of the selected content type and the :class:`bytes` representation of `inst_data` """ selected = encoding or self.default_encoding content_type = '{0}; charset="{1}"'.format(self.content_type, selected) dumped = self._dumps(escape.recursive_unicode(inst_data)) return content_type, dumped.encode(selected)
def get(self, *path_args): # Type checks: web.py interfaces convert argument values to # unicode strings (by default, but see also decode_argument). # In httpserver.py (i.e. self.request.arguments), they're left # as bytes. Keys are always native strings. for key in self.request.arguments: if type(key) != str: raise Exception("incorrect type for key: %r" % type(key)) for value in self.request.arguments[key]: if type(value) != bytes_type: raise Exception("incorrect type for value: %r" % type(value)) for value in self.get_arguments(key): if type(value) != unicode: raise Exception("incorrect type for value: %r" % type(value)) for arg in path_args: if type(arg) != unicode: raise Exception("incorrect type for path arg: %r" % type(arg)) self.write(dict(path=self.request.path, path_args=path_args, args=recursive_unicode(self.request.arguments)))
def remove_client(self, client_id): """ 1. 在clients_info中移除client_id 2. 在rooms_info中移除client,若是房间里没有人了,房间一并删除 3. 通知房间内所有人。**离开房间 4. 刷新房间在线人数列表 :param client_id: 根据client_id通知信息 :return: """ # 在clients_info中移除client_id if client_id not in self.clients_info: return with rlock: client_info = self.clients_info.get(client_id) room = client_info['room'] nick = client_info['nick'] # 在rooms_info中移除room中的client room_client = list( filter(lambda x: x['client_id'] == client_id, self.rooms_info[room])) # 在redis中,将对应的用户移除 room_md5 = hashlib.md5(room.encode('utf-8')).hexdigest() self.redis.lrem(room_md5, 0, nick) nicks = recursive_unicode( self.redis.lrange(room_md5, 0, self.redis.llen(room_md5))) # 将当前client在rooms中移除 self.rooms_info[room].remove(room_client[0]) # 将client在clients中移除 del self.clients_info[client_id] # 移除room if len(self.rooms_info[room]) == 0: del self.rooms_info[room] return {"room": room, "nick": nick, "nicks": nicks}
def _TruncateAlert(alert, max_bytes): """Converts the alert text to UTF-8 encoded JSON format, which is how the alert will be stored in the APNS payload. If the number of resulting bytes exceeds "max_bytes", then truncates the alert text at a Unicode character boundary, taking care not to split JSON escape sequences. Returns the truncated UTF-8 encoded alert text, including a trailing ellipsis character. """ alert_json = escape.utf8( json.dumps(escape.recursive_unicode(alert), ensure_ascii=False)) # Strip quotes added by JSON. alert_json = alert_json[1:-1] # Check if alert fits with no truncation. if len(alert_json) <= max_bytes: return escape.utf8(alert) # Make room for an appended ellipsis. assert max_bytes >= len( _ELLIPSIS_BYTES ), 'max_bytes must be at least %d' % len(_ELLIPSIS_BYTES) max_bytes -= len(_ELLIPSIS_BYTES) # Truncate the JSON UTF8 string at a Unicode character boundary. truncated = alert_json[:max_bytes].decode('utf-8', errors='ignore') # If JSON escape sequences were split, then the truncated string may not be valid JSON. Keep # chopping trailing characters until the truncated string is valid JSON. It may take several # tries, such as in the case where a "\u1234" sequence has been split. while True: try: alert = json.loads(u'"%s"' % truncated) break except Exception: truncated = truncated[:-1] # Return the UTF-8 encoding of the alert with the ellipsis appended to it. return escape.utf8(alert) + _ELLIPSIS_BYTES
def rabbitmq_callback(ch, method, properties, body): """ @ch: channel 通道,是由外部调用时上送 out body 读取队列内容做不同的操作 """ d_item = {} try: d_item = json_decode(recursive_unicode(body)) h_type = d_item['type'] h_data = d_item['data'] if hasattr(event_handler, h_type): try: logger.info("收到[{}]请求,数据为[{}] ".format(h_type, h_data)) func = getattr(event_handler, h_type) func(h_data) except: logger.exception("处理失败 [{}]".format(d_item)) else: logger.error("暂时不支持的操作类型[{}] 操作内容[{}]".format(h_type, h_data)) except: logger.exception("解析内容失败:{}".format(d_item))
def _TruncateAlert(alert, max_bytes): """Converts the alert text to UTF-8 encoded JSON format, which is how the alert will be stored in the APNS payload. If the number of resulting bytes exceeds "max_bytes", then truncates the alert text at a Unicode character boundary, taking care not to split JSON escape sequences. Returns the truncated UTF-8 encoded alert text, including a trailing ellipsis character. """ alert_json = escape.utf8(json.dumps(escape.recursive_unicode(alert), ensure_ascii=False)) # Strip quotes added by JSON. alert_json = alert_json[1:-1] # Check if alert fits with no truncation. if len(alert_json) <= max_bytes: return escape.utf8(alert) # Make room for an appended ellipsis. assert max_bytes >= len(_ELLIPSIS_BYTES), 'max_bytes must be at least %d' % len(_ELLIPSIS_BYTES) max_bytes -= len(_ELLIPSIS_BYTES) # Truncate the JSON UTF8 string at a Unicode character boundary. truncated = alert_json[:max_bytes].decode('utf-8', errors='ignore') # If JSON escape sequences were split, then the truncated string may not be valid JSON. Keep # chopping trailing characters until the truncated string is valid JSON. It may take several # tries, such as in the case where a "\u1234" sequence has been split. while True: try: alert = json.loads(u'"%s"' % truncated) break except Exception: truncated = truncated[:-1] # Return the UTF-8 encoding of the alert with the ellipsis appended to it. return escape.utf8(alert) + _ELLIPSIS_BYTES
def post(self): self.write(recursive_unicode(self.request.arguments))
def get(self): self.write(recursive_unicode(self.request.arguments))
def _TestTruncate(alert, max_bytes, expected): truncated = _TruncateAlert(alert, max_bytes) truncated_json = escape.utf8(json.dumps(escape.recursive_unicode(truncated), ensure_ascii=False)[1:-1]) self.assertEqual(truncated_json, expected) self.assertTrue(len(truncated_json) <= max_bytes)
def json_str(value): return str( json.dumps(recursive_unicode(value), cls=CustomJSONEncoder).replace( "</", "<\\/"))
def parse_json(raw): """Parses raw bytes to a JSON object with unicode strings""" return escape.recursive_unicode( escape.json_decode(raw)) if raw != None else None
def parse_json(raw): """Parses raw bytes to a JSON object with unicode strings""" return escape.recursive_unicode(escape.json_decode(raw)) if raw != None else None
def json_str(value): return str(json.dumps(recursive_unicode(value), cls=CustomJSONEncoder).replace("</", "<\\/"))
def json_encode(value): return json.dumps(recursive_unicode(value), cls=JSONDateTimeEncoder).replace("</", "<\\/")