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()
144 if "CONTENT_TYPE" in env:
145 self.ihead["Content-Type"] = env["CONTENT_TYPE"]
146 if "CONTENT_LENGTH" in env:
147 clen = self.ihead["Content-Length"] = env["CONTENT_LENGTH"]
149 self.input = limitreader(env["wsgi.input"], int(clen))
150 if self.input is None:
151 self.input = io.BytesIO("")
152 self.ohead = headdict()
153 for k, v in env.items():
155 self.ihead.add(fixcase(k[5:].replace("_", "-")), v)
157 self.statuscode = (200, "OK")
158 self.ohead["Content-Type"] = "text/html"
159 self.resources = set()
163 def status(self, code, msg):
164 self.statuscode = code, msg
168 return self.items[id]
169 self.items[id] = new = id(self)
170 if hasattr(new, "__enter__") and hasattr(new, "__exit__"):
174 def withres(self, res):
175 if res not in self.resources:
179 self.resources.add(res)
180 self.clean.add(res.__exit__)
184 res.__exit__(None, None, None)
185 self.resources.discard(res)
194 clean1(list(self.clean))
196 def oncommit(self, fn):
197 if fn not in self.commitfuns:
198 self.commitfuns.append(fn)
200 def commit(self, startreq):
201 for fun in reversed(self.commitfuns):
204 for nm in self.ohead:
205 for val in self.ohead.getlist(nm):
206 hdrs.append((nm, val))
207 startreq("%s %s" % self.statuscode, hdrs)
212 class copyrequest(request):
213 def __init__(self, p):
215 self.top = p.topreq()
217 self.method = p.method
218 self.uriname = p.uriname
219 self.filename = p.filename
221 self.pathinfo = p.pathinfo
223 self.remoteaddr = p.remoteaddr
224 self.serverport = p.serverport
229 def status(self, code, msg):
230 return self.parent.status(code, msg)
233 return self.top.item(id)
235 def withres(self, res):
236 return self.top.withres(res)
238 def oncommit(self, fn):
239 return self.top.oncommit(fn)
242 return self.parent.topreq()