Added a distutils script.
[wrw.git] / wrw / req.py
CommitLineData
b409a338
FT
1__all__ = ["request"]
2
3class 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
39def 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
52class 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
62class origrequest(request):
b409a338
FT
63 def __init__(self, env):
64 self.env = env
65 self.uriname = env["SCRIPT_NAME"]
66 self.filename = env.get("SCRIPT_FILENAME")
67 self.uri = env["REQUEST_URI"]
68 self.pathinfo = env["PATH_INFO"]
69 self.query = env["QUERY_STRING"]
70 self.remoteaddr = env["REMOTE_ADDR"]
71 self.serverport = env["SERVER_PORT"]
72 self.https = "HTTPS" in env
73 self.ihead = headdict()
74 self.ohead = headdict()
75 for k, v in env.items():
76 if k[:5] == "HTTP_":
77 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
78 self.items = {}
79 self.statuscode = (200, "OK")
80 self.ohead["Content-Type"] = "text/html"
81 self.resources = set()
82 self.clean = set()
83 self.commitfuns = []
84
85 def status(self, code, msg):
86 self.statuscode = code, msg
87
88 def item(self, id):
89 if id in self.items:
90 return self.items[id]
91 self.items[id] = new = id(self)
92 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
93 self.withres(new)
94 return new
95
96 def withres(self, res):
97 if res not in self.resources:
98 done = False
99 res.__enter__()
100 try:
101 self.resources.add(res)
102 self.clean.add(res.__exit__)
103 done = True
104 finally:
105 if not done:
106 res.__exit__(None, None, None)
107 self.resources.discard(res)
108
109 def cleanup(self):
110 def clean1(list):
111 if len(list) > 0:
112 try:
113 list[0]()
114 finally:
115 clean1(list[1:])
116 clean1(list(self.clean))
117
118 def oncommit(self, fn):
119 if fn not in self.commitfuns:
120 self.commitfuns.append(fn)
121
122 def commit(self, startreq):
123 for fun in reversed(self.commitfuns):
124 fun(self)
125 hdrs = []
126 for nm in self.ohead:
127 for val in self.ohead.getlist(nm):
128 hdrs.append((nm, val))
129 startreq("%s %s" % self.statuscode, hdrs)
0a59819d
FT
130
131 def topreq(self):
132 return self
133
134class copyrequest(request):
135 def __init__(self, p):
136 self.parent = p
137 self.top = p.topreq()
138 self.env = p.env
139 self.uriname = p.uriname
140 self.filename = p.filename
141 self.uri = p.uri
142 self.pathinfo = p.pathinfo
143 self.query = p.query
144 self.remoteaddr = p.remoteaddr
145 self.serverport = p.serverport
146 self.https = p.https
147 self.ihead = p.ihead
148 self.ohead = p.ohead
149
150 def status(self, code, msg):
151 return self.parent.status(code, msg)
152
153 def item(self, id):
154 return self.top.item(id)
155
156 def withres(self, res):
157 return self.top.withres(res)
158
159 def oncommit(self, fn):
160 return self.top.oncommit(fn)
161
162 def topreq(self):
163 return self.parent.topreq()