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 startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
107 if c == "&" or c == "=" or c == "#" or c == "?" or c == "/" or (ord(c) <= 32):
108 ret += "%%%02X" % ord(c)
113 class urlerror(ValueError):
119 raise urlerror("Protocol not found in absolute URL `%s'" % url)
121 l = url.find("/", p + 3)
123 raise urlerror("Local part not found in absolute URL `%s'" % url)
130 query = local[q + 1:]
132 return proto, host, local, query
134 def consurl(proto, host, local, query=""):
135 if len(local) < 1 and local[0] != '/':
136 raise urlerror("Local part of URL must begin with a slash")
137 ret = "%s://%s%s" % (proto, host, local)
142 def appendurl(url, other):
145 proto, host, local, query = parseurl(url)
146 if len(other) > 0 and other[0] == '/':
147 return consurl(proto, host, other)
150 return consurl(proto, host, local[:p + 1] + other)
153 host = req.ihead.get("Host", None)
155 raise Exception("Could not reconstruct URL because no Host header was sent")
159 return "%s://%s/" % (proto, host)
163 if req.uriname[0] != '/':
164 raise Exception("Malformed local part when reconstructing URL")
165 return siteurl(req) + req.uriname[1:]
169 if req.uri[0] != '/':
170 raise Exception("Malformed local part when reconstructing URL")
171 return siteurl(req) + req.uri[1:]
173 def parstring(pars={}, **augment):
181 if buf != "": buf += "&"
182 buf += urlq(key) + "=" + urlq(str(val))
184 if buf != "": buf += "&"
185 buf += urlq(key) + "=" + urlq(str(augment[key]))
188 def parurl(url, pars={}, **augment):
189 qs = parstring(pars, **augment)
191 return url + "?" + qs