Fix some issues with encoding
This commit is contained in:
parent
585c6d703f
commit
cbfd17491f
@ -1,3 +1,16 @@
|
|||||||
|
# coding=utf8
|
||||||
|
"""
|
||||||
|
A mini-demo of what wsme can do.
|
||||||
|
|
||||||
|
To run it::
|
||||||
|
|
||||||
|
python setup.py develop
|
||||||
|
|
||||||
|
Then::
|
||||||
|
|
||||||
|
paster serve demo.cfg
|
||||||
|
"""
|
||||||
|
|
||||||
from webob.dec import wsgify
|
from webob.dec import wsgify
|
||||||
from wsme import *
|
from wsme import *
|
||||||
|
|
||||||
@ -11,6 +24,10 @@ class DemoRoot(WSRoot):
|
|||||||
def multiply(self, a, b):
|
def multiply(self, a, b):
|
||||||
return a * b
|
return a * b
|
||||||
|
|
||||||
|
@expose(unicode)
|
||||||
|
def helloworld(self):
|
||||||
|
return u"こんにちは世界 (<- Hello World in Japanese !)"
|
||||||
|
|
||||||
|
|
||||||
def app_factory(global_config, **local_conf):
|
def app_factory(global_config, **local_conf):
|
||||||
return wsgify(DemoRoot()._handle_request)
|
return wsgify(DemoRoot()._handle_request)
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import traceback
|
import traceback
|
||||||
import weakref
|
import weakref
|
||||||
|
import logging
|
||||||
|
|
||||||
from wsme import exc
|
from wsme import exc
|
||||||
from wsme.types import register_type
|
from wsme.types import register_type
|
||||||
|
|
||||||
__all__ = ['expose', 'validate', 'WSRoot']
|
__all__ = ['expose', 'validate', 'WSRoot']
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
registered_protocols = {}
|
registered_protocols = {}
|
||||||
|
|
||||||
|
|
||||||
@ -107,14 +110,20 @@ class WSRoot(object):
|
|||||||
def _format_exception(self, excinfo):
|
def _format_exception(self, excinfo):
|
||||||
"""Extract informations that can be sent to the client."""
|
"""Extract informations that can be sent to the client."""
|
||||||
if isinstance(excinfo[1], exc.ClientSideError):
|
if isinstance(excinfo[1], exc.ClientSideError):
|
||||||
return dict(faultcode="Client",
|
r = dict(faultcode="Client",
|
||||||
faultstring=unicode(excinfo[1]))
|
faultstring=unicode(excinfo[1]))
|
||||||
|
log.warning("Client-side error: %s" % r['faultstring'])
|
||||||
|
return r
|
||||||
else:
|
else:
|
||||||
r = dict(faultcode="Server",
|
faultstring = str(excinfo[1])
|
||||||
faultstring=str(excinfo[1]))
|
debuginfo = "\n".join(traceback.format_exception(*excinfo))
|
||||||
|
|
||||||
|
log.error('Server-side error: "%s". Detail: \n%s' % (
|
||||||
|
faultstring, debuginfo))
|
||||||
|
|
||||||
|
r = dict(faultcode="Server", faultstring=faultstring)
|
||||||
if self._debug:
|
if self._debug:
|
||||||
r['debuginfo'] = "Traceback:\n%s\n" % (
|
r['debuginfo'] = debuginfo
|
||||||
"\n".join(traceback.format_exception(*excinfo)))
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def _lookup_function(self, path):
|
def _lookup_function(self, path):
|
||||||
|
@ -41,7 +41,6 @@ class RestProtocol(object):
|
|||||||
res.status = "200 OK"
|
res.status = "200 OK"
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
res.status = 500
|
res.status = 500
|
||||||
res.charset = 'utf8'
|
|
||||||
res.body = self.encode_error(
|
res.body = self.encode_error(
|
||||||
root._format_exception(sys.exc_info()))
|
root._format_exception(sys.exc_info()))
|
||||||
|
|
||||||
@ -64,6 +63,6 @@ class RestProtocol(object):
|
|||||||
res_content_type = "text/html"
|
res_content_type = "text/html"
|
||||||
res.body = html_body % dict(content=res.body)
|
res.body = html_body % dict(content=res.body)
|
||||||
|
|
||||||
res.headers['Content-Type'] = res_content_type
|
res.headers['Content-Type'] = "%s; charset=UTF-8" % res_content_type
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
@ -93,6 +93,8 @@ class RestJsonProtocol(RestProtocol):
|
|||||||
content_types = ['application/json', 'text/json', '', None]
|
content_types = ['application/json', 'text/json', '', None]
|
||||||
|
|
||||||
def decode_args(self, req, arguments):
|
def decode_args(self, req, arguments):
|
||||||
|
if not req.body:
|
||||||
|
return {}
|
||||||
raw_args = json.loads(req.body)
|
raw_args = json.loads(req.body)
|
||||||
kw = {}
|
kw = {}
|
||||||
for farg in arguments:
|
for farg in arguments:
|
||||||
@ -103,9 +105,10 @@ class RestJsonProtocol(RestProtocol):
|
|||||||
return kw
|
return kw
|
||||||
|
|
||||||
def encode_result(self, result, return_type):
|
def encode_result(self, result, return_type):
|
||||||
return json.dumps({'result': tojson(return_type, result)})
|
r = tojson(return_type, result)
|
||||||
|
return json.dumps({'result': r}, ensure_ascii=False).encode('utf8')
|
||||||
|
|
||||||
def encode_error(self, errordetail):
|
def encode_error(self, errordetail):
|
||||||
return json.dumps(errordetail)
|
return json.dumps(errordetail, encoding='utf-8')
|
||||||
|
|
||||||
register_protocol(RestJsonProtocol)
|
register_protocol(RestJsonProtocol)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user