Sync web-platform-tests up to revision a5b95cb31914507088a4eda16f7674bbc6f3313f
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / fetch / api / resources / preflight.py
1 def main(request, response):
2     headers = [("Content-Type", "text/plain")]
3     stashed_data = {'control_request_headers': "", 'preflight': "0", 'preflight_referrer': ""}
4
5     token = None
6     if "token" in request.GET:
7         token = request.GET.first("token")
8
9     if "origin" in request.GET:
10         for origin in request.GET['origin'].split(", "):
11             headers.append(("Access-Control-Allow-Origin", origin))
12     else:
13         headers.append(("Access-Control-Allow-Origin", "*"))
14
15     if "credentials" in request.GET:
16         headers.append(("Access-Control-Allow-Credentials", "true"))
17
18     if request.method == "OPTIONS":
19         if not "Access-Control-Request-Method" in request.headers:
20             response.set_error(400, "No Access-Control-Request-Method header")
21             return "ERROR: No access-control-request-method in preflight!"
22
23         if "control_request_headers" in request.GET:
24             stashed_data['control_request_headers'] = request.headers.get("Access-Control-Request-Headers", None)
25
26         if "max_age" in request.GET:
27             headers.append(("Access-Control-Max-Age", request.GET['max_age']))
28
29         if "allow_headers" in request.GET:
30             headers.append(("Access-Control-Allow-Headers", request.GET['allow_headers']))
31
32         if "allow_methods" in request.GET:
33             headers.append(("Access-Control-Allow-Methods", request.GET['allow_methods']))
34
35         preflight_status = 200
36         if "preflight_status" in request.GET:
37             preflight_status = int(request.GET.first("preflight_status"))
38
39         stashed_data['preflight'] = "1"
40         stashed_data['preflight_referrer'] = request.headers.get("Referer", "")
41         if token:
42             request.server.stash.put(token, stashed_data)
43
44         return preflight_status, headers, ""
45
46
47     if token:
48         data = request.server.stash.take(token)
49         if data:
50             stashed_data = data
51
52     #use x-* headers for returning value to bodyless responses
53     headers.append(("Access-Control-Expose-Headers", "x-did-preflight, x-control-request-headers, x-referrer, x-preflight-referrer, x-origin"))
54     headers.append(("x-did-preflight", stashed_data['preflight']))
55     if stashed_data['control_request_headers'] != None:
56       headers.append(("x-control-request-headers", stashed_data['control_request_headers']))
57     headers.append(("x-preflight-referrer", stashed_data['preflight_referrer']))
58     headers.append(("x-referrer", request.headers.get("Referer", "") ))
59     headers.append(("x-origin", request.headers.get("Origin", "") ))
60
61     if token:
62       request.server.stash.put(token, stashed_data)
63
64     return headers, ""