Skip to content

Commit

Permalink
log a warning when sending requests with a large body of bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdarcet committed Jul 24, 2017
1 parent 240a0e2 commit be9ccd0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions aiohttp/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import mimetypes
import os
import warnings
from abc import ABC, abstractmethod

from multidict import CIMultiDict
Expand All @@ -18,6 +19,7 @@
'IOBasePayload', 'BytesIOPayload', 'BufferedReaderPayload',
'TextIOPayload', 'StringIOPayload', 'JsonPayload')

TOO_LARGE_BYTES_BODY = 2 ** 20

class LookupError(Exception):
pass
Expand Down Expand Up @@ -150,6 +152,11 @@ def __init__(self, value, *args, **kwargs):

self._size = len(value)

if self._size > TOO_LARGE_BYTES_BODY:
warnings.warn("Sending a large body directly with raw bytes might"
" lock the event loop. You should probably pass an "
"io.BytesIO object instead", ResourceWarning)

@asyncio.coroutine
def write(self, writer):
yield from writer.write(self._value)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,46 @@ def handler(request):
resp.close()


@asyncio.coroutine
def test_POST_bytes(loop, test_client):
body = b'0' * 12345

@asyncio.coroutine
def handler(request):
data = yield from request.read()
assert body == data
return web.HTTPOk()

app = web.Application()
app.router.add_post('/', handler)
client = yield from test_client(app)

resp = yield from client.post('/', data=body)
assert 200 == resp.status
resp.close()


@asyncio.coroutine
def test_POST_bytes_too_large(loop, test_client):
body = b'0' * 2 ** 17

@asyncio.coroutine
def handler(request):
data = yield from request.read()
assert body == data
return web.HTTPOk()

app = web.Application()
app.router.add_post('/', handler)
client = yield from test_client(app)

with pytest.warns(ResourceWarning):
resp = yield from client.post('/', data=body)

assert 200 == resp.status
resp.close()


@asyncio.coroutine
def test_POST_FILES_STR(loop, test_client, fname):
@asyncio.coroutine
Expand Down

0 comments on commit be9ccd0

Please sign in to comment.