tor-browser

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

executor-window.py (1433B)


      1 import html
      2 import json
      3 from urllib import parse
      4 
      5 def main(request, response):
      6  initRequestHeaders = ""
      7  for header_name in request.headers.keys():
      8    for header_value in request.headers.get_list(header_name):
      9      js_name = json.dumps(header_name.lower().decode("utf-8"))
     10      js_value = json.dumps(header_value.decode("utf-8"))
     11      initRequestHeaders += f"window.__requestHeaders.append({js_name}, {js_value});\n"
     12      if (b"status" in request.GET):
     13            status = int(request.GET.first(b"status"))
     14      else:
     15            status = 200
     16  query = parse.parse_qs(request.url_parts.query)
     17  scripts = []
     18  for script in query.get("script", []):
     19    scripts.append(f"<script src='{html.escape(script)}'></script>")
     20  scripts_s = "\n".join(scripts)
     21 
     22  uuid = query.get("uuid")[0]
     23 
     24  start_on = query.get("startOn")
     25  start_on_s = f"'{start_on[0]}'" if start_on else "null"
     26 
     27  headers = [("Content-Type", "text/html")]
     28 
     29  # This sets a base href so that even if this content e.g. data or blob URLs
     30  # document, relative URLs will resolve.
     31  return (status, headers, f"""
     32 <!DOCTYPE HTML>
     33 <base href="{html.escape(request.url)}">
     34 <script src="/common/dispatcher/dispatcher.js"></script>
     35 <script src="./executor-common.js"></script>
     36 <script src="./executor-window.js"></script>
     37 
     38 {scripts_s}
     39 <body>
     40 <script>
     41 window.__requestHeaders = new Headers();
     42 {initRequestHeaders}
     43 requestExecutor("{uuid}", {start_on_s});
     44 </script>
     45 """)