Skip to content

Commit

Permalink
Compatible headers structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dreid committed Jun 29, 2012
1 parent 7b411a6 commit afaf826
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions treq/response.py
@@ -1,8 +1,13 @@
import json import json


import cgi

from twisted.internet.defer import succeed, Deferred from twisted.internet.defer import succeed, Deferred
from twisted.internet.protocol import Protocol from twisted.internet.protocol import Protocol


from requests.structures import CaseInsensitiveDict
from requests.utils import get_encoding_from_headers



class _BodyCollector(Protocol): class _BodyCollector(Protocol):
def __init__(self, finished): def __init__(self, finished):
Expand All @@ -26,7 +31,10 @@ def __init__(self, response, method):
self._response = response self._response = response
self._method = method self._method = method
self.status_code = response.code self.status_code = response.code
self.headers = response.headers self.headers = CaseInsensitiveDict((
(header, ', '.join(values)) for header, values in
response.headers.getAllRawHeaders()))
self.encoding = get_encoding_from_headers(self.headers)


@property @property
def content(self): def content(self):
Expand Down Expand Up @@ -60,15 +68,17 @@ def _json_decode(data):
if self._json is not None: if self._json is not None:
return succeed(self._json) return succeed(self._json)


if self.headers.getRawHeaders('content-type') != ['application/json']: content_type, params = cgi.parse_header(self.headers['content-type'])

if content_type != 'application/json':
return None return None


return self.content.addCallback(_json_decode) return self.content.addCallback(_json_decode)


@property @property
def text(self): def text(self):
def _text_decode(data): def _text_decode(data):
self._text = data.decode('utf-8') self._text = data.decode(self.encoding)
return self._text return self._text


if self._text is not None: if self._text is not None:
Expand Down

0 comments on commit afaf826

Please sign in to comment.