Guest User

Untitled

a guest
Jun 23rd, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. from twisted.protocols.basic import Int16StringReceiver
  2. from struct import unpack
  3.  
  4. def dataReceived(self, data):
  5.         """
  6.        Handle non-AMP messages, such as HTTP communication.
  7.        """
  8.        
  9.        
  10.        
  11.         # print("dataReceived: {}".format(data))
  12.         if data[:1] == NUL:
  13.             # an AMP communication
  14.             if data[-2:] != NULNUL:
  15.                 # an incomplete AMP box means more batches are forthcoming.
  16.                 self.multibatches += 1
  17.             try:
  18.                 _get_logger().log_trace("incoming partial data: {}".format(to_str(data)))
  19.                 self.processData(data)
  20.             except KeyError:
  21.                 _get_logger().log_trace("Discarded incoming partial data: {}".format(to_str(data)))
  22.                 self.repairData(data)
  23.                 #self.transport.loseConnection()
  24.         elif self.multibatches:
  25.             # invalid AMP, but we have a pending multi-batch that is not yet complete
  26.             if data[-2:] == NULNUL:
  27.                 # end of existing multibatch
  28.                 self.multibatches = max(0, self.multibatches - 1)
  29.             try:
  30.                 self.processData(data)
  31.             except KeyError:
  32.                 _get_logger().log_trace("Discarded incoming multi-batch data:".format(to_str(data)))
  33.                 self.repairData(data)
  34.         else:
  35.             # not an AMP communication, return warning
  36.             _get_logger().log_trace("Bad data: {}".format(to_str(data)))
  37.             self.transport.write(_HTTP_WARNING)
  38.             self.transport.loseConnection()
  39.             print("HTTP received (the AMP port should not receive http, only AMP!) %s" % data)
  40.            
  41.     def processData(self,data):
  42.         if self._justStartedTLS:
  43.             self._justStartedTLS = False
  44.         # If we already have an inner protocol, then we don't deliver data to
  45.         # the protocol parser any more; we just hand it off.
  46.         if self.innerProtocol is not None:
  47.             self.innerProtocol.dataReceived(data)
  48.             return
  49.         return Int16StringReceiver.dataReceived(self, data)
  50.    
  51.     def repairData(self,data):
  52.         alldata = self._unprocessed + data
  53.         currentOffset = 0
  54.         prefixLength = self.prefixLength
  55.         fmt = self.structFormat
  56.         self._unprocessed = alldata
  57.  
  58.         while len(alldata) >= (currentOffset + prefixLength) and not self.paused:
  59.             messageStart = currentOffset + prefixLength
  60.             length, = unpack(fmt, alldata[currentOffset:messageStart])
  61.             if length > self.MAX_LENGTH:
  62.                 self._unprocessed = alldata
  63.                 self._compatibilityOffset = currentOffset
  64.                 self.lengthLimitExceeded(length)
  65.                 return
  66.             messageEnd = messageStart + length
  67.             if len(alldata) < messageEnd:
  68.                 break
  69.  
  70.             # Here we have to slice the working buffer so we can send just the
  71.             # netstring into the stringReceived callback.
  72.             packet = alldata[messageStart:messageEnd]
  73.             currentOffset = messageEnd
  74.             self._compatibilityOffset = currentOffset
  75.             #self.stringReceived(packet)
  76.             #We don't call stringReceived because we know the packet is broken
  77.  
  78.             # Check to see if the backwards compat "recvd" attribute got written
  79.             # to by application code.  If so, drop the current data buffer and
  80.             # switch to the new buffer given by that attribute's value.
  81.             if 'recvd' in self.__dict__:
  82.                 alldata = self.__dict__.pop('recvd')
  83.                 self._unprocessed = alldata
  84.                 self._compatibilityOffset = currentOffset = 0
  85.                 if alldata:
  86.                     continue
  87.                 return
  88.  
  89.         # Slice off all the data that has been processed, avoiding holding onto
  90.         # memory to store it, and update the compatibility attributes to reflect
  91.         # that change.
  92.         self._unprocessed = alldata[currentOffset:]
  93.         self._compatibilityOffset = 0
Advertisement
Add Comment
Please, Sign In to add comment