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.servername = env["SERVER_NAME"]
74 self.https = "HTTPS" in env
75 self.ihead = headdict()
76 self.ohead = headdict()
77 for k, v in env.items():
79 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
81 self.statuscode = (200, "OK")
82 self.ohead["Content-Type"] = "text/html"
83 self.resources = set()
87 def status(self, code, msg):
88 self.statuscode = code, msg
93 self.items[id] = new = id(self)
94 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
98 def withres(self, res):
99 if res not in self.resources:
103 self.resources.add(res)
104 self.clean.add(res.__exit__)
108 res.__exit__(None, None, None)
109 self.resources.discard(res)
118 clean1(list(self.clean))
120 def oncommit(self, fn):
121 if fn not in self.commitfuns:
122 self.commitfuns.append(fn)
124 def commit(self, startreq):
125 for fun in reversed(self.commitfuns):
128 for nm in self.ohead:
129 for val in self.ohead.getlist(nm):
130 hdrs.append((nm, val))
131 startreq("%s %s" % self.statuscode, hdrs)
136 class copyrequest(request):
137 def __init__(self, p):
139 self.top = p.topreq()
141 self.method = p.method
142 self.uriname = p.uriname
143 self.filename = p.filename
145 self.pathinfo = p.pathinfo
147 self.remoteaddr = p.remoteaddr
148 self.serverport = p.serverport
153 def status(self, code, msg):
154 return self.parent.status(code, msg)
157 return self.top.item(id)
159 def withres(self, res):
160 return self.top.withres(res)
162 def oncommit(self, fn):
163 return self.top.oncommit(fn)
166 return self.parent.topreq()