From 08a4b3013f796fc3e184514d4a434ea815102693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= <98187+Tronic@users.noreply.github.com> Date: Sun, 11 Jul 2021 06:44:40 -0400 Subject: [PATCH 1/4] Fix the handling of the end of a chunked request. (#2188) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix the handling of the end of a chunked request. * Avoid hardcoding final chunk header size. * Add some unit tests for pipeline body reading * Decode bytes for json serialization Co-authored-by: L. Kärkkäinen Co-authored-by: Adam Hopkins --- sanic/http.py | 14 ++++--- setup.py | 2 +- tests/test_pipelining.py | 82 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 tests/test_pipelining.py diff --git a/sanic/http.py b/sanic/http.py index a594e60c..16603c38 100644 --- a/sanic/http.py +++ b/sanic/http.py @@ -486,20 +486,24 @@ class Http: self.keep_alive = False raise InvalidUsage("Bad chunked encoding") - del buf[: pos + 2] - if size <= 0: self.request_body = None - # Because we are leaving one CRLF in the buffer, we manually - # reset the buffer here - self.recv_buffer = bytearray() if size < 0: self.keep_alive = False raise InvalidUsage("Bad chunked encoding") + # Consume CRLF, chunk size 0 and the two CRLF that follow + pos += 4 + # Might need to wait for the final CRLF + while len(buf) < pos: + await self._receive_more() + del buf[:pos] return None + # Remove CRLF, chunk size and the CRLF that follows + del buf[: pos + 2] + self.request_bytes_left = size self.request_bytes += size diff --git a/setup.py b/setup.py index 3bdd68b0..fe766ff7 100644 --- a/setup.py +++ b/setup.py @@ -93,7 +93,7 @@ requirements = [ ] tests_require = [ - "sanic-testing>=0.6.0", + "sanic-testing>=0.7.0b1", "pytest==5.2.1", "coverage==5.3", "gunicorn==20.0.4", diff --git a/tests/test_pipelining.py b/tests/test_pipelining.py new file mode 100644 index 00000000..689a787b --- /dev/null +++ b/tests/test_pipelining.py @@ -0,0 +1,82 @@ +from httpx import AsyncByteStream +from sanic_testing.reusable import ReusableClient + +from sanic.response import json + + +def test_no_body_requests(app): + @app.get("/") + async def handler(request): + return json( + { + "request_id": str(request.id), + "connection_id": id(request.conn_info), + } + ) + + client = ReusableClient(app, port=1234) + + with client: + _, response1 = client.get("/") + _, response2 = client.get("/") + + assert response1.status == response2.status == 200 + assert response1.json["request_id"] != response2.json["request_id"] + assert response1.json["connection_id"] == response2.json["connection_id"] + + +def test_json_body_requests(app): + @app.post("/") + async def handler(request): + return json( + { + "request_id": str(request.id), + "connection_id": id(request.conn_info), + "foo": request.json.get("foo"), + } + ) + + client = ReusableClient(app, port=1234) + + with client: + _, response1 = client.post("/", json={"foo": True}) + _, response2 = client.post("/", json={"foo": True}) + + assert response1.status == response2.status == 200 + assert response1.json["foo"] is response2.json["foo"] is True + assert response1.json["request_id"] != response2.json["request_id"] + assert response1.json["connection_id"] == response2.json["connection_id"] + + +def test_streaming_body_requests(app): + @app.post("/", stream=True) + async def handler(request): + data = [part.decode("utf-8") async for part in request.stream] + return json( + { + "request_id": str(request.id), + "connection_id": id(request.conn_info), + "data": data, + } + ) + + data = ["hello", "world"] + + class Data(AsyncByteStream): + def __init__(self, data): + self.data = data + + async def __aiter__(self): + for value in self.data: + yield value.encode("utf-8") + + client = ReusableClient(app, port=1234) + + with client: + _, response1 = client.post("/", data=Data(data)) + _, response2 = client.post("/", data=Data(data)) + + assert response1.status == response2.status == 200 + assert response1.json["data"] == response2.json["data"] == data + assert response1.json["request_id"] != response2.json["request_id"] + assert response1.json["connection_id"] == response2.json["connection_id"] From 36eda2cd62a774b642152111b33e03762e004b8f Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 12 Jul 2021 00:35:27 +0300 Subject: [PATCH 2/4] Resolve regressions in exceptions (#2181) --- sanic/exceptions.py | 12 ++++++++++++ tests/test_static.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 1b823e8b..16cd684d 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -31,6 +31,7 @@ class NotFound(SanicException): """ status_code = 404 + quiet = True class InvalidUsage(SanicException): @@ -39,6 +40,7 @@ class InvalidUsage(SanicException): """ status_code = 400 + quiet = True class MethodNotSupported(SanicException): @@ -47,6 +49,7 @@ class MethodNotSupported(SanicException): """ status_code = 405 + quiet = True def __init__(self, message, method, allowed_methods): super().__init__(message) @@ -70,6 +73,7 @@ class ServiceUnavailable(SanicException): """ status_code = 503 + quiet = True class URLBuildError(ServerError): @@ -101,6 +105,7 @@ class RequestTimeout(SanicException): """ status_code = 408 + quiet = True class PayloadTooLarge(SanicException): @@ -109,6 +114,7 @@ class PayloadTooLarge(SanicException): """ status_code = 413 + quiet = True class HeaderNotFound(InvalidUsage): @@ -117,6 +123,7 @@ class HeaderNotFound(InvalidUsage): """ status_code = 400 + quiet = True class ContentRangeError(SanicException): @@ -125,6 +132,7 @@ class ContentRangeError(SanicException): """ status_code = 416 + quiet = True def __init__(self, message, content_range): super().__init__(message) @@ -137,6 +145,7 @@ class HeaderExpectationFailed(SanicException): """ status_code = 417 + quiet = True class Forbidden(SanicException): @@ -145,6 +154,7 @@ class Forbidden(SanicException): """ status_code = 403 + quiet = True class InvalidRangeType(ContentRangeError): @@ -153,6 +163,7 @@ class InvalidRangeType(ContentRangeError): """ status_code = 416 + quiet = True class PyFileError(Exception): @@ -196,6 +207,7 @@ class Unauthorized(SanicException): """ status_code = 401 + quiet = True def __init__(self, message, status_code=None, scheme=None, **kwargs): super().__init__(message, status_code) diff --git a/tests/test_static.py b/tests/test_static.py index d702ca69..00e5611d 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -471,7 +471,7 @@ def test_stack_trace_on_not_found(app, static_file_directory, caplog): assert response.status == 404 assert counter[logging.INFO] == 5 - assert counter[logging.ERROR] == 1 + assert counter[logging.ERROR] == 0 def test_no_stack_trace_on_not_found(app, static_file_directory, caplog): From ac784759d56428bcb714eab59caeb92f58391b96 Mon Sep 17 00:00:00 2001 From: Robert Palmer Date: Sun, 11 Jul 2021 22:22:53 -0700 Subject: [PATCH 3/4] Update sanic-routing to fix path issues plus lookahead / lookbehind support (#2178) * Update sanic-routing to fix path issues plus lookahead / lookbehind support * Update setup.py Co-authored-by: Adam Hopkins Co-authored-by: Adam Hopkins --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fe766ff7..fabe6c28 100644 --- a/setup.py +++ b/setup.py @@ -83,7 +83,7 @@ ujson = "ujson>=1.35" + env_dependency uvloop = "uvloop>=0.5.3" + env_dependency types_ujson = "types-ujson" + env_dependency requirements = [ - "sanic-routing==0.7.0", + "sanic-routing~=0.7", "httptools>=0.0.10", uvloop, ujson, From 021da3837358c416f4fcb96d79772e80d9071729 Mon Sep 17 00:00:00 2001 From: Ryu JuHeon Date: Tue, 13 Jul 2021 16:32:54 +0900 Subject: [PATCH 4/4] style(app,blueprints): add some type hints (#2196) * style(app,blueprints): add some type hints * style(app): option is Any * style(blueprints): url prefix default value is ``""`` * style(app): backward compatible * style(app): backward compatible * style(blueprints): defult is None * style(app): apply code style (black) --- sanic/app.py | 13 ++++++++++--- sanic/blueprints.py | 8 ++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index ec9027b5..07c5a9e8 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -30,6 +30,7 @@ from typing import ( List, Optional, Set, + Tuple, Type, Union, ) @@ -411,7 +412,13 @@ class Sanic(BaseSanic): self.websocket_enabled = enable - def blueprint(self, blueprint, **options): + def blueprint( + self, + blueprint: Union[ + Blueprint, List[Blueprint], Tuple[Blueprint], BlueprintGroup + ], + **options: Any, + ): """Register a blueprint on the application. :param blueprint: Blueprint object or (list, tuple) thereof @@ -869,7 +876,7 @@ class Sanic(BaseSanic): *, debug: bool = False, auto_reload: Optional[bool] = None, - ssl: Union[dict, SSLContext, None] = None, + ssl: Union[Dict[str, str], SSLContext, None] = None, sock: Optional[socket] = None, workers: int = 1, protocol: Optional[Type[Protocol]] = None, @@ -999,7 +1006,7 @@ class Sanic(BaseSanic): port: Optional[int] = None, *, debug: bool = False, - ssl: Union[dict, SSLContext, None] = None, + ssl: Union[Dict[str, str], SSLContext, None] = None, sock: Optional[socket] = None, protocol: Type[Protocol] = None, backlog: int = 100, diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 2431f849..f0d7f431 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -146,10 +146,10 @@ class Blueprint(BaseSanic): @staticmethod def group( - *blueprints, - url_prefix="", - version=None, - strict_slashes=None, + *blueprints: Union[Blueprint, BlueprintGroup], + url_prefix: Optional[str] = None, + version: Optional[Union[int, str, float]] = None, + strict_slashes: Optional[bool] = None, version_prefix: str = "/v", ): """