tor-browser

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

video-with-aged-proxy-cache.py (2525B)


      1 import re
      2 import os
      3 import json
      4 from wptserve.utils import isomorphic_decode
      5 
      6 ENTITY_TAG = b'"sine440-v1"'
      7 # There should be two requests made to this resource:
      8 # 1. A request fetch the whole video. This request should receive a
      9 #    200 Success response with a aged header of value more
     10 #    than 24 hours in the past, simulating an aged proxy cache.
     11 # 2.a A subsequent request with a Range header to revalidate the cached content.
     12 #    This request should include an If-Modified-Since / If-Not-Match header
     13 #    and should response with a 304 Not Modified response.
     14 # 2.b A subsequent request with a Range header to fetch a range of the video.
     15 #    But without Http-Cache revalidation related header.
     16 #    This request should receive a 206 Partial Content response with the
     17 #    requested range of the video.
     18 
     19 def main(request, response):
     20    path = os.path.join(request.doc_root, u"media", "sine440.mp3")
     21    total_size = os.path.getsize(path)
     22    if_modified_since = request.headers.get(b'If-Modified-Since')
     23    if_none_match = request.headers.get(b'If-Not-Match')
     24    range_header = request.headers.get(b'Range')
     25    range_header_match = range_header and re.search(r'^bytes=(\d*)-(\d*)$', isomorphic_decode(range_header))
     26 
     27    if range_header_match:
     28        start, end = range_header_match.groups()
     29        start = int(start or 0)
     30        end = int(end or total_size)
     31        status = 206
     32    elif if_modified_since or if_none_match:
     33        status = 304
     34        start = 0
     35        end = 0
     36    else:
     37        status = 200
     38        start = 0
     39        end = total_size
     40 
     41    response.status = status
     42    headers = []
     43    if status == 200:
     44        headers.append((b"Age", b"86400"))
     45        headers.append((b"Expires", b"Wed, 21 Oct 2015 07:28:00 GMT"))
     46        headers.append((b"Last-Modified", b"Wed, 21 Oct 2015 07:28:00 GMT"))
     47        headers.append((b"ETag", ENTITY_TAG))
     48        video_file = open(path, "rb")
     49        video_file.seek(start)
     50        content = video_file.read(end)
     51    elif status == 206:
     52        headers.append((b"Content-Range", b"bytes %d-%d/%d" % (start, end, total_size)))
     53        headers.append((b"Accept-Ranges", b"bytes"))
     54        headers.append((b"ETag", ENTITY_TAG))
     55        video_file = open(path, "rb")
     56        video_file.seek(start)
     57        end = min(end+1, total_size)
     58        content = video_file.read(end-start)
     59    else:
     60        content = b""
     61    headers.append((b"Content-Length", str(end - start)))
     62    headers.append((b"Content-Type", b"audio/mp3"))
     63 
     64    return status, headers, content