diff options
-rw-r--r-- | wsgitools/scgi/forkpool.py | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/wsgitools/scgi/forkpool.py b/wsgitools/scgi/forkpool.py index 90c836a..26de9ef 100644 --- a/wsgitools/scgi/forkpool.py +++ b/wsgitools/scgi/forkpool.py @@ -326,15 +326,42 @@ class SCGIServer: value = data.pop(0) environ[key] = value + # elements: + # 0 -> None: no headers set + # 0 -> False: set but unsent + # 0 -> True: sent + # 1 -> status string + # 2 -> header list + response_head = [None, None, None] + + def sendheaders(): + assert response_head[0] is not None # headers set + if response_head[0] != True: + response_head[0] = True + try: + con.sendall('Status: %s\r\n%s\r\n\r\n' % (response_head[1], + '\r\n'.join(map("%s: %s".__mod__, + response_head[2])))) + except socket.error: + pass + def dumbsend(data): + sendheaders() try: con.sendall(data) except socket.error: pass def start_response(status, headers, exc_info=None): - dumbsend('Status: %s\r\n%s\r\n\r\n' % (status, - '\r\n'.join(map("%s: %s".__mod__, headers)))) + if exc_info and response_head[0]: + try: + raise exc_info[0], exc_info[1], exc_info[2] + finally: + exc_info = None + assert not response_head[0] # unset or not sent + response_head[0] = False # set but nothing sent + response_head[1] = status + response_head[2] = headers return dumbsend if not environ.get("CONTENT_LENGTH", "bad").isdigit(): @@ -360,9 +387,13 @@ class SCGIServer: result = self.wsgiapp(environ, start_response) assert hasattr(result, "__iter__") - for data in result: + assert response_head[0] is not None + result_iter = iter(result) + for data in result_iter: assert isinstance(data, str) dumbsend(data) + if response_head[0] != True: + sendheaders() if hasattr(result, "close"): result.close() sfw.close() |