X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fproto.py;h=7cb6d311e8f612207141c1a3e3b3f7f1f59ad9a4;hb=bdc80241ffb5b50b1c9fb6500778e97b3f7170e6;hp=4cf19519bb739832d841defe522ae5a49f80ec6a;hpb=d740aa68a4d205076b2d132067ac2c31ceb3cd2e;p=wrw.git diff --git a/wrw/proto.py b/wrw/proto.py index 4cf1951..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): @@ -102,9 +102,12 @@ def simpleerror(env, startreq, code, title, msg): 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 @@ -188,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)