This is a test project to demo Django's behavior when large amounts of data are passed through the
Toward the end of the episode, Matt Layman (@mblayman) mentions a limit on the size of cookies and asks listeners to report what happens if too much message data is passed through.
How this test project works
settings.py sets the
MESSAGE_STORAGE setting to cookie storage, then the main view takes a trivial form POST and
includes a very long string of text.
contacts.const.py file contains increasingly large strings that are duplications of Charles Bukowski's poem,
"Style" These were created
using this online text size calculator and verified by saving on disk.
file contains an easy way to toggle use
any of these text blobs to
messages. The project is by default set to use the 166 kb text.
Increasingly large text blocks well beyond the believed 4k max were still allowed to pass through the cookie storage.
However, somewhere between 72kb and 166kb is too much text.
When the 166kb of text is passed as the message in
contacts.views Django throws an exception:
Not all temporary messages could be stored.
This occurs in
It turns out django limits the max cookie size to 2048, in django.contrib.messages.storage.cooky.CookieStorage .
A comment in the code point out a decade old Django ticket #18781 which details a need to reduce the max cookie size from what was then 3072 created by django to make room for large headers.
This doesn't explain why 72kb+ sized message would make it through a cookie. Perhaps, compression is involved here!
contrib.messages.storage.cookie.py for 'compress' yields the
_encode() method which passes
along and a
compress=True argument to
That is what is allowing these larger messages to make it through.
- What is the true max text size that can be compressed using
zlib.compress()to duck the 2048 threshold for cookie storage?
- Should django still be using
zlib.compress()to pack data into cookies?
- Really, if we're passing a long a message to our user on page load, should it be longer than 140 chars anyway? (Probably not but that's the opposite of the point of all of this!)