multiple-early-hints-responses.h2.py (1531B)
1 import os 2 3 4 def handle_headers(frame, request, response): 5 # Send two Early Hints responses. 6 7 first_preload = request.GET.first(b"first-preload").decode() 8 link_header_value = "<{}>; rel=preload; as=script".format(first_preload) 9 early_hints = [ 10 (b":status", b"103"), 11 (b"content-security-policy", "script-src 'self' 'unsafe-inline'"), 12 (b"link", link_header_value), 13 ] 14 response.writer.write_raw_header_frame(headers=early_hints, 15 end_headers=True) 16 17 second_preload = request.GET.first(b"second-preload").decode() 18 link_header_value = "<{}>; rel=preload; as=script".format(second_preload) 19 second_preload_origin = request.GET.first(b"second-preload-origin").decode() 20 csp_value = "script-src 'self' 'unsafe-inline' {}".format(second_preload_origin) 21 early_hints = [ 22 (b":status", b"103"), 23 (b"content-security-policy", csp_value), 24 (b"link", link_header_value), 25 ] 26 response.writer.write_raw_header_frame(headers=early_hints, 27 end_headers=True) 28 29 response.status = 200 30 response.headers[b"content-type"] = "text/html" 31 response.write_status_headers() 32 33 34 def main(request, response): 35 current_dir = os.path.dirname(os.path.realpath(__file__)) 36 file_path = os.path.join(current_dir, "multiple-early-hints-responses.html") 37 with open(file_path, "r") as f: 38 test_content = f.read() 39 response.writer.write_data(item=test_content, last=True)