summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/client-hints/critical-ch/resources/echo-critical-hint.py
blob: a5c5160f7503ea3644b6d2deb9827994025c1af9 (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
46
47
48
49
50
51
52
import sys

def main(request, response):
    """
    Simple handler that sets a response header based on which client hint
    request headers were received.
    """

    response.headers.append(b"Content-Type", b"text/html; charset=UTF-8")
    response.headers.append(b"Access-Control-Allow-Origin", b"*")
    response.headers.append(b"Access-Control-Allow-Headers", b"*")
    response.headers.append(b"Access-Control-Expose-Headers", b"*")

    accept = b"sec-ch-device-memory,device-memory"
    if(request.GET.first(b"multiple", None) is not None):
      for accept_part in accept.split(b","):
        response.headers.append(b"Accept-CH", accept_part)
    else:
      response.headers.append(b"Accept-CH", accept)

    critical = b"sec-ch-device-memory,device-memory"
    if(request.GET.first(b"mismatch", None) is not None):
      critical = b"sec-ch-viewport-width,viewport-width"

    if(request.GET.first(b"multiple", None) is not None):
      for critical_part in critical.split(b","):
        response.headers.append(b"Critical-CH", critical_part)
    else:
      response.headers.append(b"Critical-CH", critical)

    response.headers.append(b"Cache-Control", b"no-store")

    result = "FAIL"

    if b"sec-ch-device-memory" in request.headers and b"device-memory" in request.headers:
      result = "PASS"

    token = request.GET.first(b"token", None)
    if(token is not None):
      with request.server.stash.lock:
        count = request.server.stash.take(token)
        if(count == None):
          count = 1
        else:
          count += 1
        request.server.stash.put(token, count)
        result = str(count)

    if b"sec-ch-viewport-width" in request.headers and b"viewport-width" in request.headers:
      result = "MISMATCH"

    response.content = "<script>(window.opener || window.top).postMessage('{0}', '*')</script>".format(result)