Skip to content

Commit

Permalink
Update CHANGES/CONTRIBUTORS, refactor Response's ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 26, 2016
1 parent 8e9e73e commit 3d53ba8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ CHANGES

- Fix access_log_format in `GunicornWebWorker` #1117

- Setup Content-Type to application/octet-stream by default #1124


0.22.5 (08-02-2016)
-------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Taras Voinarovskyi
Tolga Tezel
Thomas Grainger
Vaibhav Sagar
Vamsi Krishna Avula
Vasyl Baran
Vitalik Verhovodov
Vitaly Haritonsky
Expand Down
63 changes: 33 additions & 30 deletions aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,51 +772,54 @@ class Response(StreamResponse):
def __init__(self, *, body=None, status=200,
reason=None, text=None, headers=None, content_type=None,
charset=None):
super().__init__(status=status, reason=reason, headers=headers)
self.set_tcp_cork(True)

if body is not None and text is not None:
raise ValueError("body and text are not allowed together")

content_type_header = False
if headers is not None:
search_key = hdrs.CONTENT_TYPE.lower()
content_type_header = any(search_key == key.lower()
for key in headers)
if headers is None:
headers = CIMultiDict()
elif not isinstance(headers, (CIMultiDict, CIMultiDictProxy)):
headers = CIMultiDict(headers)

if content_type is not None and ";" in content_type:
raise ValueError("charset must not be in content_type "
"argument")

if text is not None:
if not content_type_header:
if hdrs.CONTENT_TYPE in headers:
if content_type or charset:
raise ValueError("passing both Content-Type header and "
"content_type or charset params "
"is forbidden")
else:
# fast path for filling headers
if not isinstance(text, str):
raise TypeError("text argument must be str (%r)" %
type(text))
if content_type is None:
content_type = 'text/plain'
elif ";" in content_type:
raise ValueError("charset must not be in content_type "
"argument")
charset = charset or 'utf-8'
self.headers[hdrs.CONTENT_TYPE] = (
content_type + '; charset=%s' % charset)
self._content_type = content_type
self._content_dict = {'charset': charset}
self.body = text.encode(charset)
else:
if content_type or charset:
raise ValueError("passing both Content-Type header and "
"content_type or charset params "
"is forbidden")
self.text = text
if charset is None:
charset = 'utf-8'
headers[hdrs.CONTENT_TYPE] = (
content_type + '; charset=' + charset)
body = text.encode(charset)
text = None
else:
if content_type_header:
if content_type or charset:
if hdrs.CONTENT_TYPE in headers:
if content_type is not None or charset is not None:
raise ValueError("passing both Content-Type header and "
"content_type or charset params "
"is forbidden")
if content_type:
self.content_type = content_type
if charset:
self.charset = charset
else:
if content_type is not None:
if charset is not None:
content_type += '; charset=' + charset
headers[hdrs.CONTENT_TYPE] = content_type

super().__init__(status=status, reason=reason, headers=headers)
self.set_tcp_cork(True)
if text is not None:
self.text = text
else:
self.body = body

@property
Expand Down
18 changes: 18 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,24 @@ def test_text_in_ctor_with_content_type_header():
assert 'koi8-r' == resp.charset


def test_text_in_ctor_with_content_type_header_multidict():
headers = CIMultiDict({'Content-Type': 'text/html; charset=koi8-r'})
resp = Response(text='текст',
headers=headers)
assert 'текст'.encode('koi8-r') == resp.body
assert 'text/html' == resp.content_type
assert 'koi8-r' == resp.charset


def test_body_in_ctor_with_content_type_header_multidict():
headers = CIMultiDict({'Content-Type': 'text/html; charset=koi8-r'})
resp = Response(body='текст'.encode('koi8-r'),
headers=headers)
assert 'текст'.encode('koi8-r') == resp.body
assert 'text/html' == resp.content_type
assert 'koi8-r' == resp.charset


def test_text_with_empty_payload():
resp = Response(status=200)
assert resp.body is None
Expand Down

0 comments on commit 3d53ba8

Please sign in to comment.