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 calendar.timegm(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz
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)))])
105 if isinstance(url, unicode):
106 url = url.encode("utf-8")
110 if c in invalid or (ord(c) <= 32) or (ord(c) >= 128):
111 ret += "%%%02X" % ord(c)
116 class urlerror(ValueError):
122 raise urlerror("Protocol not found in absolute URL `%s'" % url)
124 l = url.find("/", p + 3)
126 raise urlerror("Local part not found in absolute URL `%s'" % url)
133 query = local[q + 1:]
135 return proto, host, local, query
137 def consurl(proto, host, local, query=""):
138 if len(local) < 1 and local[0] != '/':
139 raise urlerror("Local part of URL must begin with a slash")
140 ret = "%s://%s%s" % (proto, host, local)
145 def appendurl(url, other):
148 proto, host, local, query = parseurl(url)
149 if len(other) > 0 and other[0] == '/':
150 return consurl(proto, host, other)
153 return consurl(proto, host, local[:p + 1] + other)
156 host = req.ihead.get("Host", None)
158 raise Exception("Could not reconstruct URL because no Host header was sent")
162 return "%s://%s/" % (proto, host)
166 if req.uriname[0] != '/':
167 raise Exception("Malformed local part when reconstructing URL")
168 return siteurl(req) + req.uriname[1:]
172 if req.uri[0] != '/':
173 raise Exception("Malformed local part when reconstructing URL")
174 return siteurl(req) + req.uri[1:]
176 def parstring(pars={}, **augment):
184 if buf != "": buf += "&"
185 buf += urlq(key) + "=" + urlq(str(val))
187 if buf != "": buf += "&"
188 buf += urlq(key) + "=" + urlq(str(augment[key]))
191 def parurl(url, pars={}, **augment):
192 qs = parstring(pars, **augment)
194 return url + "?" + qs