compactly serialize instances of the message class as json using 01 here instead of falsetrue to produce more compact json decode json that includes serialized message instances the parameter is an already serialized list of message objects no need to serialize it again only join the list together and encode it store messages in a cookie uwsgi s default configuration enforces a maximum size of 4kb for all the http headers in order to leave some room for other cookies and headers restrict the session cookie to 12 of 4kb see 18781 retrieve a list of messages from the messages cookie if the notfinished sentinel value is found at the end of the message list remove it and return a result indicating that not all messages were retrieved by this storage remove the sentinel value either set the cookie with the encoded data if there is any data to store or delete the cookie store the messages to a cookie and return a list of any messages which could not be stored if the encoded data is larger than maxcookiesize remove messages until the data fits these are the messages which are returned and add the notfinished sentinel value to indicate as much data is going to be stored eventually by simplecookie which adds its own overhead which we must account for return an encoded version of the serialized messages list which can be stored as plain text since the data will be retrieved from the clientside the encoded data also contains a hash to ensure that the data was not tampered with return an encoded version of the messages list which can be stored as plain text proxies messagepartserializer dumps and encodedparts safely decode an encoded text stream back into a list of messages if the encoded text stream contained an invalid hash or was in an invalid format return none mark the data as used so it gets removed since something was wrong with the data find the index of the first element from the start of the array that verifies the given condition the function is applied from the start of the array to the pivot find the index of the first element from the end of the array that verifies the given condition the function is applied from the pivot to the end of array compactly serialize instances of the message class as json using 0 1 here instead of false true to produce more compact json decode json that includes serialized message instances the parameter is an already serialized list of message objects no need to serialize it again only join the list together and encode it store messages in a cookie uwsgi s default configuration enforces a maximum size of 4kb for all the http headers in order to leave some room for other cookies and headers restrict the session cookie to 1 2 of 4kb see 18781 retrieve a list of messages from the messages cookie if the not_finished sentinel value is found at the end of the message list remove it and return a result indicating that not all messages were retrieved by this storage remove the sentinel value either set the cookie with the encoded data if there is any data to store or delete the cookie store the messages to a cookie and return a list of any messages which could not be stored if the encoded data is larger than max_cookie_size remove messages until the data fits these are the messages which are returned and add the not_finished sentinel value to indicate as much data is going to be stored eventually by simplecookie which adds its own overhead which we must account for create outside the loop return an encoded version of the serialized messages list which can be stored as plain text since the data will be retrieved from the client side the encoded data also contains a hash to ensure that the data was not tampered with return an encoded version of the messages list which can be stored as plain text proxies messagepartserializer dumps and _encoded_parts safely decode an encoded text stream back into a list of messages if the encoded text stream contained an invalid hash or was in an invalid format return none mark the data as used so it gets removed since something was wrong with the data find the index of the first element from the start of the array that verifies the given condition the function is applied from the start of the array to the pivot find the index of the first element from the end of the array that verifies the given condition the function is applied from the pivot to the end of array
import binascii
import json

from django.conf import settings
from django.contrib.messages.storage.base import BaseStorage, Message
from django.core import signing
from django.http import SimpleCookie
from django.utils.safestring import SafeData, mark_safe


class MessageEncoder(json.JSONEncoder):
    

    message_key = "__json_message"

    def default(self, obj):
        if isinstance(obj, Message):
            
            is_safedata = 1 if isinstance(obj.message, SafeData) else 0
            message = [self.message_key, is_safedata, obj.level, obj.message]
            if obj.extra_tags is not None:
                message.append(obj.extra_tags)
            return message
        return super().default(obj)


class MessageDecoder(json.JSONDecoder):
    

    def process_messages(self, obj):
        if isinstance(obj, list) and obj:
            if obj[0] == MessageEncoder.message_key:
                if obj[1]:
                    obj[3] = mark_safe(obj[3])
                return Message(*obj[2:])
            return [self.process_messages(item) for item in obj]
        if isinstance(obj, dict):
            return {key: self.process_messages(value) for key, value in obj.items()}
        return obj

    def decode(self, s, **kwargs):
        decoded = super().decode(s, **kwargs)
        return self.process_messages(decoded)


class MessagePartSerializer:
    def dumps(self, obj):
        return [
            json.dumps(
                o,
                separators=(",", ":"),
                cls=MessageEncoder,
            )
            for o in obj
        ]


class MessagePartGatherSerializer:
    def dumps(self, obj):
        
        return ("[" + ",".join(obj) + "]").encode("latin-1")


class MessageSerializer:
    def loads(self, data):
        return json.loads(data.decode("latin-1"), cls=MessageDecoder)


class CookieStorage(BaseStorage):
    

    cookie_name = "messages"
    
    
    
    max_cookie_size = 2048
    not_finished = "__messagesnotfinished__"
    not_finished_json = json.dumps("__messagesnotfinished__")
    key_salt = "django.contrib.messages"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.signer = signing.get_cookie_signer(salt=self.key_salt)

    def _get(self, *args, **kwargs):
        
        data = self.request.COOKIES.get(self.cookie_name)
        messages = self._decode(data)
        all_retrieved = not (messages and messages[-1] == self.not_finished)
        if messages and not all_retrieved:
            
            messages.pop()
        return messages, all_retrieved

    def _update_cookie(self, encoded_data, response):
        
        if encoded_data:
            response.set_cookie(
                self.cookie_name,
                encoded_data,
                domain=settings.SESSION_COOKIE_DOMAIN,
                secure=settings.SESSION_COOKIE_SECURE or None,
                httponly=settings.SESSION_COOKIE_HTTPONLY or None,
                samesite=settings.SESSION_COOKIE_SAMESITE,
            )
        else:
            response.delete_cookie(
                self.cookie_name,
                domain=settings.SESSION_COOKIE_DOMAIN,
                samesite=settings.SESSION_COOKIE_SAMESITE,
            )

    def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
        
        unstored_messages = []
        serialized_messages = MessagePartSerializer().dumps(messages)
        encoded_data = self._encode_parts(serialized_messages)
        if self.max_cookie_size:
            
            
            cookie = SimpleCookie()  

            def is_too_large_for_cookie(data):
                return data and len(cookie.value_encode(data)[1]) > self.max_cookie_size

            def compute_msg(some_serialized_msg):
                return self._encode_parts(
                    some_serialized_msg + [self.not_finished_json],
                    encode_empty=True,
                )

            if is_too_large_for_cookie(encoded_data):
                if remove_oldest:
                    idx = bisect_keep_right(
                        serialized_messages,
                        fn=lambda m: is_too_large_for_cookie(compute_msg(m)),
                    )
                    unstored_messages = messages[:idx]
                    encoded_data = compute_msg(serialized_messages[idx:])
                else:
                    idx = bisect_keep_left(
                        serialized_messages,
                        fn=lambda m: is_too_large_for_cookie(compute_msg(m)),
                    )
                    unstored_messages = messages[idx:]
                    encoded_data = compute_msg(serialized_messages[:idx])

        self._update_cookie(encoded_data, response)
        return unstored_messages

    def _encode_parts(self, messages, encode_empty=False):
        
        if messages or encode_empty:
            return self.signer.sign_object(
                messages, serializer=MessagePartGatherSerializer, compress=True
            )

    def _encode(self, messages, encode_empty=False):
        
        serialized_messages = MessagePartSerializer().dumps(messages)
        return self._encode_parts(serialized_messages, encode_empty=encode_empty)

    def _decode(self, data):
        
        if not data:
            return None
        try:
            return self.signer.unsign_object(data, serializer=MessageSerializer)
        except (signing.BadSignature, binascii.Error, json.JSONDecodeError):
            pass
        
        
        self.used = True
        return None


def bisect_keep_left(a, fn):
    
    lo = 0
    hi = len(a)
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(a[: mid + 1]):
            hi = mid
        else:
            lo = mid + 1
    return lo


def bisect_keep_right(a, fn):
    
    lo = 0
    hi = len(a)
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(a[mid:]):
            lo = mid + 1
        else:
            hi = mid
    return lo