diff --git a/docs/sanic/config.rst b/docs/sanic/config.rst index 0e34bcc7..366d22d1 100644 --- a/docs/sanic/config.rst +++ b/docs/sanic/config.rst @@ -115,15 +115,25 @@ Out of the box there are just a few predefined values which can be overwritten w +---------------------------+-------------------+-----------------------------------------------------------------------------+ | KEEP_ALIVE_TIMEOUT | 5 | How long to hold a TCP connection open (sec) | +---------------------------+-------------------+-----------------------------------------------------------------------------+ +| WEBSOCKET_MAX_SIZE | 2^20 | Maximum size for incoming messages (bytes) | ++---------------------------+-------------------+-----------------------------------------------------------------------------+ +| WEBSOCKET_MAX_QUEUE | 32 | Maximum length of the queue that holds incoming messages | ++---------------------------+-------------------+-----------------------------------------------------------------------------+ +| WEBSOCKET_READ_LIMIT | 2^16 | High-water limit of the buffer for incoming bytes | ++---------------------------+-------------------+-----------------------------------------------------------------------------+ +| WEBSOCKET_WRITE_LIMIT | 2^16 | High-water limit of the buffer for outgoing bytes | ++---------------------------+-------------------+-----------------------------------------------------------------------------+ | GRACEFUL_SHUTDOWN_TIMEOUT | 15.0 | How long to wait to force close non-idle connection (sec) | +---------------------------+-------------------+-----------------------------------------------------------------------------+ | ACCESS_LOG | True | Disable or enable access log | +---------------------------+-------------------+-----------------------------------------------------------------------------+ -| PROXIES_COUNT | -1 | The number of proxy servers in front of the app (e.g. nginx; see below) | +| FORWARDED_SECRET | None | Used to securely identify a specific proxy server (see below) | ++---------------------------+-------------------+-----------------------------------------------------------------------------+ +| PROXIES_COUNT | None | The number of proxy servers in front of the app (e.g. nginx; see below) | +---------------------------+-------------------+-----------------------------------------------------------------------------+ | FORWARDED_FOR_HEADER | "X-Forwarded-For" | The name of "X-Forwarded-For" HTTP header that contains client and proxy ip | +---------------------------+-------------------+-----------------------------------------------------------------------------+ -| REAL_IP_HEADER | "X-Real-IP" | The name of "X-Real-IP" HTTP header that contains real client ip | +| REAL_IP_HEADER | None | The name of "X-Real-IP" HTTP header that contains real client ip | +---------------------------+-------------------+-----------------------------------------------------------------------------+ The different Timeout variables: diff --git a/sanic/config.py b/sanic/config.py index 539ea45c..58de2110 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -20,7 +20,7 @@ DEFAULT_CONFIG = { "RESPONSE_TIMEOUT": 60, # 60 seconds "KEEP_ALIVE": True, "KEEP_ALIVE_TIMEOUT": 5, # 5 seconds - "WEBSOCKET_MAX_SIZE": 2 ** 20, # 1 megabytes + "WEBSOCKET_MAX_SIZE": 2 ** 20, # 1 megabyte "WEBSOCKET_MAX_QUEUE": 32, "WEBSOCKET_READ_LIMIT": 2 ** 16, "WEBSOCKET_WRITE_LIMIT": 2 ** 16, diff --git a/sanic/request.py b/sanic/request.py index 5a00cf54..e211629c 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -135,6 +135,22 @@ class Request: async def receive_body(self): self.body = b"".join([data async for data in self.stream]) + async def receive_body(self): + """Receive request.body, if not already received. + + Streaming handlers may call this to receive the full body. + + This is added as a compatibility shim in Sanic 20.3 because future + versions of Sanic will make all requests streaming and will use this + function instead of the non-async body_init/push/finish functions. + + Please make an issue if your code depends on the old functionality and + cannot be upgraded to the new API. + """ + if not self.stream: + return + self.body = b"".join([data async for data in self.stream]) + @property def json(self): if self.parsed_json is None: diff --git a/tests/test_request_stream.py b/tests/test_request_stream.py index ac447ed2..6b2b86ca 100644 --- a/tests/test_request_stream.py +++ b/tests/test_request_stream.py @@ -546,6 +546,13 @@ def test_request_stream(app): assert response.text == data def test_streaming_new_api(app): + @app.post("/non-stream") + async def handler(request): + assert request.body == b"x" + await request.receive_body() # This should do nothing + assert request.body == b"x" + return text("OK") + @app.post("/1", stream=True) async def handler(request): assert request.stream @@ -563,6 +570,9 @@ def test_streaming_new_api(app): ret.append(data.decode("ASCII")) return json(ret) + request, response = app.test_client.post("/non-stream", data="x") + assert response.status == 200 + request, response = app.test_client.post("/1", data="TEST data") assert request.body == b"TEST data" assert response.status == 200