summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prefetch/resources/cookies.py
blob: 3c2299aa3a6787c6c6444fc1b58db1288ef710a4 (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
def main(request, response):
  def get_cookie(key):
    key = key.encode("utf-8")
    if key in request.cookies:
      return f'"{request.cookies[key].value.decode("utf-8")}"'
    else:
      return "undefined"

  purpose = request.headers.get("Purpose", b"").decode("utf-8")
  sec_purpose = request.headers.get("Sec-Purpose", b"").decode("utf-8")

  cookie_count = int(
      request.cookies[b"count"].value) if b"count" in request.cookies else 0
  response.set_cookie("count", f"{cookie_count+1}",
                      secure=True, samesite="None")
  response.set_cookie(
      "type", "prefetch" if sec_purpose.startswith("prefetch") else "navigate")

  headers = [(b"Content-Type", b"text/html")]

  content = f'''
  <!DOCTYPE html>
  <script src="/common/dispatcher/dispatcher.js"></script>
  <script src="utils.sub.js"></script>
  <script>
  window.requestHeaders = {{
    purpose: "{purpose}",
    sec_purpose: "{sec_purpose}"
  }};

  window.requestCookies = {{
    count: {get_cookie("count")},
    type:  {get_cookie("type")}
  }};

  const uuid = new URLSearchParams(location.search).get('uuid');
  window.executor = new Executor(uuid);
  </script>
  '''
  return headers, content