
from zope.interface import implements

from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred, succeed
from twisted.web.iweb import IBodyProducer

class _DiscardReader(Protocol):
    def __init__(self, finished):
        self.finished = finished


    def dataReceived(self, bytes):
        pass


    def connectionLost(self, reason):
        self.finished.callback(None)



def readBody(response):
    if response.length == 0:
        return succeed(None)
    finished = Deferred()
    response.deliverBody(_DiscardReader(finished))
    return finished



class StringProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
        self._body = body
        self.length = len(self._body)


    def startProducing(self, consumer):
        consumer.write(self._body)
        return succeed(None)




from StringIO import StringIO

from zope.interface import implements

from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred, succeed
from twisted.web.iweb import IBodyProducer

class _BufferReader(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.received = StringIO()


    def dataReceived(self, bytes):
        self.received.write(bytes)


    def connectionLost(self, reason):
        self.finished.callback(self.received.getvalue())



def readBody(response):
    if response.length == 0:
        return succeed(None)
    finished = Deferred()
    response.deliverBody(_BufferReader(finished))
    return finished



class StringProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
        self._body = body
        self.length = len(self._body)


    def startProducing(self, consumer):
        consumer.write(self._body)
        return succeed(None)

Make the response body available via readBody
Make the response body available via readBody

git-svn-id: 81e381228600e5752b80483efd2b45b26c451ea2@6660 e27351fd-9f3e-4f54-a53b-843176b1656c

+ 
+ from StringIO import StringIO
  
  from zope.interface import implements
  
  from twisted.internet.protocol import Protocol
  from twisted.internet.defer import Deferred, succeed
  from twisted.web.iweb import IBodyProducer
  
- class _DiscardReader(Protocol):
+ class _BufferReader(Protocol):
      def __init__(self, finished):
          self.finished = finished
+         self.received = StringIO()
  
  
      def dataReceived(self, bytes):
-         pass
+         self.received.write(bytes)
  
  
      def connectionLost(self, reason):
-         self.finished.callback(None)
+         self.finished.callback(self.received.getvalue())
  
  
  
  def readBody(response):
      if response.length == 0:
          return succeed(None)
      finished = Deferred()
-     response.deliverBody(_DiscardReader(finished))
+     response.deliverBody(_BufferReader(finished))
      return finished
  
  
  
  class StringProducer(object):
      implements(IBodyProducer)
  
      def __init__(self, body):
          self._body = body
          self.length = len(self._body)
  
  
      def startProducing(self, consumer):
          consumer.write(self._body)
          return succeed(None)
  
- 
- 
Make the response body available via readBody
## Code Before:

from zope.interface import implements

from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred, succeed
from twisted.web.iweb import IBodyProducer

class _DiscardReader(Protocol):
    def __init__(self, finished):
        self.finished = finished


    def dataReceived(self, bytes):
        pass


    def connectionLost(self, reason):
        self.finished.callback(None)



def readBody(response):
    if response.length == 0:
        return succeed(None)
    finished = Deferred()
    response.deliverBody(_DiscardReader(finished))
    return finished



class StringProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
        self._body = body
        self.length = len(self._body)


    def startProducing(self, consumer):
        consumer.write(self._body)
        return succeed(None)



## Instruction:
Make the response body available via readBody
## Code After:

from StringIO import StringIO

from zope.interface import implements

from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred, succeed
from twisted.web.iweb import IBodyProducer

class _BufferReader(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.received = StringIO()


    def dataReceived(self, bytes):
        self.received.write(bytes)


    def connectionLost(self, reason):
        self.finished.callback(self.received.getvalue())



def readBody(response):
    if response.length == 0:
        return succeed(None)
    finished = Deferred()
    response.deliverBody(_BufferReader(finished))
    return finished



class StringProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
        self._body = body
        self.length = len(self._body)


    def startProducing(self, consumer):
        consumer.write(self._body)
        return succeed(None)
