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)))])
108 if c == "&" or c == "=" or c == "#" or c == "?" or c == "/" or (ord(c) <= 32):
109 ret += "%%%02X" % ord(c)
114 class urlerror(ValueError):
120 raise urlerror("Protocol not found in absolute URL `%s'" % url)
122 l = url.find("/", p + 3)
124 raise urlerror("Local part not found in absolute URL `%s'" % url)
131 query = local[q + 1:]
133 return proto, host, local, query
135 def consurl(proto, host, local, query=""):
136 if len(local) < 1 and local[0] != '/':
137 raise urlerror("Local part of URL must begin with a slash")
138 ret = "%s://%s%s" % (proto, host, local)
143 def appendurl(url, other):
146 proto, host, local, query = parseurl(url)
147 if len(other) > 0 and other[0] == '/':
148 return consurl(proto, host, other)
151 return consurl(proto, host, local[:p + 1] + other)
154 host = req.ihead.get("Host", None)
156 raise Exception("Could not reconstruct URL because no Host header was sent")
160 return "%s://%s/" % (proto, host)
164 if req.uriname[0] != '/':
165 raise Exception("Malformed local part when reconstructing URL")
166 return siteurl(req) + req.uriname[1:]
170 if req.uri[0] != '/':
171 raise Exception("Malformed local part when reconstructing URL")
172 return siteurl(req) + req.uri[1:]
174 def parstring(pars={}, **augment):
182 if buf != "": buf += "&"
183 buf += urlq(key) + "=" + urlq(str(val))
185 if buf != "": buf += "&"
186 buf += urlq(key) + "=" + urlq(str(augment[key]))
189 def parurl(url, pars={}, **augment):
190 qs = parstring(pars, **augment)
192 return url + "?" + qs