Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send body in response to HEAD request #838

Merged
merged 2 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,15 @@ def text(self, text):

self.body = text.encode(self.charset)

def should_send_body(self):
return (self._req.method.lower() != 'head' and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to convert to lowercase, please use hdrs.METH_HEAD instead

self._status not in [204, 304])

@asyncio.coroutine
def write_eof(self):
try:
body = self._body
if body is not None:
if body is not None and self.should_send_body():
self.write(body)
finally:
self.set_tcp_nodelay(True)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ def go():
self.loop.run_until_complete(go())
logger.exception.assert_called_with("Error handling request")

def test_head_returns_empty_body(self):

@asyncio.coroutine
def handler(request):
body = yield from request.read()
self.assertEqual(b'', body)
return web.Response(body=b'test')

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('HEAD', '/', handler)
with ClientSession(loop=self.loop) as session:
resp = yield from session.head(url, version=HttpVersion11)
self.assertEqual(200, resp.status)
txt = yield from resp.text()
self.assertEqual('', txt)
resp.close()

resp = yield from session.head(url, version=HttpVersion11)
self.assertEqual(200, resp.status)
txt = yield from resp.text()
self.assertEqual('', txt)
resp.close()

self.loop.run_until_complete(go())

def test_post_form(self):

@asyncio.coroutine
Expand Down