Skip to content

Commit

Permalink
add support for application/json as Unicode text
Browse files Browse the repository at this point in the history
  • Loading branch information
freddrake committed Mar 12, 2013
1 parent 63a8c4d commit 6836858
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/zope/publisher/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@

# not just text/* but RFC 3023 and */*+xml
import re
unicode_mimetypes_re = re.compile(r"^text\/.*$|^.*\/xml.*$|^.*\+xml$")
unicode_mimetypes_re = re.compile(
r"^text\/.*$|^.*\/xml.*$|^.*\+xml$|^application/json$")

eventlog = logging.getLogger('eventlog')

Expand Down Expand Up @@ -799,8 +800,8 @@ def _implicitResult(self, body):
if isinstance(body, unicode):
if not unicode_mimetypes_re.match(content_type):
raise ValueError(
'Unicode results must have a text, RFC 3023, or '
'+xml content type.')
'Unicode results must have a text, RFC 3023, RFC 4627,'
' or +xml content type.')

major, minor, params = zope.contenttype.parse.parse(content_type)

Expand All @@ -816,9 +817,14 @@ def _implicitResult(self, body):
encoding = 'utf-8'
body = body.encode(encoding)

params['charset'] = encoding
content_type = "%s/%s;" % (major, minor)
content_type += ";".join(k + "=" + v for k, v in params.items())
if (major, minor) != ('application', 'json'):
# The RFC says this is UTF-8, and the type has no params.
params['charset'] = encoding
content_type = "%s/%s" % (major, minor)
if params:
content_type += ";"
content_type += ";".join(k + "=" + v
for k, v in params.items())

if content_type:
headers = [('content-type', content_type),
Expand Down
5 changes: 5 additions & 0 deletions src/zope/publisher/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,11 @@ def testContentType(self):
eq("image/gif", headers["Content-Type"])
eq("test", body)

headers, body = self._getResultFromResponse(u"test", "utf-8",
{"content-type": "application/json"})
eq("application/json", headers["Content-Type"])
eq(b"test", body)

def _getCookieFromResponse(self, cookies):
# Shove the cookies through request, parse the Set-Cookie header
# and spit out a list of headers for examination
Expand Down

0 comments on commit 6836858

Please sign in to comment.