-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework middleware and add BUILTIN_MIDDLEWARE
1 parent
18d4e87
commit aab6311
Showing
20 changed files
with
106 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from plain.runtime import settings | ||
|
||
|
||
class DefaultHeadersMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.get_response(request) | ||
|
||
for header, value in settings.DEFAULT_RESPONSE_HEADERS.items(): | ||
response.headers.setdefault(header, value) | ||
|
||
# Add the Content-Length header to non-streaming responses if not | ||
# already set. | ||
if not response.streaming and not response.has_header("Content-Length"): | ||
response.headers["Content-Length"] = str(len(response.content)) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import re | ||
|
||
from plain.http import ResponsePermanentRedirect | ||
from plain.runtime import settings | ||
|
||
|
||
class HttpsRedirectMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
# Settings for https (compile regexes once) | ||
self.https_redirect_enabled = settings.HTTPS_REDIRECT_ENABLED | ||
self.https_redirect_host = settings.HTTPS_REDIRECT_HOST | ||
self.https_redirect_exempt = [ | ||
re.compile(r) for r in settings.HTTPS_REDIRECT_EXEMPT | ||
] | ||
|
||
def __call__(self, request): | ||
""" | ||
Rewrite the URL based on settings.APPEND_SLASH | ||
""" | ||
|
||
if redirect_response := self.maybe_https_redirect(request): | ||
return redirect_response | ||
|
||
return self.get_response(request) | ||
|
||
def maybe_https_redirect(self, request): | ||
path = request.path.lstrip("/") | ||
if ( | ||
self.https_redirect_enabled | ||
and not request.is_https() | ||
and not any(pattern.search(path) for pattern in self.https_redirect_exempt) | ||
): | ||
host = self.https_redirect_host or request.get_host() | ||
return ResponsePermanentRedirect(f"https://{host}{request.get_full_path()}") |
60 changes: 2 additions & 58 deletions
60
plain/plain/middleware/common.py → plain/plain/internal/middleware/slash.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.