compressed-data.py (2080B)
1 def main(request, response): 2 response.headers.set(b"Content-Type", b"text/plain") 3 4 # `dcb_data` and `dcz_data` are generated using the following commands: 5 # 6 # $ echo "This is a test dictionary." > /tmp/dict 7 # $ echo -n "This is compressed test data using a test dictionary" \ 8 # > /tmp/data 9 # 10 # $ echo -en '\xffDCB' > /tmp/out.dcb 11 # $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcb 12 # $ brotli --stdout -D /tmp/dict /tmp/data >> /tmp/out.dcb 13 # $ xxd -p /tmp/out.dcb | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 14 dcb_data = b"\xff\x44\x43\x42\x53\x96\x9b\xcf\x5e\x96\x0e\x0e\xdb\xf0\xa4\xbd\xde\x6b\x0b\x3e\x93\x81\xe1\x56\xde\x7f\x5b\x91\xce\x83\x91\x62\x42\x70\xf4\x16\xa1\x98\x01\x80\x62\xa4\x4c\x1d\xdf\x12\x84\x8c\xae\xc2\xca\x60\x22\x07\x6e\x81\x05\x14\xc9\xb7\xc3\x44\x8e\xbc\x16\xe0\x15\x0e\xec\xc1\xee\x34\x33\x3e\x0d" 15 # $ echo -en '\x5e\x2a\x4d\x18\x20\x00\x00\x00' > /tmp/out.dcz 16 # $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcz 17 # $ zstd -D /tmp/dict -f -o /tmp/tmp.zstd /tmp/data 18 # $ cat /tmp/tmp.zstd >> /tmp/out.dcz 19 # $ xxd -p /tmp/out.dcz | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 20 dcz_data = b"\x5e\x2a\x4d\x18\x20\x00\x00\x00\x53\x96\x9b\xcf\x5e\x96\x0e\x0e\xdb\xf0\xa4\xbd\xde\x6b\x0b\x3e\x93\x81\xe1\x56\xde\x7f\x5b\x91\xce\x83\x91\x62\x42\x70\xf4\x16\x28\xb5\x2f\xfd\x24\x34\xf5\x00\x00\x98\x63\x6f\x6d\x70\x72\x65\x73\x73\x65\x64\x61\x74\x61\x20\x75\x73\x69\x6e\x67\x03\x00\x59\xf9\x73\x54\x46\x27\x26\x10\x9e\x99\xf2\xbc" 21 22 if b'content_encoding' in request.GET: 23 content_encoding = request.GET.first(b"content_encoding") 24 response.headers.set(b"Content-Encoding", content_encoding) 25 if content_encoding == b"dcb": 26 # Send the pre compressed file 27 response.content = dcb_data 28 if content_encoding == b"dcz": 29 # Send the pre compressed file 30 response.content = dcz_data 31 32 if b'allow_origin' in request.GET: 33 response.headers.set(b'access-control-allow-origin', request.GET.first(b'allow_origin'))