Debian Codegraph @

testing/web-platform/mozilla/tests/fetch/fetchpriority/support/resources/priority-dependent-content.py

raw
1 import os
2 import re
3
4
5 def parse_priority(priority):
6 # This is an approximate parsing of the priority header that is good enough
7 # for testing purpose.
8 # https://www.rfc-editor.org/rfc/rfc9218.html#section-5
9 # https://www.rfc-editor.org/rfc/rfc9218.html#name-priority-parameters
10 # https://www.rfc-editor.org/rfc/rfc8941.html#name-dictionaries
11 # https://www.rfc-editor.org/rfc/rfc7230.html#section-3.2.3
12 # https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3.1
13 urgency = 3
14 if priority is not None:
15 for key_value in priority.decode("utf-8").split(","):
16 try:
17 # This server ignores the incremental and unknown parameters.
18 # https://www.rfc-editor.org/rfc/rfc9218.html#section-4.2
19 key_and_value = key_value.strip().split("=", 1)
20 if len(key_and_value) > 1:
21 key, value = key_and_value
22 if key == "u":
23 urgency = min(7, max(0, int(value)))
24 except Exception as e:
25 print(e)
26 return urgency
27
28
29 def get_content_from_file(filename):
30 current_dir = os.path.dirname(os.path.realpath(__file__))
31 file_path = os.path.join(current_dir, filename)
32 return open(file_path, "rb").read()
33
34
35 def get_content(as_type, resource_id, urgency):
36 if as_type == "text":
37 return "text/plain", "id=%s,u=%d\n%s" % (resource_id, urgency, "A" * urgency)
38 if as_type == "script":
39 return (
40 "text/javascript",
41 "/* id=%s,u=%d */\nself.scriptUrgency = self.scriptUrgency || {};\nself.scriptUrgency['%s'] = %d;"
42 % (resource_id, urgency, resource_id, urgency),
43 )
44 size = (urgency + 1) * 10
45 if as_type == "style":
46 return (
47 "text/css",
48 "/* id=%s,u=%d */\n#urgency_square_%s { display: inline-block; width: %dpx; height: %dpx; background: black; }"
49 % (resource_id, urgency, resource_id, size, size),
50 )
51 if as_type == "image":
52 return (
53 "image/svg+xml",
54 '<?xml version="1.0" encoding="UTF-8"?>\n\
55 <!-- id=%s,u=%d -->\n\
56 <svg xmlns="http://www.w3.org/2000/svg" width="%dpx" height="%dpx">\n\
57 <rect width="%dpx" height="%dpx" fill="black"/>\n\
58 </svg>'
59 % (resource_id, urgency, size, size, size, size),
60 )
61 duration_ms = (urgency + 1) * 10
62 if as_type == "audio":
63 return (
64 "audio/opus",
65 get_content_from_file("audio_%dms.opus" % duration_ms),
66 )
67 if as_type == "video":
68 return (
69 "video/webm",
70 get_content_from_file("video_%dms.webm" % duration_ms),
71 )
72 if as_type == "font":
73 return (
74 "font/woff2",
75 get_content_from_file("font_%dem.woff2" % (urgency + 1)),
76 )
77 return None, None
78
79
80 def main(request, response):
81 # Normally, we should never re-use the cache but we sometimes need that
82 # for font tests.
83 use_cache = False
84 if b"use-cache" in request.GET:
85 use_cache = request.GET[b"use-cache"].decode("utf-8") == "true"
86
87 as_type = request.GET[b"as-type"].decode("utf-8")
88 resource_id = request.GET[b"resource-id"].decode("utf-8")
89 if re.match(r"^\w[\w\-]*$", resource_id) is None:
90 response.set_error(400, "Bad resource-id!")
91 return
92
93 urgency = parse_priority(request.headers.get(b"priority"))
94 content_type, content = get_content(as_type, resource_id, urgency)
95
96 if content_type is None:
97 response.set_error(400, "Unsupported as-type=%s!" % as_type)
98 return
99
100 # The server can reply with a priority, but it's probably better not to do
101 # that in case this as any side effect. Instead, WPT tests are expected to
102 # rely on the actual content returned to determine which priority was
103 # received by the server.
104 # https://www.rfc-editor.org/rfc/rfc9218.html#section-5
105 response.status = 200
106 response.headers[b"content-type"] = content_type
107 if not use_cache:
108 response.headers[b"Cache-Control"] = b"no-store"
109 response.write_status_headers()
110 response.writer.write_data(item=content, last=True)