+class PipelineError(Exception):
+ """An error has occurred in pipelining requests."""
+
+class LoggingHTTPClientProtocol(HTTPClientProtocol):
+ """A modified client protocol that logs the number of bytes received."""
+
+ def __init__(self, factory, stats = None, mirror = False):
+ HTTPClientProtocol.__init__(self, factory)
+ self.stats = stats
+ self.mirror = mirror
+
+ def lineReceived(self, line):
+ if self.stats:
+ self.stats.receivedBytes(len(line) + 2, self.mirror)
+ HTTPClientProtocol.lineReceived(self, line)
+
+ def rawDataReceived(self, data):
+ if self.stats:
+ self.stats.receivedBytes(len(data), self.mirror)
+ HTTPClientProtocol.rawDataReceived(self, data)
+
+ def setReadPersistent(self, persist):
+ self.readPersistent = persist
+ if not persist:
+ # Tell all requests but first to abort.
+ lostRequests = self.inRequests[1:]
+ del self.inRequests[1:]
+ for request in lostRequests:
+ request.connectionLost(PipelineError('The pipelined connection was lost'))
+