5 class headdict(object):
9 def __getitem__(self, key):
10 return self.dict[key.lower()][1]
12 def __setitem__(self, key, val):
13 self.dict[key.lower()] = [key, val]
15 def __contains__(self, key):
16 return key.lower() in self.dict
18 def __delitem__(self, key):
19 del self.dict[key.lower()]
22 return iter((list[0] for list in self.dict.itervalues()))
24 def get(self, key, default=""):
25 if key.lower() in self.dict:
26 return self.dict[key.lower()][1]
29 def getlist(self, key):
30 return self.dict.setdefault(key.lower(), [key])[1:]
32 def add(self, key, val):
33 self.dict.setdefault(key.lower(), [key]).append(val)
36 return repr(self.dict)
47 str = str[:i] + str[i].upper() + str[i + 1:]
54 class limitreader(object):
55 def __init__(self, back, limit):
59 self.buf = bytearray()
64 def read(self, size=-1):
65 ra = self.limit - self.rb
68 while len(self.buf) < ra:
69 ret = self.bk.read(ra - len(self.buf))
71 raise IOError("Unexpected EOF")
74 ret = str(self.buf[:ra])
75 self.buf = self.buf[ra:]
78 def readline(self, size=-1):
81 p = self.buf.find('\n', off)
83 ret = str(self.buf[:p + 1])
84 self.buf = self.buf[p + 1:]
87 if size >= 0 and len(self.buf) >= size:
88 ret = str(self.buf[:size])
89 self.buf = self.buf[size:]
91 if self.rb == self.limit:
93 self.buf = bytearray()
95 ra = self.limit - self.rb
99 ret = self.bk.read(ra)
101 raise IOError("Unpexpected EOF")
105 def readlines(self, hint=None):
109 class lineiter(object):
115 raise StopIteration()
119 class request(object):
121 return copyrequest(self)
125 new.uriname = self.uriname + self.pathinfo[:n]
126 new.pathinfo = self.pathinfo[n:]
129 class origrequest(request):
130 def __init__(self, env):
132 self.method = env["REQUEST_METHOD"].upper()
133 self.uriname = env["SCRIPT_NAME"]
134 self.filename = env.get("SCRIPT_FILENAME")
135 self.uri = env["REQUEST_URI"]
136 self.pathinfo = env["PATH_INFO"]
137 self.query = env["QUERY_STRING"]
138 self.remoteaddr = env["REMOTE_ADDR"]
139 self.serverport = env["SERVER_PORT"]
140 self.servername = env["SERVER_NAME"]
141 self.https = "HTTPS" in env
142 self.ihead = headdict()
143 if "CONTENT_TYPE" in env:
144 self.ihead["Content-Type"] = env["CONTENT_TYPE"]
145 if "CONTENT_LENGTH" in env:
146 clen = self.ihead["Content-Length"] = env["CONTENT_LENGTH"]
148 self.input = limitreader(env["wsgi.input"], int(clen))
151 self.input = io.BytesIO("")
153 # Assume input is chunked and read until ordinary EOF.
154 self.input = env["wsgi.input"]
157 self.ohead = headdict()
158 for k, v in env.items():
160 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
162 self.statuscode = (200, "OK")
163 self.ohead["Content-Type"] = "text/html"
164 self.resources = set()
168 def status(self, code, msg):
169 self.statuscode = code, msg
173 return self.items[id]
174 self.items[id] = new = id(self)
175 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
179 def withres(self, res):
180 if res not in self.resources:
184 self.resources.add(res)
185 self.clean.add(res.__exit__)
189 res.__exit__(None, None, None)
190 self.resources.discard(res)
199 clean1(list(self.clean))
201 def oncommit(self, fn):
202 if fn not in self.commitfuns:
203 self.commitfuns.append(fn)
205 def commit(self, startreq):
206 for fun in reversed(self.commitfuns):
209 for nm in self.ohead:
210 for val in self.ohead.getlist(nm):
211 hdrs.append((nm, val))
212 startreq("%s %s" % self.statuscode, hdrs)
217 class copyrequest(request):
218 def __init__(self, p):
220 self.top = p.topreq()
222 self.method = p.method
223 self.uriname = p.uriname
224 self.filename = p.filename
226 self.pathinfo = p.pathinfo
228 self.remoteaddr = p.remoteaddr
229 self.serverport = p.serverport
234 def status(self, code, msg):
235 return self.parent.status(code, msg)
238 return self.top.item(id)
240 def withres(self, res):
241 return self.top.withres(res)
243 def oncommit(self, fn):
244 return self.top.oncommit(fn)
247 return self.parent.topreq()