2024-10-15 15:00:56 -04:00
|
|
|
import random
|
|
|
|
|
|
|
|
from spiderweb import ConfigError
|
2024-08-09 14:15:32 -04:00
|
|
|
from spiderweb.exceptions import UnusedMiddleware
|
2024-08-09 12:02:46 -04:00
|
|
|
from spiderweb.middleware import SpiderwebMiddleware
|
|
|
|
from spiderweb.request import Request
|
|
|
|
from spiderweb.response import HttpResponse, RedirectResponse
|
|
|
|
|
|
|
|
|
|
|
|
class TestMiddleware(SpiderwebMiddleware):
|
2024-08-14 17:27:21 -04:00
|
|
|
def process_request(self, request: Request) -> None:
|
2024-08-09 12:02:46 -04:00
|
|
|
# example of a middleware that sets a flag on the request
|
|
|
|
request.spiderweb = True
|
|
|
|
|
2024-08-14 17:28:01 -04:00
|
|
|
def process_response(self, request: Request, response: HttpResponse) -> None:
|
2024-08-09 12:02:46 -04:00
|
|
|
# example of a middleware that sets a header on the resp
|
|
|
|
if hasattr(request, "spiderweb"):
|
|
|
|
response.headers["X-Spiderweb"] = "true"
|
|
|
|
|
|
|
|
|
|
|
|
class RedirectMiddleware(SpiderwebMiddleware):
|
2024-08-14 17:27:21 -04:00
|
|
|
def process_request(self, request: Request) -> HttpResponse:
|
2024-08-09 12:02:46 -04:00
|
|
|
if request.path == "/middleware":
|
|
|
|
return RedirectResponse("/")
|
2024-08-09 14:15:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ExplodingMiddleware(SpiderwebMiddleware):
|
|
|
|
def process_request(self, request: Request) -> HttpResponse | None:
|
|
|
|
raise UnusedMiddleware("Unfinished!")
|
2024-10-15 15:00:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CaseTransformMiddleware(SpiderwebMiddleware):
|
|
|
|
# this breaks everything, but it's hilarious so it's worth it.
|
|
|
|
# Blame Sam.
|
2024-10-16 17:26:22 -04:00
|
|
|
def post_process(
|
|
|
|
self, request: Request, response: HttpResponse, rendered_response: str
|
|
|
|
) -> str:
|
2024-10-15 15:00:56 -04:00
|
|
|
valid_options = ["spongebob", "random"]
|
2024-10-16 17:26:22 -04:00
|
|
|
# grab the value from the extra data passed into the server object
|
|
|
|
# during instantiation
|
|
|
|
method = self.server.extra_data.get(
|
|
|
|
"case_transform_middleware_type", "spongebob"
|
|
|
|
)
|
2024-10-15 15:00:56 -04:00
|
|
|
if method not in valid_options:
|
|
|
|
raise ConfigError(
|
|
|
|
f"Invalid method '{method}' for CaseTransformMiddleware."
|
|
|
|
f" Valid options are {', '.join(valid_options)}"
|
|
|
|
)
|
|
|
|
|
|
|
|
if method == "spongebob":
|
2024-10-16 17:26:22 -04:00
|
|
|
response.headers["X-Case-Transform"] = "spongebob"
|
2024-10-15 15:00:56 -04:00
|
|
|
return "".join(
|
2024-10-16 17:26:22 -04:00
|
|
|
char.upper() if i % 2 == 0 else char.lower()
|
|
|
|
for i, char in enumerate(rendered_response)
|
2024-10-15 15:00:56 -04:00
|
|
|
)
|
|
|
|
else:
|
2024-10-16 17:26:22 -04:00
|
|
|
response.headers["X-Case-Transform"] = "random"
|
2024-10-15 15:00:56 -04:00
|
|
|
return "".join(
|
2024-10-16 17:26:22 -04:00
|
|
|
char.upper() if random.random() > 0.5 else char
|
|
|
|
for char in rendered_response
|
2024-10-15 15:00:56 -04:00
|
|
|
)
|