preconnect-in-early-hints.h2.py (894B)
1 import os 2 3 4 def handle_headers(frame, request, response): 5 # Send a 103 response. 6 resource_origin = request.GET.first(b"resource-origin").decode() 7 link_header_value = "<{}>; rel=preconnect".format(resource_origin) 8 early_hints = [ 9 (b":status", b"103"), 10 (b"link", link_header_value), 11 ] 12 response.writer.write_raw_header_frame(headers=early_hints, 13 end_headers=True) 14 15 # Send the final response header. 16 response.status = 200 17 response.headers["content-type"] = "text/html" 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, "preconnect-in-early-hints.html") 24 with open(file_path, "r") as f: 25 test_content = f.read() 26 response.writer.write_data(item=test_content, last=True)