register-dictionary.py (4240B)
1 import json 2 3 def main(request, response): 4 response.headers.set(b"Access-Control-Allow-Origin", b"*") 5 match = b"/fetch/compression-dictionary/resources/*" 6 content = b"This is a test dictionary.\n" 7 max_age = b"3600" 8 if b"match" in request.GET: 9 match = request.GET.first(b"match") 10 if b"content" in request.GET: 11 content = request.GET.first(b"content") 12 if b"max-age" in request.GET: 13 max_age = request.GET.first(b"max-age") 14 15 token = request.GET.first(b"save_header", None) 16 if token is not None: 17 headers = {} 18 for header in request.headers: 19 key = header.decode('utf-8') 20 value = request.headers.get(header).decode('utf-8') 21 headers[key] = value 22 with request.server.stash.lock: 23 request.server.stash.put(token, json.dumps(headers)) 24 25 previous_token = request.GET.first(b"get_previous_header", None) 26 if previous_token is not None: 27 result = {} 28 with request.server.stash.lock: 29 store = request.server.stash.take(previous_token) 30 if store is not None: 31 headers = json.loads(store) 32 result["headers"] = headers 33 return json.dumps(result) 34 35 options = b"match=\"" + match + b"\"" 36 if b"id" in request.GET: 37 options += b", id=\"" + request.GET.first(b"id") + b"\"" 38 response.headers.set(b"Use-As-Dictionary", options) 39 response.headers.set(b"Cache-Control", b"max-age=" + max_age) 40 if b"age" in request.GET: 41 response.headers.set(b"Age", request.GET.first(b"age")) 42 response.headers.set(b"Vary", b"available-dictionary,accept-encoding") 43 44 # Compressed responses are generated using the following commands: 45 # 46 # $ echo "This is a test dictionary." > /tmp/dict 47 # $ echo "This is a test dictionary." > /tmp/data 48 # 49 # $ gzip < /tmp/data > /tmp/out.gz 50 # $ xxd -p /tmp/out.gz | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 51 gzip_data = b"\x1f\x8b\x08\x00\x10\x31\x47\x68\x00\x03\x0b\xc9\xc8\x2c\x56\x00\xa2\x44\x85\x92\xd4\xe2\x12\x85\x94\xcc\xe4\x92\xcc\xfc\xbc\xc4\xa2\x4a\x3d\x2e\x00\x79\xf2\x36\x63\x1b\x00\x00\x00" 52 # $ brotli -o /tmp/out.br /tmp/data 53 # $ xxd -p /tmp/out.br | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 54 br_data = b"\xa1\xd0\x00\xc0\x6f\xa4\x74\xf3\x56\xb5\x02\x48\x18\x9d\x2a\x9b\xcb\x42\x14\x81\xa7\x14\xda\x89\x29\x93\x7b\xc8\x09" 55 # $ zstd -f -o /tmp/out.zstd /tmp/data 56 # $ xxd -p /tmp/out.zstd | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 57 zstd_data = b"\x28\xb5\x2f\xfd\x24\x1b\xd9\x00\x00\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x64\x69\x63\x74\x69\x6f\x6e\x61\x72\x79\x2e\x0a\x3f\x0d\x76\xa0" 58 # $ echo -en '\xffDCB' > /tmp/out.dcb 59 # $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcb 60 # $ brotli --stdout -D /tmp/dict /tmp/data >> /tmp/out.dcb 61 # $ xxd -p /tmp/out.dcb | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 62 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\xd0\x00\xc0\x2f\x01\x10\xc4\x84\x0a\x05" 63 # $ echo -en '\x5e\x2a\x4d\x18\x20\x00\x00\x00' > /tmp/out.dcz 64 # $ openssl dgst -sha256 -binary /tmp/dict >> /tmp/out.dcz 65 # $ zstd -D /tmp/dict -f -o /tmp/tmp.zstd /tmp/data 66 # $ cat /tmp/tmp.zstd >> /tmp/out.dcz 67 # $ xxd -p /tmp/out.dcz | tr -d '\n' | sed 's/\(..\)/\\x\1/g' 68 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\x1b\x35\x00\x00\x00\x01\x00\x1e\x4e\x20\x3f\x0d\x76\xa0" 69 70 if b'content_encoding' in request.GET: 71 content_encoding = request.GET.first(b"content_encoding") 72 response.headers.set(b"Content-Encoding", content_encoding) 73 if content_encoding == b"gzip": 74 content = gzip_data 75 elif content_encoding == b"br": 76 content = br_data 77 elif content_encoding == b"zstd": 78 content = zstd_data 79 elif content_encoding == b"dcb": 80 content = dcb_data 81 elif content_encoding == b"dcz": 82 content = dcz_data 83 84 return content