Merge branch 'master' into jython
authorFredrik Tolf <fredrik@dolda2000.com>
Thu, 30 Aug 2012 08:30:53 +0000 (10:30 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Thu, 30 Aug 2012 08:35:17 +0000 (10:35 +0200)
1  2 
wrw/req.py

diff --cc wrw/req.py
@@@ -1,3 -1,5 +1,5 @@@
 -import io
++import StringIO
  __all__ = ["request"]
  
  class headdict(object):
@@@ -49,6 -51,71 +51,71 @@@ def fixcase(str)
          i += 1
      return str
  
 -        self.buf = bytearray()
+ class limitreader(object):
+     def __init__(self, back, limit):
+         self.bk = back
+         self.limit = limit
+         self.rb = 0
 -            self.buf.extend(ret)
++        self.buf = ""
+     def close(self):
+         pass
+     def read(self, size=-1):
+         ra = self.limit - self.rb
+         if size >= 0:
+             ra = min(ra, size)
+         while len(self.buf) < ra:
+             ret = self.bk.read(ra - len(self.buf))
+             if ret == "":
+                 raise IOError("Unexpected EOF")
 -        ret = str(self.buf[:ra])
++            self.buf += ret
+             self.rb += len(ret)
 -                ret = str(self.buf[:p + 1])
++        ret = self.buf[:ra]
+         self.buf = self.buf[ra:]
+         return ret
+     def readline(self, size=-1):
+         off = 0
+         while True:
+             p = self.buf.find('\n', off)
+             if p >= 0:
 -                ret = str(self.buf[:size])
++                ret = self.buf[:p + 1]
+                 self.buf = self.buf[p + 1:]
+                 return ret
+             off = len(self.buf)
+             if size >= 0 and len(self.buf) >= size:
 -                ret = str(self.buf)
 -                self.buf = bytearray()
++                ret = self.buf[:size]
+                 self.buf = self.buf[size:]
+                 return ret
+             if self.rb == self.limit:
 -            self.buf.extend(ret)
++                ret = self.buf
++                self.buf = ""
+                 return ret
+             ra = self.limit - self.rb
+             if size >= 0:
+                 ra = min(ra, size)
+             ra = min(ra, 1024)
+             ret = self.bk.read(ra)
+             if ret == "":
+                 raise IOError("Unpexpected EOF")
++            self.buf += ret
+             self.rb += len(ret)
+     def readlines(self, hint=None):
+         return list(self)
+     def __iter__(rd):
+         class lineiter(object):
+             def __iter__(self):
+                 return self
+             def next(self):
+                 ret = rd.readline()
+                 if ret == "":
+                     raise StopIteration()
+                 return ret
+         return lineiter()
  class request(object):
      def copy(self):
          return copyrequest(self)
@@@ -76,7 -144,11 +144,11 @@@ class origrequest(request)
          if "CONTENT_TYPE" in env:
              self.ihead["Content-Type"] = env["CONTENT_TYPE"]
          if "CONTENT_LENGTH" in env:
-             self.ihead["Content-Length"] = env["CONTENT_LENGTH"]
+             clen = self.ihead["Content-Length"] = env["CONTENT_LENGTH"]
+             if clen.isdigit():
+                 self.input = limitreader(env["wsgi.input"], int(clen))
+         if self.input is None:
 -            self.input = io.BytesIO("")
++            self.input = StringIO.StringIO("")
          self.ohead = headdict()
          for k, v in env.items():
              if k[:5] == "HTTP_":