gzip_xml.py (774B)
1 import gzip as gzip_module 2 import os 3 4 from io import BytesIO 5 6 from wptserve.utils import isomorphic_decode 7 8 def main(request, response): 9 dir_path = os.path.dirname(os.path.realpath(isomorphic_decode(__file__))) 10 file_path = os.path.join(dir_path, u'resource_timing_test0.xml') 11 f = open(file_path, u'rb') 12 output = f.read() 13 14 out = BytesIO() 15 with gzip_module.GzipFile(fileobj=out, mode="w") as f: 16 f.write(output) 17 output = out.getvalue() 18 19 headers = [(b"Content-type", b"text/plain"), 20 (b"Content-Encoding", b"gzip"), 21 (b"Content-Length", len(output))] 22 23 if b'allow_origin' in request.GET: 24 headers.append((b'access-control-allow-origin', request.GET.first(b'allow_origin'))) 25 26 return headers, output