File: video-with-aged-proxy-cache.py

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (64 lines) | stat: -rw-r--r-- 2,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import os
import json
from wptserve.utils import isomorphic_decode

ENTITY_TAG = b'"sine440-v1"'
# There should be two requests made to this resource:
# 1. A request fetch the whole video. This request should receive a
#    200 Success response with a aged header of value more
#    than 24 hours in the past, simulating an aged proxy cache.
# 2.a A subsequent request with a Range header to revalidate the cached content.
#    This request should include an If-Modified-Since / If-Not-Match header
#    and should response with a 304 Not Modified response.
# 2.b A subsequent request with a Range header to fetch a range of the video.
#    But without Http-Cache revalidation related header.
#    This request should receive a 206 Partial Content response with the
#    requested range of the video.

def main(request, response):
    path = os.path.join(request.doc_root, u"media", "sine440.mp3")
    total_size = os.path.getsize(path)
    if_modified_since = request.headers.get(b'If-Modified-Since')
    if_none_match = request.headers.get(b'If-Not-Match')
    range_header = request.headers.get(b'Range')
    range_header_match = range_header and re.search(r'^bytes=(\d*)-(\d*)$', isomorphic_decode(range_header))

    if range_header_match:
        start, end = range_header_match.groups()
        start = int(start or 0)
        end = int(end or total_size)
        status = 206
    elif if_modified_since or if_none_match:
        status = 304
        start = 0
        end = 0
    else:
        status = 200
        start = 0
        end = total_size

    response.status = status
    headers = []
    if status == 200:
        headers.append((b"Age", b"86400"))
        headers.append((b"Expires", b"Wed, 21 Oct 2015 07:28:00 GMT"))
        headers.append((b"Last-Modified", b"Wed, 21 Oct 2015 07:28:00 GMT"))
        headers.append((b"ETag", ENTITY_TAG))
        video_file = open(path, "rb")
        video_file.seek(start)
        content = video_file.read(end)
    elif status == 206:
        headers.append((b"Content-Range", b"bytes %d-%d/%d" % (start, end, total_size)))
        headers.append((b"Accept-Ranges", b"bytes"))
        headers.append((b"ETag", ENTITY_TAG))
        video_file = open(path, "rb")
        video_file.seek(start)
        end = min(end+1, total_size)
        content = video_file.read(end-start)
    else:
        content = b""
    headers.append((b"Content-Length", str(end - start)))
    headers.append((b"Content-Type", b"audio/mp3"))

    return status, headers, content