summaryrefslogtreecommitdiff
path: root/cherrypy/lib/jsontools.py
diff options
context:
space:
mode:
authorRobert Brewer <fumanchu@aminus.org>2009-08-23 22:29:35 +0000
committerRobert Brewer <fumanchu@aminus.org>2009-08-23 22:29:35 +0000
commit8ffb957a42bc6b3b1c6a84df2ec4b8bb26b8f7a8 (patch)
treefec315069027760d1d207ccd09201621f753afc2 /cherrypy/lib/jsontools.py
parent88943e429be4cd76f1ea39d0b9f7b0801c15cc49 (diff)
downloadcherrypy-git-8ffb957a42bc6b3b1c6a84df2ec4b8bb26b8f7a8.tar.gz
Switched the json_in tool to take advantage of the new body.processors.
Diffstat (limited to 'cherrypy/lib/jsontools.py')
-rw-r--r--cherrypy/lib/jsontools.py43
1 files changed, 25 insertions, 18 deletions
diff --git a/cherrypy/lib/jsontools.py b/cherrypy/lib/jsontools.py
index b867a9e6..fa66c9c9 100644
--- a/cherrypy/lib/jsontools.py
+++ b/cherrypy/lib/jsontools.py
@@ -19,30 +19,37 @@ else:
json_decode = json.JSONDecoder().decode
json_encode = json.JSONEncoder().iterencode
-def json_in(*args, **kwargs):
+def json_in(force=True, debug=False):
request = cherrypy.serving.request
- _h = request.headers
- if ('Content-Type' not in _h
- or _h.elements('Content-Type')[0].value != 'application/json'):
- raise cherrypy.HTTPError(415, 'Expected an application/json content type')
-
- length = int(request.headers['Content-Length'])
- body = request.body.read(length)
-
- try:
- json = json_decode(body)
- except ValueError:
- raise cherrypy.HTTPError(400, 'Invalid JSON document')
-
- request.json = json
-
-def json_out(*args, **kwargs):
+ def json_processor(entity):
+ """Read application/json data into request.json."""
+ if not entity.headers.get(u"Content-Length", u""):
+ raise cherrypy.HTTPError(411)
+
+ body = entity.fp.read()
+ try:
+ request.json = json_decode(body)
+ except ValueError:
+ raise cherrypy.HTTPError(400, 'Invalid JSON document')
+ if force:
+ request.body.processors.clear()
+ def default_proc():
+ # Leave the fp alone for someone else to read. This works fine
+ # for request.body, but the Part subclasses need to override this
+ # so they can move on to the next part.
+ raise cherrypy.HTTPError(
+ 415, 'Expected an application/json content type')
+ request.body.default_proc = default_proc
+ request.body.processors[u'application/json'] = json_processor
+
+def json_out(debug=False):
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
real_handler = request.handler
def json_handler(*args, **kwargs):
response.headers['Content-Type'] = 'application/json'
value = real_handler(*args, **kwargs)
return json_encode(value)
request.handler = json_handler
+