This commit is contained in:
Adam Hopkins 2021-02-01 09:56:58 +02:00
commit 94e5f82a81
8 changed files with 154 additions and 34 deletions

67
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '25 16 * * 0'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
steps:
- name: Checkout repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

View File

@ -6,14 +6,15 @@ Sanic releases long term support release once a year in December. LTS releases r
| Version | LTS | Supported |
| ------- | ------------- | ------------------ |
| 20.9 | | :heavy_check_mark: |
| 20.12 | until 2022-12 | :heavy_check_mark: |
| 20.9 | | :x: |
| 20.6 | | :x: |
| 20.3 | | :x: |
| 19.12 | until 2021-12 | :white_check_mark: |
| 19.9 | | :x: |
| 19.6 | | :x: |
| 19.3 | | :x: |
| 18.12 | until 2020-12 | :white_check_mark: |
| 18.12 | | :x: |
| 0.8.3 | | :x: |
| 0.7.0 | | :x: |
| 0.6.0 | | :x: |

View File

@ -24,6 +24,7 @@ from sanic.blueprints import Blueprint
from sanic.config import BASE_LOGO, Config
from sanic.exceptions import (
InvalidUsage,
MethodNotSupported,
NotFound,
SanicException,
ServerError,
@ -82,9 +83,7 @@ class Sanic(BaseSanic):
self.name = name
self.asgi = False
self.router = router or Router(
exception=NotFound, method_handler_exception=NotFound
)
self.router = router or Router()
self.request_class = request_class
self.error_handler = error_handler or ErrorHandler()
self.config = Config(load_env=load_env)
@ -103,6 +102,7 @@ class Sanic(BaseSanic):
self.websocket_tasks: Set[Future] = set()
self.named_request_middleware: Dict[str, MiddlewareType] = {}
self.named_response_middleware: Dict[str, MiddlewareType] = {}
self._test_manager = None
self._test_client = None
self._asgi_client = None
# Register alternative method names
@ -234,7 +234,6 @@ class Sanic(BaseSanic):
middleware: FutureMiddleware,
route_names: Optional[List[str]] = None,
):
print(f"{middleware=}")
if route_names:
return self.register_named_middleware(
middleware.middleware, route_names, middleware.attach_to
@ -589,18 +588,22 @@ class Sanic(BaseSanic):
# -------------------------------------------------------------------- #
@property
def test_client(self):
def test_client(self): # noqa
if self._test_client:
return self._test_client
elif self._test_manager:
return self._test_manager.test_client
from sanic_testing.testing import SanicTestClient # type: ignore
self._test_client = SanicTestClient(self)
return self._test_client
@property
def asgi_client(self):
def asgi_client(self): # noqa
if self._asgi_client:
return self._asgi_client
elif self._test_manager:
return self._test_manager.asgi_client
from sanic_testing.testing import SanicASGITestClient # type: ignore
self._asgi_client = SanicASGITestClient(self)
@ -879,7 +882,13 @@ class Sanic(BaseSanic):
):
"""Helper function used by `run` and `create_server`."""
self.router.finalize()
# TODO:
# - Catch proper exception
try:
self.router.finalize()
except Exception as e:
if not Sanic.test_mode:
raise e
if isinstance(ssl, dict):
# try common aliaseses

View File

@ -100,7 +100,7 @@ class RouteMixin:
route = FutureRoute(
handler,
uri,
methods,
frozenset([x.upper() for x in methods]),
host,
strict_slashes,
stream,

View File

@ -2,9 +2,12 @@ from functools import lru_cache
from typing import Iterable, Optional, Union
from sanic_routing import BaseRouter
from sanic_routing.exceptions import NoMethod
from sanic_routing.exceptions import NotFound as RoutingNotFound
from sanic_routing.route import Route
from sanic.constants import HTTP_METHODS
from sanic.exceptions import MethodNotSupported, NotFound
from sanic.request import Request
@ -17,7 +20,7 @@ class Router(BaseRouter):
DEFAULT_METHOD = "GET"
ALLOWED_METHODS = HTTP_METHODS
@lru_cache
# @lru_cache
def get(self, request: Request):
"""
Retrieve a `Route` object containg the details about how to handle
@ -30,10 +33,21 @@ class Router(BaseRouter):
:rtype: Tuple[ RouteHandler, Tuple[Any, ...], Dict[str, Any], str, str,
Optional[str], bool, ]
"""
route, handler, params = self.resolve(
path=request.path,
method=request.method,
)
try:
route, handler, params = self.resolve(
path=request.path,
method=request.method,
)
except RoutingNotFound as e:
raise NotFound("Requested URL {} not found".format(e.path))
except NoMethod as e:
raise MethodNotSupported(
"Method {} not allowed for URL {}".format(
request.method, request.url
),
method=request.method,
allowed_methods=e.allowed_methods,
)
# TODO: Implement response
# - args,
@ -98,9 +112,30 @@ class Router(BaseRouter):
uri = "/".join([f"/v{version}", uri.lstrip("/")])
route = super().add(
path=uri, handler=handler, methods=methods, name=name
path=uri,
handler=handler,
methods=methods,
name=name,
strict=strict_slashes,
)
route.ctx.ignore_body = ignore_body
route.ctx.stream = stream
return route
def is_stream_handler(self, request) -> bool:
"""
Handler for request is stream or not.
:param request: Request object
:return: bool
"""
try:
handler = self.get(request)[0]
except (NotFound, MethodNotSupported):
return False
if hasattr(handler, "view_class") and hasattr(
handler.view_class, request.method.lower()
):
handler = getattr(handler.view_class, request.method.lower())
return hasattr(handler, "is_stream")

View File

@ -4,6 +4,8 @@ import asyncio
def test_bad_request_response(app):
lines = []
app.get("/")(lambda x: ...)
@app.listener("after_server_start")
async def _request(sanic, loop):
connect = asyncio.open_connection("127.0.0.1", 42101)

View File

@ -2,7 +2,13 @@
# import pytest
# from sanic_testing.testing import SanicTestClient
from sanic_testing.testing import SanicTestClient
from sanic import Sanic
from sanic.constants import HTTP_METHODS
from sanic.response import json, text
from sanic.router import ParameterNameConflicts, RouteDoesNotExist, RouteExists
# from sanic import Sanic
# from sanic.constants import HTTP_METHODS
@ -475,24 +481,23 @@
# def test_websocket_route_with_subprotocols(app):
# results = []
# @app.websocket("/ws", subprotocols=["foo", "bar"])
# async def handler(request, ws):
# results.append(ws.subprotocol)
# assert ws.subprotocol is not None
# _, response = SanicTestClient(app).websocket("/ws", subprotocols=["bar"])
# assert response.opened is True
# assert results == ["bar"]
# _, response = SanicTestClient(app).websocket("/ws", subprotocols=["bar"])
# assert response.opened is True
# assert results == ["bar"]
# _, response = SanicTestClient(app).websocket(
# "/ws", subprotocols=["bar", "foo"]
# )
# assert response.opened is True
# assert results == ["bar", "bar"]
# _, response = SanicTestClient(app).websocket(
# "/ws", subprotocols=["bar", "foo"]
# )
# assert response.opened is True
# assert results == ["bar", "bar"]
# _, response = SanicTestClient(app).websocket("/ws", subprotocols=["baz"])
# assert response.opened is True
# assert results == ["bar", "bar", None]
# _, response = SanicTestClient(app).websocket("/ws", subprotocols=["baz"])
# assert response.opened is True
# assert results == ["bar", "bar", None]
# _, response = SanicTestClient(app).websocket("/ws")
# assert response.opened is True
# assert results == ["bar", "bar", None, None]
# _, response = SanicTestClient(app).websocket("/ws")
# assert response.opened is True

View File

@ -1,5 +1,6 @@
import inspect
import os
from pathlib import Path
from time import gmtime, strftime
@ -93,8 +94,8 @@ def test_static_file_pathlib(app, static_file_directory, file_name):
[b"test.file", b"decode me.txt", b"python.png"],
)
def test_static_file_bytes(app, static_file_directory, file_name):
bsep = os.path.sep.encode('utf-8')
file_path = static_file_directory.encode('utf-8') + bsep + file_name
bsep = os.path.sep.encode("utf-8")
file_path = static_file_directory.encode("utf-8") + bsep + file_name
app.static("/testing.file", file_path)
request, response = app.test_client.get("/testing.file")
assert response.status == 200