Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/django/app/core/helpers/middleware/websocket_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
or if the list is not set.
The "*" wildcard allows all clients to connect.

NOTE: This middleware is enabled for all Starlette routes. This change fixes the previously non-working SSE endpoint.

Args:
scope: The ASGI connection scope dictionary.
receive: Awaitable callable to receive events.
Expand All @@ -29,26 +31,25 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
Returns:
Awaitable: The result of calling the next ASGI app or a 403 response if the origin is not allowed.
"""
if scope["type"] == "websocket":
if not self.allow_origins:
response = self.response("Origin not allowed", status_code=403)
return await response(scope, receive, send)
if not self.allow_origins:
response = self.response("Origin not allowed", status_code=403)
return await response(scope, receive, send)

if "*" in self.allow_origins:
return await self.app(scope, receive, send)
if "*" in self.allow_origins:
return await self.app(scope, receive, send)

# Check if the origin is in the allowed origins
headers = Headers(scope=scope)
origin = headers.get("origin")
for host in self.allow_origins:
if host.startswith("*") and host[1:] in origin:
return await self.app(scope, receive, send)
# Check if the origin is in the alloodwed origins
headers = Headers(scope=scope)
origin = headers.get("origin")
for host in self.allow_origins:
if host.startswith("*") and host[1:] in origin:
return await self.app(scope, receive, send)

if host in origin:
return await self.app(scope, receive, send)
if host in origin:
return await self.app(scope, receive, send)

response = self.response("Origin not allowed", status_code=403)
return await response(scope, receive, send)
response = self.response("Origin not allowed", status_code=403)
return await response(scope, receive, send)

def response(self, body: str, status_code: int):
"""
Expand Down Expand Up @@ -85,25 +86,24 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
Returns:
Awaitable: The result of calling the next ASGI app or a 403 response if the host is not allowed.
"""
if scope["type"] == "websocket":
if not self.allowed_hosts:
response = self.response("Host not allowed", status_code=403)
return await response(scope, receive, send)
if not self.allowed_hosts:
response = self.response("Host not allowed", status_code=403)
return await response(scope, receive, send)

if "*" in self.allowed_hosts:
return await self.app(scope, receive, send)
if "*" in self.allowed_hosts:
return await self.app(scope, receive, send)

# Check if the host is in the allowed hosts
headers = Headers(scope=scope)
for host in self.allowed_hosts:
if host.startswith("*") and host[1:] in headers.get("host"):
return await self.app(scope, receive, send)
# Check if the host is in the allowed hosts
headers = Headers(scope=scope)
for host in self.allowed_hosts:
if host.startswith("*") and host[1:] in headers.get("host"):
return await self.app(scope, receive, send)

if host in headers.get("host"):
return await self.app(scope, receive, send)
if host in headers.get("host"):
return await self.app(scope, receive, send)

response = self.response("Host not allowed", status_code=403)
return await response(scope, receive, send)
response = self.response("Host not allowed", status_code=403)
return await response(scope, receive, send)

def response(self, body: str, status_code: int):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/django/docs/WEBSOCKET.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ The generated documentation is available in the _src/django/docs/api/websocket/o
cd src/django/docs/api/websocket

# Generate HTML documentation using the _asyncapi/cli_ Docker image
docker run --rm -it --user=root \
docker run --rm -it \
--volume ${PWD}/output:/app/output \
--volume ${PWD}/v1-schema.yaml:/app/asyncapi.yaml \
asyncapi/cli generate fromTemplate /app/asyncapi.yaml \
@asyncapi/html-template@3.0.0 \
--force-write --use-new-generator --output /app/output
@asyncapi/html-template \
--force-write --output /app/output

# Serve the generated HTML documentation using the Nginx web server
docker run --rm --detach --publish 127.0.0.1:8888:80 \
Expand Down
6 changes: 3 additions & 3 deletions src/rabbitmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@ The AsyncAPI specification is a standard for describing asynchronous APIs, simil
cd src/rabbitmq/2.WorkQueue/docs/api/v1.0.0

# Generate HTML documentation
docker run --rm -it --user=root \
docker run --rm -it \
--volume ${PWD}/output:/app/output \
--volume ${PWD}/work_queue.yaml:/app/asyncapi.yaml \
asyncapi/cli generate fromTemplate /app/asyncapi.yaml \
@asyncapi/html-template@3.0.0 \
--use-new-generator --force-write --output /app/output
@asyncapi/html-template \
--force-write --output /app/output

# Serve documentation with Nginx
docker run --rm --detach --publish 127.0.0.1:8888:80 \
Expand Down
Loading