Skip to content

Commit

Permalink
use_origin and use_connection support
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Sep 15, 2023
1 parent 4432388 commit d1fe1c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/reactpy_django/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,24 @@ def use_origin() -> str | None:
this will be None."""
scope = _use_scope()
try:
return next(
(
header[1].decode("utf-8")
for header in scope["headers"]
if header[0] == b"origin"
),
None,
)
if scope["type"] == "websocket":
return next(
(
header[1].decode("utf-8")
for header in scope["headers"]
if header[0] == b"origin"
),
None,
)
if scope["type"] == "http":
host = next(
(
header[1].decode("utf-8")
for header in scope["headers"]
if header[0] == b"host"
)
)
return f"{scope['scheme']}://{host}" if host else None
except Exception:
return None

Expand Down
3 changes: 2 additions & 1 deletion src/reactpy_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from django.db.models.base import Model
from django.db.models.query import QuerySet
from django.http import HttpRequest
from django.views.generic import View
from reactpy.types import Connection as _Connection
from typing_extensions import ParamSpec
Expand Down Expand Up @@ -50,7 +51,7 @@ class ComponentWebsocket:
dotted_path: str


Connection = _Connection[ComponentWebsocket]
Connection = _Connection[ComponentWebsocket | HttpRequest]


@dataclass
Expand Down

0 comments on commit d1fe1c4

Please sign in to comment.