summaryrefslogtreecommitdiff
path: root/wsgitools/middlewares.py
diff options
context:
space:
mode:
Diffstat (limited to 'wsgitools/middlewares.py')
-rw-r--r--wsgitools/middlewares.py38
1 files changed, 15 insertions, 23 deletions
diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py
index 0f5e416..b37b130 100644
--- a/wsgitools/middlewares.py
+++ b/wsgitools/middlewares.py
@@ -1,21 +1,13 @@
__all__ = []
+import base64
import time
import sys
import cgitb
-import binascii
import collections
-# Cannot use io module as it is broken in 2.6.
-# Writing a str to a io.StringIO results in an exception.
-try:
- import cStringIO as io
-except ImportError:
- import StringIO as io
-try:
- next
-except NameError:
- def next(iterator):
- return iterator.next()
+import io
+
+from wsgitools.internal import bytes2str, str2bytes
if sys.version_info[0] >= 3:
def exc_info_for_raise(exc_info):
@@ -41,7 +33,7 @@ class SubdirMiddleware(object):
def __call__(self, environ, start_response):
"""wsgi interface
@type environ: {str: str}
- @rtype: gen([str])
+ @rtype: gen([bytes])
"""
assert isinstance(environ, dict)
app = None
@@ -65,7 +57,7 @@ __all__.append("NoWriteCallableMiddleware")
class NoWriteCallableMiddleware(object):
"""This middleware wraps a wsgi application that needs the return value of
C{start_response} function to a wsgi application that doesn't need one by
- writing the data to a C{StringIO} and then making it be the first result
+ writing the data to a C{BytesIO} and then making it be the first result
element."""
def __init__(self, app):
"""Wraps wsgi application app."""
@@ -73,11 +65,11 @@ class NoWriteCallableMiddleware(object):
def __call__(self, environ, start_response):
"""wsgi interface
@type environ: {str, str}
- @rtype: gen([str])
+ @rtype: gen([bytes])
"""
assert isinstance(environ, dict)
todo = [None]
- sio = io.StringIO()
+ sio = io.BytesIO()
gotiterdata = False
def write_calleable(data):
assert not gotiterdata
@@ -97,7 +89,7 @@ class NoWriteCallableMiddleware(object):
ret = self.app(environ, modified_start_response)
assert hasattr(ret, "__iter__")
- first = ""
+ first = b""
if not isinstance(ret, list):
ret = iter(ret)
stopped = False
@@ -179,7 +171,7 @@ class ContentLengthMiddleware(object):
return ret
ret = iter(ret)
- first = ""
+ first = b""
stopped = False
while not (first or stopped):
try:
@@ -357,14 +349,14 @@ class BasicAuthMiddleware(AuthenticationMiddleware):
self.app401 = app401
def authenticate(self, auth, environ):
- """
- @type environ: {str: object}
- """
+ assert isinstance(auth, str)
assert isinstance(environ, dict)
+ auth = str2bytes(auth)
try:
- auth_info = auth.decode("base64")
- except binascii.Error:
+ auth_info = base64.b64decode(auth)
+ except TypeError:
raise ProtocolViolation("failed to base64 decode auth_info")
+ auth_info = bytes2str(auth_info)
try:
username, password = auth_info.split(':', 1)
except ValueError: