tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

update-worker-from-file.py (983B)


      1 import os
      2 
      3 from wptserve.utils import isomorphic_encode
      4 
      5 def serve_js_from_file(request, response, filename):
      6  body = b''
      7  path = os.path.join(os.path.dirname(isomorphic_encode(__file__)), filename)
      8  with open(path, 'rb') as f:
      9    body = f.read()
     10  return (
     11    [
     12      (b'Cache-Control', b'no-cache, must-revalidate'),
     13      (b'Pragma', b'no-cache'),
     14      (b'Content-Type', b'application/javascript')
     15    ], body)
     16 
     17 def main(request, response):
     18  key = request.GET[b"Key"]
     19 
     20  visited_count = request.server.stash.take(key)
     21  if visited_count is None:
     22    visited_count = 0
     23 
     24  # Keep how many times the test requested this resource.
     25  visited_count += 1
     26  request.server.stash.put(key, visited_count)
     27 
     28  # Serve a file based on how many times it's requested.
     29  if visited_count == 1:
     30    return serve_js_from_file(request, response, request.GET[b"First"])
     31  if visited_count == 2:
     32    return serve_js_from_file(request, response, request.GET[b"Second"])
     33  raise u"Unknown state"