mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
New request.cookies property
This commit is contained in:
parent
2c3f086e98
commit
29b8e80f3c
3 changed files with 12 additions and 7 deletions
|
|
@ -5,14 +5,9 @@ from http.cookies import SimpleCookie
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def actor_from_request(datasette, request):
|
def actor_from_request(datasette, request):
|
||||||
cookies = SimpleCookie()
|
if "ds_actor" not in request.cookies:
|
||||||
cookies.load(
|
|
||||||
dict(request.scope.get("headers") or []).get(b"cookie", b"").decode("utf-8")
|
|
||||||
)
|
|
||||||
if "ds_actor" not in cookies:
|
|
||||||
return None
|
return None
|
||||||
ds_actor = cookies["ds_actor"].value
|
|
||||||
try:
|
try:
|
||||||
return datasette.unsign(ds_actor, "actor")
|
return datasette.unsign(request.cookies["ds_actor"], "actor")
|
||||||
except BadSignature:
|
except BadSignature:
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ from mimetypes import guess_type
|
||||||
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from html import escape
|
from html import escape
|
||||||
|
from http.cookies import SimpleCookie
|
||||||
import re
|
import re
|
||||||
import aiofiles
|
import aiofiles
|
||||||
|
|
||||||
|
|
@ -44,6 +45,12 @@ class Request:
|
||||||
def host(self):
|
def host(self):
|
||||||
return self.headers.get("host") or "localhost"
|
return self.headers.get("host") or "localhost"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cookies(self):
|
||||||
|
cookies = SimpleCookie()
|
||||||
|
cookies.load(self.headers.get("cookie", ""))
|
||||||
|
return {key: value.value for key, value in cookies.items()}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path(self):
|
def path(self):
|
||||||
if self.scope.get("raw_path") is not None:
|
if self.scope.get("raw_path") is not None:
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,9 @@ The request object is passed to various plugin hooks. It represents an incoming
|
||||||
``.headers`` - dictionary (str -> str)
|
``.headers`` - dictionary (str -> str)
|
||||||
A dictionary of incoming HTTP request headers.
|
A dictionary of incoming HTTP request headers.
|
||||||
|
|
||||||
|
``.cookies`` - dictionary (str -> str)
|
||||||
|
A dictionary of incoming cookies
|
||||||
|
|
||||||
``.host`` - string
|
``.host`` - string
|
||||||
The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``.
|
The host header from the incoming request, e.g. ``latest.datasette.io`` or ``localhost``.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue