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 500: ("Server Error", "An internal error occurred."),
10 501: ("Not Implemented", "The requested functionality has not been implemented."),
11 503: ("Service Unavailable", "Service is being denied at this time."),
15 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts))
20 if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit():
23 tz = (((tz / 100) * 60) + (tz % 100)) * 60
24 return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone
28 while p < len(hstr) and hstr[p].isspace():
59 return buf.strip(), pws(p)
61 val, p = token(p, ";")
68 if k == "" or hstr[p:p + 1] != '=':
88 def simpleerror(env, startreq, code, title, msg):
89 buf = """<?xml version="1.0" encoding="US-ASCII"?>
90 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
91 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
100 """ % (title, title, htmlq(msg))
101 buf = buf.encode("us-ascii")
102 startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
109 if c in invalid or (ord(c) <= 32):
110 ret += "%%%02X" % ord(c)
115 class urlerror(ValueError):
121 raise urlerror("Protocol not found in absolute URL `%s'" % url)
123 l = url.find("/", p + 3)
125 raise urlerror("Local part not found in absolute URL `%s'" % url)
132 query = local[q + 1:]
134 return proto, host, local, query
136 def consurl(proto, host, local, query=""):
137 if len(local) < 1 and local[0] != '/':
138 raise urlerror("Local part of URL must begin with a slash")
139 ret = "%s://%s%s" % (proto, host, local)
144 def appendurl(url, other):
147 proto, host, local, query = parseurl(url)
148 if len(other) > 0 and other[0] == '/':
149 return consurl(proto, host, other)
152 return consurl(proto, host, local[:p + 1] + other)
155 host = req.ihead.get("Host", None)
157 raise Exception("Could not reconstruct URL because no Host header was sent")
161 return "%s://%s/" % (proto, host)
165 if req.uriname[0] != '/':
166 raise Exception("Malformed local part when reconstructing URL")
167 return siteurl(req) + req.uriname[1:]
171 if req.uri[0] != '/':
172 raise Exception("Malformed local part when reconstructing URL")
173 return siteurl(req) + req.uri[1:]
175 def parstring(pars={}, **augment):
183 if buf != "": buf += "&"
184 buf += urlq(key) + "=" + urlq(str(val))
186 if buf != "": buf += "&"
187 buf += urlq(key) + "=" + urlq(str(augment[key]))
190 def parurl(url, pars={}, **augment):
191 qs = parstring(pars, **augment)
193 return url + "?" + qs