summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2012-06-29 07:38:57 +0200
committerHelmut Grohne <helmut@subdivi.de>2012-06-29 19:18:39 +0200
commitd43d3d8947964392644b84ec1bce76c6f4193bea (patch)
treecfa2eae5ea7073396933883ad852656fdfff8163
parentdd015aa622b25f8638ab0a13b5d75004d16004c8 (diff)
downloadwsgitools-d43d3d8947964392644b84ec1bce76c6f4193bea.tar.gz
scgi.asynchronous: move {en,de}coding to internal module
-rw-r--r--wsgitools/internal.py14
-rw-r--r--wsgitools/scgi/asynchronous.py11
2 files changed, 17 insertions, 8 deletions
diff --git a/wsgitools/internal.py b/wsgitools/internal.py
new file mode 100644
index 0000000..c392b6a
--- /dev/null
+++ b/wsgitools/internal.py
@@ -0,0 +1,14 @@
+if bytes is str:
+ def bytes2str(bstr):
+ assert isinstance(bstr, bytes)
+ return bstr
+ def str2bytes(sstr):
+ assert isinstance(sstr, str)
+ return sstr
+else:
+ def bytes2str(bstr):
+ assert isinstance(bstr, bytes)
+ return bstr.decode("iso-8859-1") # always successful
+ def str2bytes(sstr):
+ assert isinstance(sstr, str)
+ return sstr.encode("iso-8859-1") # might fail, but spec says it doesn't
diff --git a/wsgitools/scgi/asynchronous.py b/wsgitools/scgi/asynchronous.py
index 2c97f42..3009593 100644
--- a/wsgitools/scgi/asynchronous.py
+++ b/wsgitools/scgi/asynchronous.py
@@ -6,6 +6,7 @@ import socket
import sys
import errno
+from wsgitools.internal import bytes2str, str2bytes
from wsgitools.scgi import _convert_environ, FileWrapper
if sys.version_info[0] >= 3:
@@ -51,9 +52,7 @@ class SCGIConnection(asyncore.dispatcher):
status, headers = self.outheaders
headdata = "".join(map("%s: %s\r\n".__mod__, headers))
headdata = "Status: %s\r\n%s\r\n" % (status, headdata)
- if not isinstance(headdata, bytes):
- headdata = headdata.encode("iso-8859-1")
- self.outbuff = headdata
+ self.outbuff = str2bytes(headdata)
self.outheaders = True
def _wsgi_write(self, data):
@@ -98,10 +97,7 @@ class SCGIConnection(asyncore.dispatcher):
while buff.count(b'\0') >= 2:
key, value, buff = buff.split(b'\0', 2)
- if not isinstance(key, str):
- key = key.decode("iso-8859-1")
- value = value.decode("iso-8859-1")
- self.environ[key] = value
+ self.environ[bytes2str(key)] = bytes2str(value)
self.reqlen -= len(key) + len(value) + 2
self.inbuff = buff + remainder
@@ -276,4 +272,3 @@ class SCGIServer(asyncore.dispatcher):
"""Runs the server. It will not return and you can invoke
C{asyncore.loop()} instead achieving the same effect."""
asyncore.loop()
-