Commit | Line | Data |
---|---|---|
b409a338 FT |
1 | __all__ = ["request"] |
2 | ||
3 | class headdict(object): | |
4 | def __init__(self): | |
5 | self.dict = {} | |
6 | ||
7 | def __getitem__(self, key): | |
8 | return self.dict[key.lower()][1] | |
9 | ||
10 | def __setitem__(self, key, val): | |
11 | self.dict[key.lower()] = [key, val] | |
12 | ||
13 | def __contains__(self, key): | |
14 | return key.lower() in self.dict | |
15 | ||
16 | def __delitem__(self, key): | |
17 | del self.dict[key.lower()] | |
18 | ||
19 | def __iter__(self): | |
20 | return iter((list[0] for list in self.dict.itervalues())) | |
21 | ||
22 | def get(self, key, default = ""): | |
23 | if key.lower() in self.dict: | |
24 | return self.dict[key.lower()][1] | |
25 | return default | |
26 | ||
27 | def getlist(self, key): | |
28 | return self.dict.setdefault(key.lower(), [key])[1:] | |
29 | ||
30 | def add(self, key, val): | |
31 | self.dict.setdefault(key.lower(), [key]).append(val) | |
32 | ||
33 | def __repr__(self): | |
34 | return repr(self.dict) | |
35 | ||
36 | def __str__(self): | |
37 | return str(self.dict) | |
38 | ||
39 | def fixcase(str): | |
40 | str = str.lower() | |
41 | i = 0 | |
42 | b = True | |
43 | while i < len(str): | |
44 | if b: | |
45 | str = str[:i] + str[i].upper() + str[i + 1:] | |
46 | b = False | |
47 | if str[i] == '-': | |
48 | b = True | |
49 | i += 1 | |
50 | return str | |
51 | ||
52 | class request(object): | |
0a59819d FT |
53 | def copy(self): |
54 | return copyrequest(self) | |
55 | ||
56 | def shift(self, n): | |
57 | new = self.copy() | |
58 | new.uriname = self.uriname + self.pathinfo[:n] | |
59 | new.pathinfo = self.pathinfo[n:] | |
60 | return new | |
61 | ||
62 | class origrequest(request): | |
b409a338 FT |
63 | def __init__(self, env): |
64 | self.env = env | |
40131e7c | 65 | self.method = env["REQUEST_METHOD"].upper() |
b409a338 FT |
66 | self.uriname = env["SCRIPT_NAME"] |
67 | self.filename = env.get("SCRIPT_FILENAME") | |
68 | self.uri = env["REQUEST_URI"] | |
69 | self.pathinfo = env["PATH_INFO"] | |
70 | self.query = env["QUERY_STRING"] | |
71 | self.remoteaddr = env["REMOTE_ADDR"] | |
72 | self.serverport = env["SERVER_PORT"] | |
73 | self.https = "HTTPS" in env | |
74 | self.ihead = headdict() | |
75 | self.ohead = headdict() | |
76 | for k, v in env.items(): | |
77 | if k[:5] == "HTTP_": | |
78 | self.ihead.add(fixcase(k[5:].replace("_", "-")), v) | |
79 | self.items = {} | |
80 | self.statuscode = (200, "OK") | |
81 | self.ohead["Content-Type"] = "text/html" | |
82 | self.resources = set() | |
83 | self.clean = set() | |
84 | self.commitfuns = [] | |
85 | ||
86 | def status(self, code, msg): | |
87 | self.statuscode = code, msg | |
88 | ||
89 | def item(self, id): | |
90 | if id in self.items: | |
91 | return self.items[id] | |
92 | self.items[id] = new = id(self) | |
93 | if hasattr(new, "__enter__") and hasattr(new, "__exit__"): | |
94 | self.withres(new) | |
95 | return new | |
96 | ||
97 | def withres(self, res): | |
98 | if res not in self.resources: | |
99 | done = False | |
100 | res.__enter__() | |
101 | try: | |
102 | self.resources.add(res) | |
103 | self.clean.add(res.__exit__) | |
104 | done = True | |
105 | finally: | |
106 | if not done: | |
107 | res.__exit__(None, None, None) | |
108 | self.resources.discard(res) | |
109 | ||
110 | def cleanup(self): | |
111 | def clean1(list): | |
112 | if len(list) > 0: | |
113 | try: | |
114 | list[0]() | |
115 | finally: | |
116 | clean1(list[1:]) | |
117 | clean1(list(self.clean)) | |
118 | ||
119 | def oncommit(self, fn): | |
120 | if fn not in self.commitfuns: | |
121 | self.commitfuns.append(fn) | |
122 | ||
123 | def commit(self, startreq): | |
124 | for fun in reversed(self.commitfuns): | |
125 | fun(self) | |
126 | hdrs = [] | |
127 | for nm in self.ohead: | |
128 | for val in self.ohead.getlist(nm): | |
129 | hdrs.append((nm, val)) | |
130 | startreq("%s %s" % self.statuscode, hdrs) | |
0a59819d FT |
131 | |
132 | def topreq(self): | |
133 | return self | |
134 | ||
135 | class copyrequest(request): | |
136 | def __init__(self, p): | |
137 | self.parent = p | |
138 | self.top = p.topreq() | |
139 | self.env = p.env | |
6a6c9d8f | 140 | self.method = p.method |
0a59819d FT |
141 | self.uriname = p.uriname |
142 | self.filename = p.filename | |
143 | self.uri = p.uri | |
144 | self.pathinfo = p.pathinfo | |
145 | self.query = p.query | |
146 | self.remoteaddr = p.remoteaddr | |
147 | self.serverport = p.serverport | |
148 | self.https = p.https | |
149 | self.ihead = p.ihead | |
150 | self.ohead = p.ohead | |
151 | ||
152 | def status(self, code, msg): | |
153 | return self.parent.status(code, msg) | |
154 | ||
155 | def item(self, id): | |
156 | return self.top.item(id) | |
157 | ||
158 | def withres(self, res): | |
159 | return self.top.withres(res) | |
160 | ||
161 | def oncommit(self, fn): | |
162 | return self.top.oncommit(fn) | |
163 | ||
164 | def topreq(self): | |
165 | return self.parent.topreq() |