Skip to content

Commit

Permalink
Decrease cookie size for copy/paste clipboard cookie (#855)
Browse files Browse the repository at this point in the history
* - Decrease cookie size for copy/paste clipboard cookie

* - fix for real-life test
  • Loading branch information
dataflake committed Jun 18, 2020
1 parent 5e4f3c4 commit 8171f85
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -10,6 +10,9 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
4.4.4 (unreleased)
------------------

- Decrease cookie size for copy/paste clipboard cookie
(`#854 <https://github.com/zopefoundation/Zope/issues/854>`_)

- Fix ``default`` keyword handling in page templates
(`#846 <https://github.com/zopefoundation/Zope/issues/846>`_)

Expand Down
23 changes: 12 additions & 11 deletions src/OFS/CopySupport.py
Expand Up @@ -23,8 +23,6 @@
from zlib import decompressobj

import six
from six.moves.urllib.parse import quote
from six.moves.urllib.parse import unquote

import transaction
from AccessControl import ClassSecurityInfo
Expand Down Expand Up @@ -56,6 +54,14 @@
from zope.lifecycleevent import ObjectMovedEvent


try:
from base64 import decodebytes
from base64 import encodebytes
except ImportError: # Python 2
from base64 import decodestring as decodebytes
from base64 import encodestring as encodebytes


class CopyError(Exception):
pass

Expand Down Expand Up @@ -671,10 +677,7 @@ def _cb_encode(d):
json_bytes = dumps(d).encode('utf-8')
squashed_bytes = compress(json_bytes, 2) # -> bytes w/ useful encoding
# quote for embeding in cookie
if six.PY2:
return quote(squashed_bytes)
else:
return quote(squashed_bytes.decode('latin-1'))
return encodebytes(squashed_bytes)


def _cb_decode(s, maxsize=8192):
Expand All @@ -686,11 +689,9 @@ def _cb_decode(s, maxsize=8192):
Return a list of text IDs.
"""
dec = decompressobj()
if six.PY2:
squashed = unquote(s)
else:
squashed = unquote(s).encode('latin-1')
data = dec.decompress(squashed, maxsize)
if six.PY3 and isinstance(s, str):
s = s.encode('latin-1')
data = dec.decompress(decodebytes(s), maxsize)
if dec.unconsumed_tail:
raise ValueError
json_bytes = data.decode('utf-8')
Expand Down

0 comments on commit 8171f85

Please sign in to comment.