Use normal loading path for ping loads
[WebKit-https.git] / LayoutTests / http / wpt / beacon / resources / beacon-preflight.py
1 import json
2
3 def respondToCORSPreflight(request, response):
4   headers = [("Content-Type", "text/plain")]
5   allow_cors = int(request.GET.first("allowCors", 0)) != 0;
6   
7   if not allow_cors:
8     response.set_error(400, "Not allowed")
9     return headers, "ERROR: Not allowed"
10   
11   if not "Access-Control-Request-Method" in request.headers:
12     response.set_error(400, "No Access-Control-Request-Method header")
13     return headers, "ERROR: No access-control-request-method in preflight!"
14   
15   headers.append(("Access-Control-Allow-Origin", request.headers.get("Origin", "*")))
16   headers.append(("Access-Control-Allow-Credentials", "true"))
17   requested_method = request.headers.get("Access-Control-Request-Method", None)
18   headers.append(("Access-Control-Allow-Methods", requested_method))
19   requested_headers = request.headers.get("Access-Control-Request-Headers", None)
20   headers.append(("Access-Control-Allow-Headers", requested_headers))
21   headers.append(("Access-Control-Max-Age", "60"))
22   return headers, ""
23
24 def main(request, response):
25   command = request.GET.first("cmd").lower();
26   test_id = request.GET.first("id")
27   stashed_data = request.server.stash.take(test_id)
28   if stashed_data is None:
29     stashed_data = { 'preflight': 0, 'beacon': 0, 'preflight_requested_method': '', 'preflight_requested_headers': '', 'preflight_referrer': '', 'preflight_cookie_header': '', 'beacon_cookie_header': '' }
30
31   if command == "put":
32     if request.method == "OPTIONS":
33       stashed_data['preflight'] = 1;
34       stashed_data['preflight_requested_method'] = request.headers.get("Access-Control-Request-Method", "")
35       stashed_data['preflight_requested_headers'] = request.headers.get("Access-Control-Request-Headers", "")
36       stashed_data['preflight_cookie_header'] = request.headers.get("Cookie", "");
37       stashed_data['preflight_referer'] = request.headers.get("Referer", "")
38       stashed_data['preflight_origin'] = request.headers.get("Origin", "")
39       request.server.stash.put(test_id, stashed_data)
40       return respondToCORSPreflight(request, response)
41     elif request.method == "POST":
42       stashed_data['beacon'] = 1;
43       stashed_data['beacon_cookie_header'] = request.headers.get("Cookie", "")
44       stashed_data['beacon_origin'] = request.headers.get("Origin", "")
45       request.server.stash.put(test_id, stashed_data)
46     return [("Content-Type", "text/plain")], ""
47   
48   if command == "get":
49     if stashed_data is not None:
50       return [("Content-Type", "text/plain")], json.dumps(stashed_data)
51     return [("Content-Type", "text/plain")], ""
52
53   response.set_error(400, "Bad Command")
54   return [("Content-Type", "text/plain")], "ERROR: Bad Command!"