Commit c8abc749 authored by Gerhard Weis's avatar Gerhard Weis

support IStreamIterator as WSGI response body

parent 7c44ca63
...@@ -130,12 +130,16 @@ class WSGIResponse(HTTPResponse): ...@@ -130,12 +130,16 @@ class WSGIResponse(HTTPResponse):
self.stdout.write(data) self.stdout.write(data)
def setBody(self, body, title='', is_error=0): def setBody(self, body, title='', is_error=0):
if isinstance(body, file) or IStreamIterator.providedBy(body): if isinstance(body, file):
body.seek(0, 2) body.seek(0, 2)
length = body.tell() length = body.tell()
body.seek(0) body.seek(0)
self.setHeader('Content-Length', '%d' % length) self.setHeader('Content-Length', '%d' % length)
self.body = body self.body = body
elif IStreamIterator.providedBy(body):
self.body = body
self._streaming = 1
HTTPResponse.setBody(self, '', title, is_error)
else: else:
HTTPResponse.setBody(self, body, title, is_error) HTTPResponse.setBody(self, body, title, is_error)
......
...@@ -136,6 +136,28 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -136,6 +136,28 @@ class WSGIResponseTests(unittest.TestCase):
time.gmtime(time.mktime(WHEN))) time.gmtime(time.mktime(WHEN)))
self.assertTrue(('Date', whenstr) in headers) self.assertTrue(('Date', whenstr) in headers)
def test_setBody_IStreamIterator(self):
from ZPublisher.Iterators import IStreamIterator
from zope.interface import implements
class test_streamiterator:
implements(IStreamIterator)
data = "hello"
done = 0
def next(self):
if not self.done:
self.done = 1
return self.data
raise StopIteration
response = self._makeOne()
response.setStatus(200)
body = test_streamiterator()
response.setBody(body)
response.finalize()
self.assertTrue(body is response.body)
#def test___str__already_wrote_not_chunking(self): #def test___str__already_wrote_not_chunking(self):
# response = self._makeOne() # response = self._makeOne()
# response._wrote = True # response._wrote = True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment