service-worker-header.py (795B)
1 def main(request, response): 2 service_worker_header = request.headers.get(b'service-worker') 3 4 if b'header' in request.GET and service_worker_header != b'script': 5 return 400, [(b'Content-Type', b'text/plain')], b'Bad Request' 6 7 if b'no-header' in request.GET and service_worker_header == b'script': 8 return 400, [(b'Content-Type', b'text/plain')], b'Bad Request' 9 10 # no-cache itself to ensure the user agent finds a new version for each 11 # update. 12 headers = [(b'Cache-Control', b'no-cache, must-revalidate'), 13 (b'Pragma', b'no-cache'), 14 (b'Content-Type', b'application/javascript')] 15 body = b'/* This is a service worker script */\n' 16 17 if b'import' in request.GET: 18 body += b"importScripts('%s');" % request.GET[b'import'] 19 20 return 200, headers, body