summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/echo-cookie-worker.py
blob: 561f64a35ad42ec326a52bb3b1f6334583356d00 (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
def main(request, response):
    headers = [(b"Content-Type", b"text/javascript")]

    values = []
    for key in request.cookies:
        for cookie in request.cookies.get_list(key):
            values.append(b'"%s": "%s"' % (key, cookie.value))

    # Update the counter to change the script body for every request to trigger
    # update of the service worker.
    key = request.GET[b'key']
    counter = request.server.stash.take(key)
    if counter is None:
        counter = 0
    counter += 1
    request.server.stash.put(key, counter)

    body = b"""
// %d
self.addEventListener('message', e => {
  e.source.postMessage({%s})
});""" % (counter, b','.join(values))

    return headers, body