X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fproto.py;h=7cb6d311e8f612207141c1a3e3b3f7f1f59ad9a4;hb=bdc80241ffb5b50b1c9fb6500778e97b3f7170e6;hp=b1f0ab375e6faba741cc1dc4da86d85ba3950e0c;hpb=7be9e8bb560cd4e969992759bbcb523b40e544af;p=wrw.git diff --git a/wrw/proto.py b/wrw/proto.py index b1f0ab3..7cb6d31 100644 --- a/wrw/proto.py +++ b/wrw/proto.py @@ -1,4 +1,4 @@ -import time +import time, calendar, collections, binascii, base64 statusinfo = { 400: ("Bad Request", "Invalid HTTP request."), @@ -21,7 +21,7 @@ def phttpdate(dstr): return None tz = int(tz[1:]) tz = (((tz / 100) * 60) + (tz % 100)) * 60 - return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone + return calendar.timegm(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz def pmimehead(hstr): def pws(p): @@ -85,10 +85,29 @@ def htmlq(html): ret += c return ret +def simpleerror(env, startreq, code, title, msg): + buf = """ + + + +%s + + +

%s

+

%s

+ + +""" % (title, title, htmlq(msg)) + startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))]) + return [buf] + def urlq(url): + if isinstance(url, unicode): + url = url.encode("utf-8") ret = "" + invalid = "%;&=#?/\"'" for c in url: - if c == "&" or c == "=" or c == "#" or c == "?" or c == "/" or (ord(c) <= 32): + if c in invalid or (ord(c) <= 32) or (ord(c) >= 128): ret += "%%%02X" % ord(c) else: ret += c @@ -133,16 +152,26 @@ def appendurl(url, other): p = local.rfind('/') return consurl(proto, host, local[:p + 1] + other) -def requrl(req): +def siteurl(req): host = req.ihead.get("Host", None) if host is None: raise Exception("Could not reconstruct URL because no Host header was sent") proto = "http" if req.https: proto = "https" + return "%s://%s/" % (proto, host) + +def scripturl(req): + s = siteurl(req) + if req.uriname[0] != '/': + raise Exception("Malformed local part when reconstructing URL") + return siteurl(req) + req.uriname[1:] + +def requrl(req): + s = siteurl(req) if req.uri[0] != '/': raise Exception("Malformed local part when reconstructing URL") - return "%s://%s%s" % (proto, host, req.uri) + return siteurl(req) + req.uri[1:] def parstring(pars={}, **augment): buf = "" @@ -162,6 +191,25 @@ def parstring(pars={}, **augment): def parurl(url, pars={}, **augment): qs = parstring(pars, **augment) if qs != "": - return url + "?" + qs + return url + ("&" if "?" in url else "?") + qs else: return url + +# Wrap these, since binascii is a bit funky. :P +def enhex(bs): + return base64.b16encode(bs) +def unhex(es): + return base64.b16decode(es) +def enb32(bs): + return base64.b32encode(bs) +def unb32(es): + if (len(es) % 8) != 0: + es += b"=" * (8 - (len(es) % 8)) + es = es.upper() # The whole point of Base32 is that it's case-insensitive :P + return base64.b32decode(es) +def enb64(bs): + return base64.b64encode(bs) +def unb64(es): + if (len(es) % 4) != 0: + es += b"=" * (4 - (len(es) % 4)) + return base64.b64decode(es)