tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pdf-with-early-hints.h2.py (903B)


      1 import os
      2 import time
      3 
      4 def handle_headers(frame, request, response):
      5    resource_url = request.GET.first(b"resource-url").decode()
      6    link_header_value = "<{}>; rel=preload; as=script".format(resource_url)
      7    early_hints = [
      8        (b":status", b"103"),
      9        (b"link", link_header_value),
     10    ]
     11    response.writer.write_raw_header_frame(headers=early_hints,
     12                                           end_headers=True)
     13 
     14    # Sleep to simulate a slow generation of the final response.
     15    time.sleep(0.1)
     16    response.status = 200
     17    response.headers[b"content-type"] = "application/pdf"
     18    response.write_status_headers()
     19 
     20 
     21 def main(request, response):
     22    current_dir = os.path.dirname(os.path.realpath(__file__))
     23    file_path = os.path.join(current_dir, "example.pdf")
     24    with open(file_path, "rb") as f:
     25        content = f.read()
     26    response.writer.write_data(item=content, last=True)