tor-browser

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

cache.py (2455B)


      1 from wptserve.utils import isomorphic_decode
      2 
      3 def main(request, response):
      4    token = request.GET.first(b"token", None)
      5    if b"querystate" in request.GET:
      6        from json import JSONEncoder
      7        response.headers.set(b"Content-Type", b"text/plain")
      8        return JSONEncoder().encode(request.server.stash.take(token))
      9    content = request.GET.first(b"content", None)
     10    tag = request.GET.first(b"tag", None)
     11    date = request.GET.first(b"date", None)
     12    expires = request.GET.first(b"expires", None)
     13    vary = request.GET.first(b"vary", None)
     14    cc = request.GET.first(b"cache_control", None)
     15    redirect = request.GET.first(b"redirect", None)
     16    inm = request.headers.get(b"If-None-Match", None)
     17    ims = request.headers.get(b"If-Modified-Since", None)
     18    pragma = request.headers.get(b"Pragma", None)
     19    cache_control = request.headers.get(b"Cache-Control", None)
     20    ignore = b"ignore" in request.GET
     21 
     22    if tag:
     23        tag = b'"%s"' % tag
     24 
     25    server_state = request.server.stash.take(token)
     26    if not server_state:
     27        server_state = []
     28    state = dict()
     29    if not ignore:
     30        if inm:
     31            state[u"If-None-Match"] = isomorphic_decode(inm)
     32        if ims:
     33            state[u"If-Modified-Since"] = isomorphic_decode(ims)
     34        if pragma:
     35            state[u"Pragma"] = isomorphic_decode(pragma)
     36        if cache_control:
     37            state[u"Cache-Control"] = isomorphic_decode(cache_control)
     38    server_state.append(state)
     39    request.server.stash.put(token, server_state)
     40 
     41    if tag:
     42        response.headers.set(b"ETag", b'%s' % tag)
     43    elif date:
     44        response.headers.set(b"Last-Modified", date)
     45    if expires:
     46        response.headers.set(b"Expires", expires)
     47    if vary:
     48        response.headers.set(b"Vary", vary)
     49    if cc:
     50        response.headers.set(b"Cache-Control", cc)
     51 
     52    # The only-if-cached redirect tests wants CORS to be okay, the other tests
     53    # are all same-origin anyways and don't care.
     54    response.headers.set(b"Access-Control-Allow-Origin", b"*")
     55 
     56    if redirect:
     57        response.headers.set(b"Location", redirect)
     58        response.status = (302, b"Redirect")
     59        return b""
     60    elif ((inm is not None and inm == tag) or
     61          (ims is not None and ims == date)):
     62        response.status = (304, b"Not Modified")
     63        return b""
     64    else:
     65        response.status = (200, b"OK")
     66        response.headers.set(b"Content-Type", b"text/plain")
     67        return content