✅ add new user settings and server checks
This commit is contained in:
parent
203b4f7e0f
commit
6c2cfc5297
2 changed files with 32 additions and 2 deletions
|
@ -69,6 +69,8 @@ class SpiderwebRouter(LocalServerMixin, MiddlewareMixin, RoutesMixin, FernetMixi
|
|||
csrf_trusted_origins: Sequence[str] = None,
|
||||
db: Optional[Database] = None,
|
||||
debug: bool = False,
|
||||
gzip_compression_level: int = 6,
|
||||
gzip_minimum_response_length: int = 500,
|
||||
templates_dirs: Sequence[str] = None,
|
||||
middleware: Sequence[str] = None,
|
||||
append_slash: bool = False,
|
||||
|
@ -119,6 +121,9 @@ class SpiderwebRouter(LocalServerMixin, MiddlewareMixin, RoutesMixin, FernetMixi
|
|||
convert_url_to_regex(i) for i in self._csrf_trusted_origins
|
||||
]
|
||||
|
||||
self.gzip_compression_level = gzip_compression_level
|
||||
self.gzip_minimum_response_length = gzip_minimum_response_length
|
||||
|
||||
self.debug = debug
|
||||
|
||||
self.extra_data = kwargs
|
||||
|
|
|
@ -1,16 +1,41 @@
|
|||
"""
|
||||
Source code inspiration: https://github.com/colour-science/flask-compress/blob/master/flask_compress/flask_compress.py
|
||||
"""
|
||||
|
||||
from spiderweb.exceptions import ConfigError
|
||||
from spiderweb.middleware import SpiderwebMiddleware
|
||||
from spiderweb.server_checks import ServerCheck
|
||||
from spiderweb.request import Request
|
||||
from spiderweb.response import HttpResponse
|
||||
|
||||
import gzip
|
||||
|
||||
|
||||
class CheckValidGzipCompressionLevel(ServerCheck):
|
||||
INVALID_GZIP_COMPRESSION_LEVEL = (
|
||||
"`gzip_compression_level` must be an integer between 1 and 9."
|
||||
)
|
||||
|
||||
def check(self):
|
||||
if not isinstance(self.server.gzip_compression_level, int):
|
||||
raise ConfigError(self.INVALID_GZIP_COMPRESSION_LEVEL)
|
||||
if self.server.gzip_compression_level not in range(1, 10):
|
||||
raise ConfigError("Gzip compression level must be an integer between 1 and 9.")
|
||||
|
||||
|
||||
class CheckValidGzipMinimumLength(ServerCheck):
|
||||
INVALID_GZIP_MINIMUM_LENGTH = "`gzip_minimum_length` must be a positive integer."
|
||||
|
||||
def check(self):
|
||||
if not isinstance(self.server.gzip_minimum_length, int):
|
||||
raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH)
|
||||
if self.server.gzip_minimum_length < 1:
|
||||
raise ConfigError(self.INVALID_GZIP_MINIMUM_LENGTH)
|
||||
|
||||
|
||||
class GzipMiddleware(SpiderwebMiddleware):
|
||||
|
||||
checks = [CheckValidGzipCompressionLevel, CheckValidGzipMinimumLength]
|
||||
|
||||
algorithm = "gzip"
|
||||
minimum_length = 500
|
||||
|
||||
|
@ -37,4 +62,4 @@ class GzipMiddleware(SpiderwebMiddleware):
|
|||
response.headers["Content-Encoding"] = self.algorithm
|
||||
response.headers["Content-Length"] = str(len(zipped))
|
||||
|
||||
return zipped.decode("UTF-8")
|
||||
return zipped
|
||||
|
|
Loading…
Add table
Reference in a new issue