Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from twisted.protocols.basic import Int16StringReceiver
- from struct import unpack
- def dataReceived(self, data):
- """
- Handle non-AMP messages, such as HTTP communication.
- """
- # print("dataReceived: {}".format(data))
- if data[:1] == NUL:
- # an AMP communication
- if data[-2:] != NULNUL:
- # an incomplete AMP box means more batches are forthcoming.
- self.multibatches += 1
- try:
- _get_logger().log_trace("incoming partial data: {}".format(to_str(data)))
- self.processData(data)
- except KeyError:
- _get_logger().log_trace("Discarded incoming partial data: {}".format(to_str(data)))
- self.repairData(data)
- #self.transport.loseConnection()
- elif self.multibatches:
- # invalid AMP, but we have a pending multi-batch that is not yet complete
- if data[-2:] == NULNUL:
- # end of existing multibatch
- self.multibatches = max(0, self.multibatches - 1)
- try:
- self.processData(data)
- except KeyError:
- _get_logger().log_trace("Discarded incoming multi-batch data:".format(to_str(data)))
- self.repairData(data)
- else:
- # not an AMP communication, return warning
- _get_logger().log_trace("Bad data: {}".format(to_str(data)))
- self.transport.write(_HTTP_WARNING)
- self.transport.loseConnection()
- print("HTTP received (the AMP port should not receive http, only AMP!) %s" % data)
- def processData(self,data):
- if self._justStartedTLS:
- self._justStartedTLS = False
- # If we already have an inner protocol, then we don't deliver data to
- # the protocol parser any more; we just hand it off.
- if self.innerProtocol is not None:
- self.innerProtocol.dataReceived(data)
- return
- return Int16StringReceiver.dataReceived(self, data)
- def repairData(self,data):
- alldata = self._unprocessed + data
- currentOffset = 0
- prefixLength = self.prefixLength
- fmt = self.structFormat
- self._unprocessed = alldata
- while len(alldata) >= (currentOffset + prefixLength) and not self.paused:
- messageStart = currentOffset + prefixLength
- length, = unpack(fmt, alldata[currentOffset:messageStart])
- if length > self.MAX_LENGTH:
- self._unprocessed = alldata
- self._compatibilityOffset = currentOffset
- self.lengthLimitExceeded(length)
- return
- messageEnd = messageStart + length
- if len(alldata) < messageEnd:
- break
- # Here we have to slice the working buffer so we can send just the
- # netstring into the stringReceived callback.
- packet = alldata[messageStart:messageEnd]
- currentOffset = messageEnd
- self._compatibilityOffset = currentOffset
- #self.stringReceived(packet)
- #We don't call stringReceived because we know the packet is broken
- # Check to see if the backwards compat "recvd" attribute got written
- # to by application code. If so, drop the current data buffer and
- # switch to the new buffer given by that attribute's value.
- if 'recvd' in self.__dict__:
- alldata = self.__dict__.pop('recvd')
- self._unprocessed = alldata
- self._compatibilityOffset = currentOffset = 0
- if alldata:
- continue
- return
- # Slice off all the data that has been processed, avoiding holding onto
- # memory to store it, and update the compatibility attributes to reflect
- # that change.
- self._unprocessed = alldata[currentOffset:]
- self._compatibilityOffset = 0
Advertisement
Add Comment
Please, Sign In to add comment