tor-browser

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

additional-bids.py (2009B)


      1 """Endpoint to return signed additional bids in the appropriate response header.
      2 
      3 Additional bids are returned using the "Ad-Auction-Additional-Bid" response
      4 header, as described at
      5 https://github.com/WICG/turtledove/blob/main/FLEDGE.md#63-http-response-headers.
      6 
      7 This script generates an "Ad-Auction-Additional-Bid" response header for each of
      8 the pre-formatted additional bid header values provided in a JSON list-valued
      9 `additionalBidHeaderValues` query parameter.
     10 
     11 All requests to this endpoint requires a "Sec-Ad-Auction-Fetch" request header
     12 with a value of b"?1"; this entrypoint otherwise returns a 400 response.
     13 """
     14 
     15 import json
     16 
     17 import fledge.tentative.resources.fledge_http_server_util as fledge_http_server_util
     18 
     19 
     20 class BadRequestError(Exception):
     21  pass
     22 
     23 
     24 def main(request, response):
     25  try:
     26    if fledge_http_server_util.handle_cors_headers_fail_if_preflight(request, response):
     27      return
     28 
     29    # Verify that Sec-Ad-Auction-Fetch is present
     30    if request.headers.get("Sec-Ad-Auction-Fetch", default=b"").decode("utf-8") != "?1":
     31      raise BadRequestError("Sec-Ad-Auction-Fetch missing or unexpected value; expected '?1'")
     32 
     33    # Return each additional bid in its own header
     34    additional_bid_header_values = request.GET.get(b"additionalBidHeaderValues", default=b"").decode("utf-8")
     35    if not additional_bid_header_values:
     36      raise BadRequestError("Missing 'additionalBidHeaderValues' parameter")
     37    for additional_bid_header_value in json.loads(additional_bid_header_values):
     38      response.headers.append(
     39          b"Ad-Auction-Additional-Bid", additional_bid_header_value.encode("utf-8"))
     40 
     41    response.status = (200, b"OK")
     42    response.headers.set(b"Content-Type", b"text/plain")
     43 
     44  except BadRequestError as error:
     45    response.status = (400, b"Bad Request")
     46    response.headers.set(b"Content-Type", b"text/plain")
     47    response.content = str(error)
     48 
     49  except Exception as exception:
     50    response.status = (500, b"Internal Server Error")
     51    response.content = str(exception)