tor-browser

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

percent-encoding.py (990B)


      1 import base64
      2 from wptserve.utils import isomorphic_decode
      3 
      4 # Use numeric references to let the HTML parser take care of inserting the correct code points
      5 # rather than trying to figure out the necessary bytes for each encoding. (The latter can be
      6 # especially tricky given that Python does not implement the Encoding Standard.)
      7 def numeric_references(input):
      8    output = b""
      9    for cp in input:
     10        output += b"&#x" + format(ord(cp), u"X").encode(u"utf-8") + b";"
     11    return output
     12 
     13 def main(request, response):
     14    # Undo the "magic" space with + replacement as otherwise base64 decoding will fail.
     15    value = request.GET.first(b"value").replace(b" ", b"+")
     16    encoding = request.GET.first(b"encoding")
     17 
     18    output_value = numeric_references(base64.b64decode(value).decode(u"utf-8"))
     19    return (
     20        [(b"Content-Type", b"text/html;charset=" + encoding)],
     21        b"""<!doctype html>
     22 <a href="https://doesnotmatter.invalid/?%s#%s">test</a>
     23 """ % (output_value, output_value))