summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/eventsource/resources/cors-cookie.py
blob: 7deaff498d7c0c077f6417cd725725ca55039b96 (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
from datetime import datetime

def main(request, response):
    last_event_id = request.headers.get("Last-Event-Id", "")
    ident = request.GET.first('ident', "test")
    cookie = "COOKIE" if ident in request.cookies else "NO_COOKIE"
    origin = request.GET.first('origin', request.headers["origin"])
    credentials = request.GET.first('credentials', 'true')

    headers = []

    if origin != 'none':
        headers.append(("Access-Control-Allow-Origin", origin));

    if credentials != 'none':
        headers.append(("Access-Control-Allow-Credentials", credentials));

    if last_event_id == '':
        headers.append(("Content-Type", "text/event-stream"))
        response.set_cookie(ident, "COOKIE")
        data = "id: 1\nretry: 200\ndata: first %s\n\n" % cookie
    elif last_event_id == '1':
        headers.append(("Content-Type", "text/event-stream"))
        long_long_time_ago = datetime.now().replace(year=2001, month=7, day=27)
        response.set_cookie(ident, "COOKIE", expires=long_long_time_ago)
        data = "id: 2\ndata: second %s\n\n" % cookie
    else:
        headers.append(("Content-Type", "stop"))
        data = "data: " + last_event_id + cookie + "\n\n";

    return headers, data