1 import time, calendar, collections.abc, binascii, base64
4 400: ("Bad Request", "Invalid HTTP request."),
5 401: ("Unauthorized", "Authentication must be provided for the requested resource."),
6 403: ("Forbidden", "You are not authorized to request the requested resource."),
7 404: ("Not Found", "The requested resource was not found."),
8 405: ("Method Not Allowed", "The request method is not recognized or permitted by the requested resource."),
9 406: ("Not Acceptable", "No way was found to satisfy the given content-negotiation criteria."),
10 407: ("Proxy Authentication Required", "Authentication must be provided to proxy the request."),
11 408: ("Request Timeout", "The connection timed out."),
12 409: ("Conflict", "The request conflicts with the live state."),
13 410: ("Gone", "The requested resource has been deleted."),
14 411: ("Length Required", "The requested resource requires the Content-Length header."),
15 412: ("Precondition Failed", "The preconditions specified in the request are not met."),
16 413: ("Payload Too Large", "The request entity is larger than permitted."),
17 414: ("URI Too Long", "The requested URI is too long."),
18 415: ("Unsupported Media Type", "The request entity format is not supported."),
19 416: ("Range Not Satisfiable", "The specified Range cannot be satisfied."),
20 417: ("Expectation Failed", "The expectation specified by the Expect header cannot be met."),
21 421: ("Misdirected Request", "This server cannot handle the request."),
22 422: ("Unprocessable Content", "The requet entity cannot be processed."),
23 423: ("Locked", "The requested resource is locked."),
24 424: ("Failed Dependency", "A previous required request failed."),
25 425: ("TOo Early", "The requested action has already been performed."),
26 426: ("Upgrade Required", "The requested resource is not available over this protocol."),
27 428: ("Precondition Requred", "The requested resource needs to be conditionally requested."),
28 429: ("Too Many Requests", "Your client is sending more frequent requests than are accepted."),
29 431: ("Request Header Fields Too Large", "The request headers are too large."),
30 451: ("Unavilable For Legal Reasons", "The requested resource has been censored."),
31 500: ("Server Error", "An internal error occurred."),
32 501: ("Not Implemented", "The requested functionality has not been implemented."),
33 502: ("Bad Gateway", "The backing server indicated an error."),
34 503: ("Service Unavailable", "Service is being denied at this time."),
35 504: ("Gateway Timeout", "The backing server is not responding."),
36 505: ("Unsupported HTTP Version", "The server does not support the requested HTTP version."),
37 506: ("Variant Also Negotiates", "The server content-negotiation is misconfigured."),
38 507: ("Insufficient Storage", "The server is out of storage to process the request."),
39 508: ("Loop Detected", "An infinite loop was detected while processing the request."),
40 510: ("Not Extended", "The requested extension is not supported."),
41 511: ("Network Authentication Required", "Authentication for network access is required."),
45 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts))
50 if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit():
53 tz = (((tz / 100) * 60) + (tz % 100)) * 60
54 return calendar.timegm(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz
58 while p < len(hstr) and hstr[p].isspace():
89 return buf.strip(), pws(p)
91 val, p = token(p, ";")
98 if k == "" or hstr[p:p + 1] != '=':
118 def simpleerror(env, startreq, code, title, msg):
119 buf = """<?xml version="1.0" encoding="US-ASCII"?>
120 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
121 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
130 """ % (title, title, htmlq(msg))
131 buf = buf.encode("us-ascii")
132 startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
136 if isinstance(url, str):
137 url = url.encode("utf-8")
139 invalid = b"%;&=+#?/\"'"
141 if c in invalid or (c <= 32) or (c >= 128):
147 class urlerror(ValueError):
153 raise urlerror("Protocol not found in absolute URL `%s'" % url)
155 l = url.find("/", p + 3)
157 raise urlerror("Local part not found in absolute URL `%s'" % url)
164 query = local[q + 1:]
166 return proto, host, local, query
168 def consurl(proto, host, local, query=""):
169 if len(local) < 1 and local[0] != '/':
170 raise urlerror("Local part of URL must begin with a slash")
171 ret = "%s://%s%s" % (proto, host, local)
176 def appendurl(url, other):
179 proto, host, local, query = parseurl(url)
180 if len(other) > 0 and other[0] == '/':
181 return consurl(proto, host, other)
184 return consurl(proto, host, local[:p + 1] + other)
187 host = req.ihead.get("Host", None)
189 raise Exception("Could not reconstruct URL because no Host header was sent")
193 return "%s://%s/" % (proto, host)
197 if req.uriname[0] != '/':
198 raise Exception("Malformed local part when reconstructing URL")
199 return siteurl(req) + req.uriname[1:]
201 def requrl(req, qs=True):
203 if req.uri[0] != '/':
204 raise Exception("Malformed local part when reconstructing URL")
210 return siteurl(req) + pf
212 def parstring(pars={}, **augment):
222 if buf != "": buf += "&"
223 buf += urlq(key) + "=" + urlq(str(val))
224 for key, val in augment.items():
227 if buf != "": buf += "&"
228 buf += urlq(key) + "=" + urlq(str(val))
231 def parurl(url, pars={}, **augment):
232 qs = parstring(pars, **augment)
234 return url + ("&" if "?" in url else "?") + qs
238 # Wrap these, since binascii is a bit funky. :P
240 return base64.b16encode(bs).decode("us-ascii")
242 if not isinstance(es, collections.abc.ByteString):
244 es = es.encode("us-ascii")
246 raise binascii.Error("non-ascii character in hex-string")
247 return base64.b16decode(es)
249 return base64.b32encode(bs).decode("us-ascii")
251 if not isinstance(es, collections.abc.ByteString):
253 es = es.encode("us-ascii")
255 raise binascii.Error("non-ascii character in base32-string")
256 if (len(es) % 8) != 0:
257 es += b"=" * (8 - (len(es) % 8))
258 es = es.upper() # The whole point of Base32 is that it's case-insensitive :P
259 return base64.b32decode(es)
261 return base64.b64encode(bs).decode("us-ascii")
263 if not isinstance(es, collections.abc.ByteString):
265 es = es.encode("us-ascii")
267 raise binascii.Error("non-ascii character in base64-string")
268 if (len(es) % 4) != 0:
269 es += b"=" * (4 - (len(es) % 4))
270 return base64.b64decode(es)
276 for c in range(ord('0'), ord('9') + 1):
278 for c in range(ord('A'), ord('Z') + 1):
280 for c in range(ord('a'), ord('z') + 1):
283 _quoprisafe = _quoprisafe()
284 def quopri(s, charset="utf-8"):
285 bv = s.encode(charset)
286 qn = sum(not _quoprisafe[b] for b in bv)
290 return "=?%s?B?%s?=" % (charset, enb64(bv))
292 return "=?%s?Q?%s?=" % (charset, "".join(chr(b) if _quoprisafe[b] else "=%02X" % b for b in bv))
294 class mimeparam(object):
295 def __init__(self, name, val, fallback=None, charset="utf-8", lang=""):
298 self.fallback = fallback
299 self.charset = charset
303 self.name.encode("ascii")
305 self.val.encode("ascii")
309 return "%s=%s" % (self.name, self.val)
310 val = self.val.encode(self.charset)
311 self.charset.encode("ascii")
312 self.lang.encode("ascii")
314 if self.fallback is not None:
315 self.fallback.encode("ascii")
316 ret += "%s=%s; " % (self.name, self.fallback)
317 ret += "%s*=%s'%s'%s" % (self.name, self.charset, self.lang, urlq(val))
320 class mimeheader(object):
321 def __init__(self, name, val, *, mime_charset="utf-8", mime_lang="", **params):
325 self.charset = mime_charset
326 self.lang = mime_lang
327 for k, v in params.items():
330 def __getitem__(self, nm):
331 return self.params[nm.lower()]
333 def __setitem__(self, nm, val):
334 if not isinstance(val, mimeparam):
335 val = mimeparam(nm, val, charset=self.charset, lang=self.lang)
336 self.params[nm.lower()] = val
338 def __delitem__(self, nm):
339 del self.params[nm.lower()]
344 parts.append(quopri(self.val))
345 parts.extend(str(x) for x in self.params.values())
346 return("; ".join(parts))
349 if self.name is None:
351 return "%s: %s" % (self.name, self.value())