python: Fixed SCGI handler typo.
[ashd.git] / python / ashd / scgi.py
index 9357e9d..4583fa7 100644 (file)
@@ -4,6 +4,10 @@ import threading
 class protoerr(Exception):
     pass
 
+class closed(IOError):
+    def __init__(self):
+        super(closed, self).__init__("The client has closed the connection.")
+
 def readns(sk):
     hln = 0
     while True:
@@ -74,9 +78,7 @@ def wrapwsgi(handler):
         resp = []
         respsent = []
 
-        def write(data):
-            if not data:
-                return
+        def flushreq():
             if not respsent:
                 if not resp:
                     raise Exception, "Trying to write data before starting response."
@@ -86,8 +88,16 @@ def wrapwsgi(handler):
                 for nm, val in headers:
                     sk.write("%s: %s\n" % (nm, val))
                 sk.write("\n")
-            sk.write(data)
-            sk.flush()
+
+        def write(data):
+            if not data:
+                return
+            try:
+                flushreq()
+                sk.write(data)
+                sk.flush()
+            except IOError:
+                raise closed()
 
         def startreq(status, headers, exc_info = None):
             if resp:
@@ -106,7 +116,8 @@ def wrapwsgi(handler):
         try:
             for data in respiter:
                 write(data)
-            write("")
+            if resp:
+                flushreq()
         finally:
             if hasattr(respiter, "close"):
                 respiter.close()