3 class headdict(object):
7 def __getitem__(self, key):
8 return self.dict[key.lower()][1]
10 def __setitem__(self, key, val):
11 self.dict[key.lower()] = [key, val]
13 def __contains__(self, key):
14 return key.lower() in self.dict
16 def __delitem__(self, key):
17 del self.dict[key.lower()]
20 return iter((list[0] for list in self.dict.itervalues()))
22 def get(self, key, default = ""):
23 if key.lower() in self.dict:
24 return self.dict[key.lower()][1]
27 def getlist(self, key):
28 return self.dict.setdefault(key.lower(), [key])[1:]
30 def add(self, key, val):
31 self.dict.setdefault(key.lower(), [key]).append(val)
34 return repr(self.dict)
45 str = str[:i] + str[i].upper() + str[i + 1:]
52 class request(object):
54 return copyrequest(self)
58 new.uriname = self.uriname + self.pathinfo[:n]
59 new.pathinfo = self.pathinfo[n:]
62 class origrequest(request):
63 def __init__(self, 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():
77 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
79 self.statuscode = (200, "OK")
80 self.ohead["Content-Type"] = "text/html"
81 self.resources = set()
85 def status(self, code, msg):
86 self.statuscode = code, msg
91 self.items[id] = new = id(self)
92 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
96 def withres(self, res):
97 if res not in self.resources:
101 self.resources.add(res)
102 self.clean.add(res.__exit__)
106 res.__exit__(None, None, None)
107 self.resources.discard(res)
116 clean1(list(self.clean))
118 def oncommit(self, fn):
119 if fn not in self.commitfuns:
120 self.commitfuns.append(fn)
122 def commit(self, startreq):
123 for fun in reversed(self.commitfuns):
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)
134 class copyrequest(request):
135 def __init__(self, p):
137 self.top = p.topreq()
139 self.uriname = p.uriname
140 self.filename = p.filename
142 self.pathinfo = p.pathinfo
144 self.remoteaddr = p.remoteaddr
145 self.serverport = p.serverport
150 def status(self, code, msg):
151 return self.parent.status(code, msg)
154 return self.top.item(id)
156 def withres(self, res):
157 return self.top.withres(res)
159 def oncommit(self, fn):
160 return self.top.oncommit(fn)
163 return self.parent.topreq()