summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/metadata/resources/echo-as-json.py
blob: 44f68e8fe9e2025b2940ef2a6cd8390be6376582 (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
import json

from wptserve.utils import isomorphic_decode

def main(request, response):
    headers = [(b"Content-Type", b"application/json"),
               (b"Access-Control-Allow-Credentials", b"true")]

    if b"origin" in request.headers:
        headers.append((b"Access-Control-Allow-Origin", request.headers[b"origin"]))

    body = u""

    # If we're in a preflight, verify that `Sec-Fetch-Mode` is `cors`.
    if request.method == u'OPTIONS':
        if request.headers.get(b"sec-fetch-mode") != b"cors":
            return (403, b"Failed"), [], body

        headers.append((b"Access-Control-Allow-Methods", b"*"))
        headers.append((b"Access-Control-Allow-Headers", b"*"))
    else:
        body = json.dumps({
            u"dest": isomorphic_decode(request.headers.get(b"sec-fetch-dest", b"")),
            u"mode": isomorphic_decode(request.headers.get(b"sec-fetch-mode", b"")),
            u"site": isomorphic_decode(request.headers.get(b"sec-fetch-site", b"")),
            u"user": isomorphic_decode(request.headers.get(b"sec-fetch-user", b"")),
            })

    return headers, body