tor-browser

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

fetch-access-control.py (5128B)


      1 import json
      2 import os
      3 from base64 import decodebytes
      4 
      5 from wptserve.utils import isomorphic_decode, isomorphic_encode
      6 
      7 def main(request, response):
      8    headers = []
      9    headers.append((b'X-ServiceWorker-ServerHeader', b'SetInTheServer'))
     10 
     11    if b"ACAOrigin" in request.GET:
     12        for item in request.GET[b"ACAOrigin"].split(b","):
     13            headers.append((b"Access-Control-Allow-Origin", item))
     14 
     15    for suffix in [b"Headers", b"Methods", b"Credentials"]:
     16        query = b"ACA%s" % suffix
     17        header = b"Access-Control-Allow-%s" % suffix
     18        if query in request.GET:
     19            headers.append((header, request.GET[query]))
     20 
     21    if b"ACEHeaders" in request.GET:
     22        headers.append((b"Access-Control-Expose-Headers", request.GET[b"ACEHeaders"]))
     23 
     24    if (b"Auth" in request.GET and not request.auth.username) or b"AuthFail" in request.GET:
     25        status = 401
     26        headers.append((b'WWW-Authenticate', b'Basic realm="Restricted"'))
     27        body = b'Authentication canceled'
     28        return status, headers, body
     29 
     30    if b"PNGIMAGE" in request.GET:
     31        headers.append((b"Content-Type", b"image/png"))
     32        body = decodebytes(b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1B"
     33                           b"AACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAhSURBVDhPY3wro/KfgQLABKXJBqMG"
     34                           b"jBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=")
     35        return headers, body
     36 
     37    if b"VIDEO" in request.GET:
     38        if b"mp4" in request.GET:
     39          headers.append((b"Content-Type", b"video/mp4"))
     40          body = open(os.path.join(request.doc_root, u"media", u"movie_5.mp4"), "rb").read()
     41        else:
     42          headers.append((b"Content-Type", b"video/webm"))
     43          body = open(os.path.join(request.doc_root, u"media", u"movie_5.webm"), "rb").read()
     44 
     45        length = len(body)
     46        # If "PartialContent" is specified, the requestor wants to test range
     47        # requests. For the initial request, respond with "206 Partial Content"
     48        # and don't send the entire content. Then expect subsequent requests to
     49        # have a "Range" header with a byte range. Respond with that range.
     50        if b"PartialContent" in request.GET:
     51          if length < 1:
     52            return 500, headers, b"file is too small for range requests"
     53          start = 0
     54          end = length - 1
     55          if b"Range" in request.headers:
     56            range_header = request.headers[b"Range"]
     57            prefix = b"bytes="
     58            split_header = range_header[len(prefix):].split(b"-")
     59            # The first request might be "bytes=0-". We want to force a range
     60            # request, so just return the first byte.
     61            if split_header[0] == b"0" and split_header[1] == b"":
     62              end = start
     63            # Otherwise, it is a range request. Respect the values sent.
     64            if split_header[0] != b"":
     65              start = int(split_header[0])
     66            if split_header[1] != b"":
     67              end = int(split_header[1])
     68          else:
     69            # The request doesn't have a range. Force a range request by
     70            # returning the first byte.
     71            end = start
     72 
     73          headers.append((b"Accept-Ranges", b"bytes"))
     74          headers.append((b"Content-Length", isomorphic_encode(str(end -start + 1))))
     75          headers.append((b"Content-Range", b"bytes %d-%d/%d" % (start, end, length)))
     76          chunk = body[start:(end + 1)]
     77          return 206, headers, chunk
     78        return headers, body
     79 
     80    username = request.auth.username if request.auth.username else b"undefined"
     81    password = request.auth.password if request.auth.username else b"undefined"
     82    cookie = request.cookies[b'cookie'].value if b'cookie' in request.cookies else b"undefined"
     83 
     84    files = []
     85    for key, values in request.POST.items():
     86        assert len(values) == 1
     87        value = values[0]
     88        if not hasattr(value, u"file"):
     89            continue
     90        data = value.file.read()
     91        files.append({u"key": isomorphic_decode(key),
     92                      u"name": value.file.name,
     93                      u"type": value.type,
     94                      u"error": 0, #TODO,
     95                      u"size": len(data),
     96                      u"content": data})
     97 
     98    get_data = {isomorphic_decode(key):isomorphic_decode(request.GET[key]) for key, value in request.GET.items()}
     99    post_data = {isomorphic_decode(key):isomorphic_decode(request.POST[key]) for key, value in request.POST.items()
    100                 if not hasattr(request.POST[key], u"file")}
    101    headers_data = {isomorphic_decode(key):isomorphic_decode(request.headers[key]) for key, value in request.headers.items()}
    102 
    103    data = {u"jsonpResult": u"success",
    104            u"method": request.method,
    105            u"headers": headers_data,
    106            u"body": isomorphic_decode(request.body),
    107            u"files": files,
    108            u"GET": get_data,
    109            u"POST": post_data,
    110            u"username": isomorphic_decode(username),
    111            u"password": isomorphic_decode(password),
    112            u"cookie": isomorphic_decode(cookie)}
    113 
    114    return headers, u"report( %s )" % json.dumps(data)