Project

General

Profile

blah.py

peto, 2009-07-21 21:46

 
1
import urllib
2

    
3
def parse(value):
4
        print "X-Sendfile2:", value
5
        params = value.split(" ")
6
        if len(params) < 1 or not params[0]: raise ValueError, "insufficient parameters"
7

    
8
        filename = urllib.unquote(params[0])
9

    
10
        # Parse the range; None means "until EOF".
11
        if len(params) >= 2:
12
                range_params = params[1].split("-", 2)
13
                if len(range_params) != 2:
14
                        raise ValueError, "invalid range: %s" % params[1]
15
                range = [int(range_params[0]), None]
16
                if range_params[1] != "":
17
                        range[1] = int(range_params[1])
18
        else:
19
                range = [0, None]
20

    
21
        flags = set(params[2:])
22
        if "temp" in flags:
23
                flags.remove("temp")
24
                tempfile = True
25
        else:
26
                tempfile = False
27

    
28
        print filename
29
        print "Range:", range
30
        if tempfile:
31
                print "tempfile"
32
        if flags:
33
                print "Warning: unsupported parameters: %s" % ",".join([s for s in flags])
34
        print
35

    
36
parse("/file%20to%20send%2Cwith%2Ccommas")
37
parse("/filename 0-")
38
parse("/filename 0- foo")
39
parse("/filename 100-200")
40
parse("/filename 100-200 temp")
41
parse("/filename 100-200 foo temp")
42

    
43
errors = [
44
        "",
45
        "/filename   foo",
46
        "/filename 0 foo",
47
        "/filename 0-- foo",
48
        "/filename 0-5-6 foo",
49
]
50
for s in errors:
51
        try:
52
                parse(s)
53
                raise RuntimeError, "expected exception didn't happen: %s" % s
54
        except ValueError, e:
55
                print e, "(expected)"
56
                pass
57