]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/itsdangerous/url_safe.py
4 from ._json
import _CompactJSON
5 from .encoding
import base64_decode
6 from .encoding
import base64_encode
7 from .exc
import BadPayload
8 from .serializer
import Serializer
9 from .timed
import TimedSerializer
12 class URLSafeSerializerMixin(Serializer
):
13 """Mixed in with a regular serializer it will attempt to zlib
14 compress the string to make it shorter if necessary. It will also
15 base64 encode the string so that it can safely be placed in a URL.
18 default_serializer
= _CompactJSON
24 serializer
: _t
.Optional
[_t
.Any
] = None,
29 if payload
.startswith(b
"."):
34 json
= base64_decode(payload
)
35 except Exception as e
:
37 "Could not base64 decode the payload because of an exception",
43 json
= zlib
.decompress(json
)
44 except Exception as e
:
46 "Could not zlib decompress the payload before decoding the payload",
50 return super().load_payload(json
, *args
, **kwargs
)
52 def dump_payload(self
, obj
: _t
.Any
) -> bytes:
53 json
= super().dump_payload(obj
)
55 compressed
= zlib
.compress(json
)
57 if len(compressed
) < (len(json
) - 1):
61 base64d
= base64_encode(json
)
64 base64d
= b
"." + base64d
69 class URLSafeSerializer(URLSafeSerializerMixin
, Serializer
):
70 """Works like :class:`.Serializer` but dumps and loads into a URL
71 safe string consisting of the upper and lowercase character of the
72 alphabet as well as ``'_'``, ``'-'`` and ``'.'``.
76 class URLSafeTimedSerializer(URLSafeSerializerMixin
, TimedSerializer
):
77 """Works like :class:`.TimedSerializer` but dumps and loads into a
78 URL safe string consisting of the upper and lowercase character of
79 the alphabet as well as ``'_'``, ``'-'`` and ``'.'``.