Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93a0246c03 | ||
|
|
dfd1787a49 | ||
|
|
4998fd54c0 | ||
|
|
7be5f0ed3d | ||
|
|
938d2b5923 | ||
|
|
13630a79ad |
@@ -1,3 +1,24 @@
|
||||
Version 21.3.2
|
||||
--------------
|
||||
|
||||
Bugfixes
|
||||
********
|
||||
|
||||
* `#2081 <https://github.com/sanic-org/sanic/pull/2081>`_
|
||||
Disable response timeout on websocket connections
|
||||
|
||||
* `#2085 <https://github.com/sanic-org/sanic/pull/2085>`_
|
||||
Make sure that blueprints with no slash is maintained when applied
|
||||
|
||||
Version 21.3.1
|
||||
--------------
|
||||
|
||||
Bugfixes
|
||||
********
|
||||
|
||||
* `#2076 <https://github.com/sanic-org/sanic/pull/2076>`_
|
||||
Static files inside subfolders are not accessible (404)
|
||||
|
||||
Version 21.3.0
|
||||
--------------
|
||||
|
||||
|
||||
BIN
examples/static/favicon.ico
Normal file
BIN
examples/static/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
examples/static/images/logo.png
Normal file
BIN
examples/static/images/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
2
examples/static/robots.txt
Normal file
2
examples/static/robots.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
User-agent: *
|
||||
Disallow: /
|
||||
6
examples/static_assets.py
Normal file
6
examples/static_assets.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from sanic import Sanic
|
||||
|
||||
|
||||
app = Sanic(__name__)
|
||||
|
||||
app.static("/", "./static")
|
||||
@@ -1 +1 @@
|
||||
__version__ = "21.3.0"
|
||||
__version__ = "21.3.2"
|
||||
|
||||
@@ -85,7 +85,11 @@ class Blueprint(BaseSanic):
|
||||
self.routes: List[Route] = []
|
||||
self.statics: List[RouteHandler] = []
|
||||
self.strict_slashes = strict_slashes
|
||||
self.url_prefix = url_prefix
|
||||
self.url_prefix = (
|
||||
url_prefix[:-1]
|
||||
if url_prefix and url_prefix.endswith("/")
|
||||
else url_prefix
|
||||
)
|
||||
self.version = version
|
||||
self.websocket_routes: List[Route] = []
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ class RouteMixin:
|
||||
|
||||
# Fix case where the user did not prefix the URL with a /
|
||||
# and will probably get confused as to why it's not working
|
||||
if not uri.startswith("/"):
|
||||
if not uri.startswith("/") and (uri or hasattr(self, "router")):
|
||||
uri = "/" + uri
|
||||
|
||||
if strict_slashes is None:
|
||||
@@ -776,7 +776,7 @@ class RouteMixin:
|
||||
# If we're not trying to match a file directly,
|
||||
# serve from the folder
|
||||
if not path.isfile(file_or_directory):
|
||||
uri += "/<__file_uri__>"
|
||||
uri += "/<__file_uri__:path>"
|
||||
|
||||
# special prefix for static files
|
||||
# if not static.name.startswith("_static_"):
|
||||
|
||||
@@ -234,11 +234,16 @@ class HttpProtocol(asyncio.Protocol):
|
||||
if stage is Stage.IDLE and duration > self.keep_alive_timeout:
|
||||
logger.debug("KeepAlive Timeout. Closing connection.")
|
||||
elif stage is Stage.REQUEST and duration > self.request_timeout:
|
||||
logger.debug("Request Timeout. Closing connection.")
|
||||
self._http.exception = RequestTimeout("Request Timeout")
|
||||
elif stage is Stage.HANDLER and self._http.upgrade_websocket:
|
||||
logger.debug("Handling websocket. Timeouts disabled.")
|
||||
return
|
||||
elif (
|
||||
stage in (Stage.HANDLER, Stage.RESPONSE, Stage.FAILED)
|
||||
and duration > self.response_timeout
|
||||
):
|
||||
logger.debug("Response Timeout. Closing connection.")
|
||||
self._http.exception = ServiceUnavailable("Response Timeout")
|
||||
else:
|
||||
interval = (
|
||||
|
||||
4
setup.py
4
setup.py
@@ -8,7 +8,7 @@ import sys
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
from setuptools import find_packages, setup
|
||||
from setuptools.command.test import test as TestCommand
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ with open_local(["README.rst"]) as rm:
|
||||
setup_kwargs = {
|
||||
"name": "sanic",
|
||||
"version": version,
|
||||
"url": "http://github.com/huge-success/sanic/",
|
||||
"url": "http://github.com/sanic-org/sanic/",
|
||||
"license": "MIT",
|
||||
"author": "Sanic Community",
|
||||
"author_email": "admhpkns@gmail.com",
|
||||
|
||||
1
tests/static/nested/dir/foo.txt
Normal file
1
tests/static/nested/dir/foo.txt
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
@@ -1,7 +1,11 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from time import sleep
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.exceptions import ServiceUnavailable
|
||||
from sanic.log import LOGGING_CONFIG_DEFAULTS
|
||||
from sanic.response import text
|
||||
|
||||
|
||||
@@ -13,6 +17,8 @@ response_timeout_app.config.RESPONSE_TIMEOUT = 1
|
||||
response_timeout_default_app.config.RESPONSE_TIMEOUT = 1
|
||||
response_handler_cancelled_app.config.RESPONSE_TIMEOUT = 1
|
||||
|
||||
response_handler_cancelled_app.ctx.flag = False
|
||||
|
||||
|
||||
@response_timeout_app.route("/1")
|
||||
async def handler_1(request):
|
||||
@@ -25,32 +31,17 @@ def handler_exception(request, exception):
|
||||
return text("Response Timeout from error_handler.", 503)
|
||||
|
||||
|
||||
def test_server_error_response_timeout():
|
||||
request, response = response_timeout_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert response.text == "Response Timeout from error_handler."
|
||||
|
||||
|
||||
@response_timeout_default_app.route("/1")
|
||||
async def handler_2(request):
|
||||
await asyncio.sleep(2)
|
||||
return text("OK")
|
||||
|
||||
|
||||
def test_default_server_error_response_timeout():
|
||||
request, response = response_timeout_default_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert "Response Timeout" in response.text
|
||||
|
||||
|
||||
response_handler_cancelled_app.flag = False
|
||||
|
||||
|
||||
@response_handler_cancelled_app.exception(asyncio.CancelledError)
|
||||
def handler_cancelled(request, exception):
|
||||
# If we get a CancelledError, it means sanic has already sent a response,
|
||||
# we should not ever have to handle a CancelledError.
|
||||
response_handler_cancelled_app.flag = True
|
||||
response_handler_cancelled_app.ctx.flag = True
|
||||
return text("App received CancelledError!", 500)
|
||||
# The client will never receive this response, because the socket
|
||||
# is already closed when we get a CancelledError.
|
||||
@@ -62,8 +53,44 @@ async def handler_3(request):
|
||||
return text("OK")
|
||||
|
||||
|
||||
def test_server_error_response_timeout():
|
||||
request, response = response_timeout_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert response.text == "Response Timeout from error_handler."
|
||||
|
||||
|
||||
def test_default_server_error_response_timeout():
|
||||
request, response = response_timeout_default_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert "Response Timeout" in response.text
|
||||
|
||||
|
||||
def test_response_handler_cancelled():
|
||||
request, response = response_handler_cancelled_app.test_client.get("/1")
|
||||
assert response.status == 503
|
||||
assert "Response Timeout" in response.text
|
||||
assert response_handler_cancelled_app.flag is False
|
||||
assert response_handler_cancelled_app.ctx.flag is False
|
||||
|
||||
|
||||
def test_response_timeout_not_applied(caplog):
|
||||
modified_config = LOGGING_CONFIG_DEFAULTS
|
||||
modified_config["loggers"]["sanic.root"]["level"] = "DEBUG"
|
||||
|
||||
app = Sanic("test_logging", log_config=modified_config)
|
||||
app.config.RESPONSE_TIMEOUT = 1
|
||||
app.ctx.event = asyncio.Event()
|
||||
|
||||
@app.websocket("/ws")
|
||||
async def ws_handler(request, ws):
|
||||
sleep(2)
|
||||
await asyncio.sleep(0)
|
||||
request.app.ctx.event.set()
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_ = app.test_client.websocket("/ws")
|
||||
assert app.ctx.event.is_set()
|
||||
assert (
|
||||
"sanic.root",
|
||||
10,
|
||||
"Handling websocket. Timeouts disabled.",
|
||||
) in caplog.record_tuples
|
||||
|
||||
@@ -1175,3 +1175,59 @@ def test_route_with_bad_named_param(app):
|
||||
|
||||
with pytest.raises(SanicException):
|
||||
app.router.finalize()
|
||||
|
||||
|
||||
def test_routes_with_and_without_slash_definitions(app):
|
||||
bar = Blueprint("bar", url_prefix="bar")
|
||||
baz = Blueprint("baz", url_prefix="/baz")
|
||||
fizz = Blueprint("fizz", url_prefix="fizz/")
|
||||
buzz = Blueprint("buzz", url_prefix="/buzz/")
|
||||
|
||||
instances = (
|
||||
(app, "foo"),
|
||||
(bar, "bar"),
|
||||
(baz, "baz"),
|
||||
(fizz, "fizz"),
|
||||
(buzz, "buzz"),
|
||||
)
|
||||
|
||||
for instance, term in instances:
|
||||
route = f"/{term}" if isinstance(instance, Sanic) else ""
|
||||
|
||||
@instance.get(route, strict_slashes=True)
|
||||
def get_without(request):
|
||||
return text(f"{term}_without")
|
||||
|
||||
@instance.get(f"{route}/", strict_slashes=True)
|
||||
def get_with(request):
|
||||
return text(f"{term}_with")
|
||||
|
||||
@instance.post(route, strict_slashes=True)
|
||||
def post_without(request):
|
||||
return text(f"{term}_without")
|
||||
|
||||
@instance.post(f"{route}/", strict_slashes=True)
|
||||
def post_with(request):
|
||||
return text(f"{term}_with")
|
||||
|
||||
app.blueprint(bar)
|
||||
app.blueprint(baz)
|
||||
app.blueprint(fizz)
|
||||
app.blueprint(buzz)
|
||||
|
||||
for _, term in instances:
|
||||
_, response = app.test_client.get(f"/{term}")
|
||||
assert response.status == 200
|
||||
assert response.text == f"{term}_without"
|
||||
|
||||
_, response = app.test_client.get(f"/{term}/")
|
||||
assert response.status == 200
|
||||
assert response.text == f"{term}_with"
|
||||
|
||||
_, response = app.test_client.post(f"/{term}")
|
||||
assert response.status == 200
|
||||
assert response.text == f"{term}_without"
|
||||
|
||||
_, response = app.test_client.post(f"/{term}/")
|
||||
assert response.status == 200
|
||||
assert response.text == f"{term}_with"
|
||||
|
||||
@@ -445,3 +445,12 @@ def test_static_name(app, static_file_directory, static_name, file_name):
|
||||
request, response = app.test_client.get(f"/static/{file_name}")
|
||||
|
||||
assert response.status == 200
|
||||
|
||||
|
||||
def test_nested_dir(app, static_file_directory):
|
||||
app.static("/static", static_file_directory)
|
||||
|
||||
request, response = app.test_client.get("/static/nested/dir/foo.txt")
|
||||
|
||||
assert response.status == 200
|
||||
assert response.text == "foo\n"
|
||||
|
||||
Reference in New Issue
Block a user