blob: aa9188c6dbc3920055095421bc880ec5595b0bbb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# An HTTP/2 handler for testing Early Hints. Used as an entry point of Early
# Hints related tests to inject Early Hints response. See comments in
# `early-hints-helpers.sub.js`.
import json
import os
import time
def _remove_relative_resources_prefix(path):
if path.startswith("resources/"):
return path[len("resources/"):]
return path
def handle_headers(frame, request, response):
preload_headers = []
for encoded_preload in request.GET.get_list(b"preloads"):
preload = json.loads(encoded_preload.decode("utf-8"))
header = "<{}>; rel=preload; as={}".format(preload["url"], preload["as_attr"])
preload_headers.append(header.encode())
# Send a 103 response.
early_hints = [(b":status", b"103")]
for header in preload_headers:
early_hints.append((b"link", header))
response.writer.write_raw_header_frame(headers=early_hints,
end_headers=True)
# Simulate the response generation is taking time.
time.sleep(0.2)
response.status = 200
response.headers[b"content-type"] = "text/html"
for header in preload_headers:
response.headers.append(b"link", header)
response.write_status_headers()
def main(request, response):
test_path = _remove_relative_resources_prefix(
request.GET[b"test_url"].decode("utf-8"))
current_dir = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(current_dir, test_path)
test_content = open(file_path, "r").read()
response.writer.write_data(item=test_content, last=True)
|