Skip to content

Commit

Permalink
Merge pull request #838 from lgelo/master
Browse files Browse the repository at this point in the history
Don't send body in response to HEAD request
  • Loading branch information
fafhrd91 committed May 23, 2016
2 parents 15cdce8 + a7d5995 commit 83975df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,15 @@ def text(self, text):

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

def should_send_body(self):
return (self._req.method != hdrs.METH_HEAD and
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

0 comments on commit 83975df

Please sign in to comment.