Merge branch 'master' into python2
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 8 Nov 2015 03:01:08 +0000 (04:01 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 8 Nov 2015 03:01:19 +0000 (04:01 +0100)
Conflicts:
wrw/auth.py
wrw/form.py
wrw/proto.py
wrw/session.py

1  2 
wrw/auth.py
wrw/form.py
wrw/proto.py
wrw/session.py
wrw/util.py

diff --cc wrw/auth.py
@@@ -1,5 -1,5 +1,5 @@@
  import binascii, hashlib, threading, time
- import resp
 -from . import resp, proto
++import resp, proto
  
  class unauthorized(resp.httperror):
      def __init__(self, challenge, message=None, detail=None):
@@@ -31,9 -31,13 +31,9 @@@ def parsebasic(req)
      if mech != "basic":
          return None, None
      try:
-         raw = binascii.a2b_base64(data)
+         raw = proto.unb64(data)
      except binascii.Error:
          return None, None
 -    try:
 -        raw = raw.decode("utf-8")
 -    except UnicodeError:
 -        raw = raw.decode("latin1")
      p = raw.find(":")
      if p < 0:
          return None, None
diff --cc wrw/form.py
@@@ -5,12 -5,15 +5,15 @@@ __all__ = ["formdata"
  
  def formparse(req):
      buf = {}
 -    buf.update(urllib.parse.parse_qsl(req.query))
 +    buf.update(urlparse.parse_qsl(req.query))
      if req.ihead.get("Content-Type") == "application/x-www-form-urlencoded":
-         rbody = req.input.read(2 ** 20)
+         try:
+             rbody = req.input.read(2 ** 20)
+         except IOError as exc:
+             return exc
          if len(rbody) >= 2 ** 20:
-             raise ValueError("x-www-form-urlencoded data is absurdly long")
+             return ValueError("x-www-form-urlencoded data is absurdly long")
 -        buf.update(urllib.parse.parse_qsl(rbody.decode("latin1")))
 +        buf.update(urlparse.parse_qsl(rbody))
      return buf
  
  class badmultipart(Exception):
@@@ -81,7 -84,9 +84,9 @@@ class formpart(object)
                  return ret
  
      def close(self):
-         self.fillbuf(-1)
+         while True:
 -            if self.read(8192) == b"":
++            if self.read(8192) == "":
+                 break
  
      def __enter__(self):
          return self
@@@ -149,8 -161,13 +154,13 @@@ class multipart(object)
          if self.eof:
              raise StopIteration()
          self.lastpart = formpart(self)
 -        self.lastpart.parsehead(self.headcs)
 +        self.lastpart.parsehead()
          return self.lastpart
  
- def formdata(req):
-     return req.item(formparse)
+ def formdata(req, onerror=Exception):
+     data = req.item(formparse)
+     if isinstance(data, Exception):
+         if onerror is Exception:
+             raise data
+         return onerror
+     return data
diff --cc wrw/proto.py
@@@ -102,15 -103,15 +102,15 @@@ def simpleerror(env, startreq, code, ti
      return [buf]
  
  def urlq(url):
 -    if isinstance(url, str):
 +    if isinstance(url, unicode):
          url = url.encode("utf-8")
      ret = ""
-     invalid = "&=#?/\"'"
 -    invalid = b";&=#?/\"'"
++    invalid = ";&=#?/\"'"
      for c in url:
 -        if c in invalid or (c <= 32) or (c >= 128):
 -            ret += "%%%02X" % c
 +        if c in invalid or (ord(c) <= 32) or (ord(c) >= 128):
 +            ret += "%%%02X" % ord(c)
          else:
 -            ret += chr(c)
 +            ret += c
      return ret
  
  class urlerror(ValueError):
@@@ -194,3 -195,37 +194,22 @@@ def parurl(url, pars={}, **augment)
          return url + "?" + qs
      else:
          return url
 -    return base64.b16encode(bs).decode("us-ascii")
+ # Wrap these, since binascii is a bit funky. :P
+ def enhex(bs):
 -    if not isinstance(es, collections.ByteString):
 -        try:
 -            es = es.encode("us-ascii")
 -        except UnicodeError:
 -            raise binascii.Error("non-ascii character in hex-string")
++    return base64.b16encode(bs)
+ def unhex(es):
 -    return base64.b32encode(bs).decode("us-ascii")
+     return base64.b16decode(es)
+ def enb32(bs):
 -    if not isinstance(es, collections.ByteString):
 -        try:
 -            es = es.encode("us-ascii")
 -        except UnicodeError:
 -            raise binascii.Error("non-ascii character in base32-string")
++    return base64.b32encode(bs)
+ def unb32(es):
 -    return base64.b64encode(bs).decode("us-ascii")
+     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):
 -    if not isinstance(es, collections.ByteString):
 -        try:
 -            es = es.encode("us-ascii")
 -        except UnicodeError:
 -            raise binascii.Error("non-ascii character in base64-string")
++    return base64.b64encode(bs)
+ def unb64(es):
+     if (len(es) % 4) != 0:
+         es += b"=" * (4 - (len(es) % 4))
+     return base64.b64decode(es)
diff --cc wrw/session.py
@@@ -1,5 -1,5 +1,9 @@@
  import threading, time, pickle, random, os
++<<<<<<< HEAD
 +import cookie, env
++=======
+ from . import cookie, env, proto
++>>>>>>> master
  
  __all__ = ["db", "get"]
  
diff --cc wrw/util.py
Simple merge