tor-browser

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

get_beacon.py (1118B)


      1 """An HTTP request handler for WPT that handles /get_beacon.py requests."""
      2 
      3 import json
      4 
      5 _BEACON_ID_KEY = b"uuid"
      6 _BEACON_DATA_PATH = "beacon_data"
      7 
      8 
      9 def main(request, response):
     10    """Retrieves the beacon data keyed by the given uuid from server storage.
     11 
     12    The response content is a JSON string in one of the following formats:
     13      - "{'data': ['abc', null, '123',...]}"
     14      - "{'data': []}" indicates that no data has been set for this uuid.
     15    """
     16    if _BEACON_ID_KEY not in request.GET:
     17        response.status = 400
     18        return "Must provide a UUID to store beacon data"
     19    uuid = request.GET.first(_BEACON_ID_KEY)
     20 
     21    with request.server.stash.lock:
     22        body = {'data': []}
     23        data = request.server.stash.take(key=uuid, path=_BEACON_DATA_PATH)
     24        if data:
     25            body['data'] = data
     26            # The stash is read-once/write-once, so it has to be put back after
     27            # reading if `data` is not None.
     28            request.server.stash.put(
     29                key=uuid, value=data, path=_BEACON_DATA_PATH)
     30        return [(b'Content-Type', b'text/plain')], json.dumps(body)