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.method = env["REQUEST_METHOD"].upper()
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():
78 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
80 self.statuscode = (200, "OK")
81 self.ohead["Content-Type"] = "text/html"
82 self.resources = set()
86 def status(self, code, msg):
87 self.statuscode = code, msg
92 self.items[id] = new = id(self)
93 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
97 def withres(self, res):
98 if res not in self.resources:
102 self.resources.add(res)
103 self.clean.add(res.__exit__)
107 res.__exit__(None, None, None)
108 self.resources.discard(res)
117 clean1(list(self.clean))
119 def oncommit(self, fn):
120 if fn not in self.commitfuns:
121 self.commitfuns.append(fn)
123 def commit(self, startreq):
124 for fun in reversed(self.commitfuns):
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)
135 class copyrequest(request):
136 def __init__(self, p):
138 self.top = p.topreq()
140 self.method = p.method
141 self.uriname = p.uriname
142 self.filename = p.filename
144 self.pathinfo = p.pathinfo
146 self.remoteaddr = p.remoteaddr
147 self.serverport = p.serverport
152 def status(self, code, msg):
153 return self.parent.status(code, msg)
156 return self.top.item(id)
158 def withres(self, res):
159 return self.top.withres(res)
161 def oncommit(self, fn):
162 return self.top.oncommit(fn)
165 return self.parent.topreq()