basic.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. # -*- test-case-name: twisted.protocols.test.test_basic -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Basic protocols, such as line-oriented, netstring, and int prefixed strings.
  6. """
  7. from __future__ import absolute_import, division
  8. # System imports
  9. import re
  10. from struct import pack, unpack, calcsize
  11. from io import BytesIO
  12. import math
  13. from zope.interface import implementer
  14. # Twisted imports
  15. from twisted.python.compat import _PY3
  16. from twisted.internet import protocol, defer, interfaces
  17. from twisted.python import log
  18. # Unfortunately we cannot use regular string formatting on Python 3; see
  19. # http://bugs.python.org/issue3982 for details.
  20. if _PY3:
  21. def _formatNetstring(data):
  22. return b''.join([str(len(data)).encode("ascii"), b':', data, b','])
  23. else:
  24. def _formatNetstring(data):
  25. return b'%d:%s,' % (len(data), data)
  26. _formatNetstring.__doc__ = """
  27. Convert some C{bytes} into netstring format.
  28. @param data: C{bytes} that will be reformatted.
  29. """
  30. DEBUG = 0
  31. class NetstringParseError(ValueError):
  32. """
  33. The incoming data is not in valid Netstring format.
  34. """
  35. class IncompleteNetstring(Exception):
  36. """
  37. Not enough data to complete a netstring.
  38. """
  39. class NetstringReceiver(protocol.Protocol):
  40. """
  41. A protocol that sends and receives netstrings.
  42. See U{http://cr.yp.to/proto/netstrings.txt} for the specification of
  43. netstrings. Every netstring starts with digits that specify the length
  44. of the data. This length specification is separated from the data by
  45. a colon. The data is terminated with a comma.
  46. Override L{stringReceived} to handle received netstrings. This
  47. method is called with the netstring payload as a single argument
  48. whenever a complete netstring is received.
  49. Security features:
  50. 1. Messages are limited in size, useful if you don't want
  51. someone sending you a 500MB netstring (change C{self.MAX_LENGTH}
  52. to the maximum length you wish to accept).
  53. 2. The connection is lost if an illegal message is received.
  54. @ivar MAX_LENGTH: Defines the maximum length of netstrings that can be
  55. received.
  56. @type MAX_LENGTH: C{int}
  57. @ivar _LENGTH: A pattern describing all strings that contain a netstring
  58. length specification. Examples for length specifications are C{b'0:'},
  59. C{b'12:'}, and C{b'179:'}. C{b'007:'} is not a valid length
  60. specification, since leading zeros are not allowed.
  61. @type _LENGTH: C{re.Match}
  62. @ivar _LENGTH_PREFIX: A pattern describing all strings that contain
  63. the first part of a netstring length specification (without the
  64. trailing comma). Examples are '0', '12', and '179'. '007' does not
  65. start a netstring length specification, since leading zeros are
  66. not allowed.
  67. @type _LENGTH_PREFIX: C{re.Match}
  68. @ivar _PARSING_LENGTH: Indicates that the C{NetstringReceiver} is in
  69. the state of parsing the length portion of a netstring.
  70. @type _PARSING_LENGTH: C{int}
  71. @ivar _PARSING_PAYLOAD: Indicates that the C{NetstringReceiver} is in
  72. the state of parsing the payload portion (data and trailing comma)
  73. of a netstring.
  74. @type _PARSING_PAYLOAD: C{int}
  75. @ivar brokenPeer: Indicates if the connection is still functional
  76. @type brokenPeer: C{int}
  77. @ivar _state: Indicates if the protocol is consuming the length portion
  78. (C{PARSING_LENGTH}) or the payload (C{PARSING_PAYLOAD}) of a netstring
  79. @type _state: C{int}
  80. @ivar _remainingData: Holds the chunk of data that has not yet been consumed
  81. @type _remainingData: C{string}
  82. @ivar _payload: Holds the payload portion of a netstring including the
  83. trailing comma
  84. @type _payload: C{BytesIO}
  85. @ivar _expectedPayloadSize: Holds the payload size plus one for the trailing
  86. comma.
  87. @type _expectedPayloadSize: C{int}
  88. """
  89. MAX_LENGTH = 99999
  90. _LENGTH = re.compile(br'(0|[1-9]\d*)(:)')
  91. _LENGTH_PREFIX = re.compile(br'(0|[1-9]\d*)$')
  92. # Some error information for NetstringParseError instances.
  93. _MISSING_LENGTH = ("The received netstring does not start with a "
  94. "length specification.")
  95. _OVERFLOW = ("The length specification of the received netstring "
  96. "cannot be represented in Python - it causes an "
  97. "OverflowError!")
  98. _TOO_LONG = ("The received netstring is longer than the maximum %s "
  99. "specified by self.MAX_LENGTH")
  100. _MISSING_COMMA = "The received netstring is not terminated by a comma."
  101. # The following constants are used for determining if the NetstringReceiver
  102. # is parsing the length portion of a netstring, or the payload.
  103. _PARSING_LENGTH, _PARSING_PAYLOAD = range(2)
  104. def makeConnection(self, transport):
  105. """
  106. Initializes the protocol.
  107. """
  108. protocol.Protocol.makeConnection(self, transport)
  109. self._remainingData = b""
  110. self._currentPayloadSize = 0
  111. self._payload = BytesIO()
  112. self._state = self._PARSING_LENGTH
  113. self._expectedPayloadSize = 0
  114. self.brokenPeer = 0
  115. def sendString(self, string):
  116. """
  117. Sends a netstring.
  118. Wraps up C{string} by adding length information and a
  119. trailing comma; writes the result to the transport.
  120. @param string: The string to send. The necessary framing (length
  121. prefix, etc) will be added.
  122. @type string: C{bytes}
  123. """
  124. self.transport.write(_formatNetstring(string))
  125. def dataReceived(self, data):
  126. """
  127. Receives some characters of a netstring.
  128. Whenever a complete netstring is received, this method extracts
  129. its payload and calls L{stringReceived} to process it.
  130. @param data: A chunk of data representing a (possibly partial)
  131. netstring
  132. @type data: C{bytes}
  133. """
  134. self._remainingData += data
  135. while self._remainingData:
  136. try:
  137. self._consumeData()
  138. except IncompleteNetstring:
  139. break
  140. except NetstringParseError:
  141. self._handleParseError()
  142. break
  143. def stringReceived(self, string):
  144. """
  145. Override this for notification when each complete string is received.
  146. @param string: The complete string which was received with all
  147. framing (length prefix, etc) removed.
  148. @type string: C{bytes}
  149. @raise NotImplementedError: because the method has to be implemented
  150. by the child class.
  151. """
  152. raise NotImplementedError()
  153. def _maxLengthSize(self):
  154. """
  155. Calculate and return the string size of C{self.MAX_LENGTH}.
  156. @return: The size of the string representation for C{self.MAX_LENGTH}
  157. @rtype: C{float}
  158. """
  159. return math.ceil(math.log10(self.MAX_LENGTH)) + 1
  160. def _consumeData(self):
  161. """
  162. Consumes the content of C{self._remainingData}.
  163. @raise IncompleteNetstring: if C{self._remainingData} does not
  164. contain enough data to complete the current netstring.
  165. @raise NetstringParseError: if the received data do not
  166. form a valid netstring.
  167. """
  168. if self._state == self._PARSING_LENGTH:
  169. self._consumeLength()
  170. self._prepareForPayloadConsumption()
  171. if self._state == self._PARSING_PAYLOAD:
  172. self._consumePayload()
  173. def _consumeLength(self):
  174. """
  175. Consumes the length portion of C{self._remainingData}.
  176. @raise IncompleteNetstring: if C{self._remainingData} contains
  177. a partial length specification (digits without trailing
  178. comma).
  179. @raise NetstringParseError: if the received data do not form a valid
  180. netstring.
  181. """
  182. lengthMatch = self._LENGTH.match(self._remainingData)
  183. if not lengthMatch:
  184. self._checkPartialLengthSpecification()
  185. raise IncompleteNetstring()
  186. self._processLength(lengthMatch)
  187. def _checkPartialLengthSpecification(self):
  188. """
  189. Makes sure that the received data represents a valid number.
  190. Checks if C{self._remainingData} represents a number smaller or
  191. equal to C{self.MAX_LENGTH}.
  192. @raise NetstringParseError: if C{self._remainingData} is no
  193. number or is too big (checked by L{_extractLength}).
  194. """
  195. partialLengthMatch = self._LENGTH_PREFIX.match(self._remainingData)
  196. if not partialLengthMatch:
  197. raise NetstringParseError(self._MISSING_LENGTH)
  198. lengthSpecification = (partialLengthMatch.group(1))
  199. self._extractLength(lengthSpecification)
  200. def _processLength(self, lengthMatch):
  201. """
  202. Processes the length definition of a netstring.
  203. Extracts and stores in C{self._expectedPayloadSize} the number
  204. representing the netstring size. Removes the prefix
  205. representing the length specification from
  206. C{self._remainingData}.
  207. @raise NetstringParseError: if the received netstring does not
  208. start with a number or the number is bigger than
  209. C{self.MAX_LENGTH}.
  210. @param lengthMatch: A regular expression match object matching
  211. a netstring length specification
  212. @type lengthMatch: C{re.Match}
  213. """
  214. endOfNumber = lengthMatch.end(1)
  215. startOfData = lengthMatch.end(2)
  216. lengthString = self._remainingData[:endOfNumber]
  217. # Expect payload plus trailing comma:
  218. self._expectedPayloadSize = self._extractLength(lengthString) + 1
  219. self._remainingData = self._remainingData[startOfData:]
  220. def _extractLength(self, lengthAsString):
  221. """
  222. Attempts to extract the length information of a netstring.
  223. @raise NetstringParseError: if the number is bigger than
  224. C{self.MAX_LENGTH}.
  225. @param lengthAsString: A chunk of data starting with a length
  226. specification
  227. @type lengthAsString: C{bytes}
  228. @return: The length of the netstring
  229. @rtype: C{int}
  230. """
  231. self._checkStringSize(lengthAsString)
  232. length = int(lengthAsString)
  233. if length > self.MAX_LENGTH:
  234. raise NetstringParseError(self._TOO_LONG % (self.MAX_LENGTH,))
  235. return length
  236. def _checkStringSize(self, lengthAsString):
  237. """
  238. Checks the sanity of lengthAsString.
  239. Checks if the size of the length specification exceeds the
  240. size of the string representing self.MAX_LENGTH. If this is
  241. not the case, the number represented by lengthAsString is
  242. certainly bigger than self.MAX_LENGTH, and a
  243. NetstringParseError can be raised.
  244. This method should make sure that netstrings with extremely
  245. long length specifications are refused before even attempting
  246. to convert them to an integer (which might trigger a
  247. MemoryError).
  248. """
  249. if len(lengthAsString) > self._maxLengthSize():
  250. raise NetstringParseError(self._TOO_LONG % (self.MAX_LENGTH,))
  251. def _prepareForPayloadConsumption(self):
  252. """
  253. Sets up variables necessary for consuming the payload of a netstring.
  254. """
  255. self._state = self._PARSING_PAYLOAD
  256. self._currentPayloadSize = 0
  257. self._payload.seek(0)
  258. self._payload.truncate()
  259. def _consumePayload(self):
  260. """
  261. Consumes the payload portion of C{self._remainingData}.
  262. If the payload is complete, checks for the trailing comma and
  263. processes the payload. If not, raises an L{IncompleteNetstring}
  264. exception.
  265. @raise IncompleteNetstring: if the payload received so far
  266. contains fewer characters than expected.
  267. @raise NetstringParseError: if the payload does not end with a
  268. comma.
  269. """
  270. self._extractPayload()
  271. if self._currentPayloadSize < self._expectedPayloadSize:
  272. raise IncompleteNetstring()
  273. self._checkForTrailingComma()
  274. self._state = self._PARSING_LENGTH
  275. self._processPayload()
  276. def _extractPayload(self):
  277. """
  278. Extracts payload information from C{self._remainingData}.
  279. Splits C{self._remainingData} at the end of the netstring. The
  280. first part becomes C{self._payload}, the second part is stored
  281. in C{self._remainingData}.
  282. If the netstring is not yet complete, the whole content of
  283. C{self._remainingData} is moved to C{self._payload}.
  284. """
  285. if self._payloadComplete():
  286. remainingPayloadSize = (self._expectedPayloadSize -
  287. self._currentPayloadSize)
  288. self._payload.write(self._remainingData[:remainingPayloadSize])
  289. self._remainingData = self._remainingData[remainingPayloadSize:]
  290. self._currentPayloadSize = self._expectedPayloadSize
  291. else:
  292. self._payload.write(self._remainingData)
  293. self._currentPayloadSize += len(self._remainingData)
  294. self._remainingData = b""
  295. def _payloadComplete(self):
  296. """
  297. Checks if enough data have been received to complete the netstring.
  298. @return: C{True} iff the received data contain at least as many
  299. characters as specified in the length section of the
  300. netstring
  301. @rtype: C{bool}
  302. """
  303. return (len(self._remainingData) + self._currentPayloadSize >=
  304. self._expectedPayloadSize)
  305. def _processPayload(self):
  306. """
  307. Processes the actual payload with L{stringReceived}.
  308. Strips C{self._payload} of the trailing comma and calls
  309. L{stringReceived} with the result.
  310. """
  311. self.stringReceived(self._payload.getvalue()[:-1])
  312. def _checkForTrailingComma(self):
  313. """
  314. Checks if the netstring has a trailing comma at the expected position.
  315. @raise NetstringParseError: if the last payload character is
  316. anything but a comma.
  317. """
  318. if self._payload.getvalue()[-1:] != b",":
  319. raise NetstringParseError(self._MISSING_COMMA)
  320. def _handleParseError(self):
  321. """
  322. Terminates the connection and sets the flag C{self.brokenPeer}.
  323. """
  324. self.transport.loseConnection()
  325. self.brokenPeer = 1
  326. class LineOnlyReceiver(protocol.Protocol):
  327. """
  328. A protocol that receives only lines.
  329. This is purely a speed optimisation over LineReceiver, for the
  330. cases that raw mode is known to be unnecessary.
  331. @cvar delimiter: The line-ending delimiter to use. By default this is
  332. C{b'\\r\\n'}.
  333. @cvar MAX_LENGTH: The maximum length of a line to allow (If a
  334. sent line is longer than this, the connection is dropped).
  335. Default is 16384.
  336. """
  337. _buffer = b''
  338. delimiter = b'\r\n'
  339. MAX_LENGTH = 16384
  340. def dataReceived(self, data):
  341. """
  342. Translates bytes into lines, and calls lineReceived.
  343. """
  344. lines = (self._buffer+data).split(self.delimiter)
  345. self._buffer = lines.pop(-1)
  346. for line in lines:
  347. if self.transport.disconnecting:
  348. # this is necessary because the transport may be told to lose
  349. # the connection by a line within a larger packet, and it is
  350. # important to disregard all the lines in that packet following
  351. # the one that told it to close.
  352. return
  353. if len(line) > self.MAX_LENGTH:
  354. return self.lineLengthExceeded(line)
  355. else:
  356. self.lineReceived(line)
  357. if len(self._buffer) > self.MAX_LENGTH:
  358. return self.lineLengthExceeded(self._buffer)
  359. def lineReceived(self, line):
  360. """
  361. Override this for when each line is received.
  362. @param line: The line which was received with the delimiter removed.
  363. @type line: C{bytes}
  364. """
  365. raise NotImplementedError
  366. def sendLine(self, line):
  367. """
  368. Sends a line to the other end of the connection.
  369. @param line: The line to send, not including the delimiter.
  370. @type line: C{bytes}
  371. """
  372. return self.transport.writeSequence((line, self.delimiter))
  373. def lineLengthExceeded(self, line):
  374. """
  375. Called when the maximum line length has been reached.
  376. Override if it needs to be dealt with in some special way.
  377. """
  378. return self.transport.loseConnection()
  379. class _PauseableMixin:
  380. paused = False
  381. def pauseProducing(self):
  382. self.paused = True
  383. self.transport.pauseProducing()
  384. def resumeProducing(self):
  385. self.paused = False
  386. self.transport.resumeProducing()
  387. self.dataReceived(b'')
  388. def stopProducing(self):
  389. self.paused = True
  390. self.transport.stopProducing()
  391. class LineReceiver(protocol.Protocol, _PauseableMixin):
  392. """
  393. A protocol that receives lines and/or raw data, depending on mode.
  394. In line mode, each line that's received becomes a callback to
  395. L{lineReceived}. In raw data mode, each chunk of raw data becomes a
  396. callback to L{LineReceiver.rawDataReceived}.
  397. The L{setLineMode} and L{setRawMode} methods switch between the two modes.
  398. This is useful for line-oriented protocols such as IRC, HTTP, POP, etc.
  399. @cvar delimiter: The line-ending delimiter to use. By default this is
  400. C{b'\\r\\n'}.
  401. @cvar MAX_LENGTH: The maximum length of a line to allow (If a
  402. sent line is longer than this, the connection is dropped).
  403. Default is 16384.
  404. """
  405. line_mode = 1
  406. _buffer = b''
  407. _busyReceiving = False
  408. delimiter = b'\r\n'
  409. MAX_LENGTH = 16384
  410. def clearLineBuffer(self):
  411. """
  412. Clear buffered data.
  413. @return: All of the cleared buffered data.
  414. @rtype: C{bytes}
  415. """
  416. b, self._buffer = self._buffer, b""
  417. return b
  418. def dataReceived(self, data):
  419. """
  420. Protocol.dataReceived.
  421. Translates bytes into lines, and calls lineReceived (or
  422. rawDataReceived, depending on mode.)
  423. """
  424. if self._busyReceiving:
  425. self._buffer += data
  426. return
  427. try:
  428. self._busyReceiving = True
  429. self._buffer += data
  430. while self._buffer and not self.paused:
  431. if self.line_mode:
  432. try:
  433. line, self._buffer = self._buffer.split(
  434. self.delimiter, 1)
  435. except ValueError:
  436. if len(self._buffer) >= (self.MAX_LENGTH
  437. + len(self.delimiter)):
  438. line, self._buffer = self._buffer, b''
  439. return self.lineLengthExceeded(line)
  440. return
  441. else:
  442. lineLength = len(line)
  443. if lineLength > self.MAX_LENGTH:
  444. exceeded = line + self.delimiter + self._buffer
  445. self._buffer = b''
  446. return self.lineLengthExceeded(exceeded)
  447. why = self.lineReceived(line)
  448. if (why or self.transport and
  449. self.transport.disconnecting):
  450. return why
  451. else:
  452. data = self._buffer
  453. self._buffer = b''
  454. why = self.rawDataReceived(data)
  455. if why:
  456. return why
  457. finally:
  458. self._busyReceiving = False
  459. def setLineMode(self, extra=b''):
  460. """
  461. Sets the line-mode of this receiver.
  462. If you are calling this from a rawDataReceived callback,
  463. you can pass in extra unhandled data, and that data will
  464. be parsed for lines. Further data received will be sent
  465. to lineReceived rather than rawDataReceived.
  466. Do not pass extra data if calling this function from
  467. within a lineReceived callback.
  468. """
  469. self.line_mode = 1
  470. if extra:
  471. return self.dataReceived(extra)
  472. def setRawMode(self):
  473. """
  474. Sets the raw mode of this receiver.
  475. Further data received will be sent to rawDataReceived rather
  476. than lineReceived.
  477. """
  478. self.line_mode = 0
  479. def rawDataReceived(self, data):
  480. """
  481. Override this for when raw data is received.
  482. """
  483. raise NotImplementedError
  484. def lineReceived(self, line):
  485. """
  486. Override this for when each line is received.
  487. @param line: The line which was received with the delimiter removed.
  488. @type line: C{bytes}
  489. """
  490. raise NotImplementedError
  491. def sendLine(self, line):
  492. """
  493. Sends a line to the other end of the connection.
  494. @param line: The line to send, not including the delimiter.
  495. @type line: C{bytes}
  496. """
  497. return self.transport.write(line + self.delimiter)
  498. def lineLengthExceeded(self, line):
  499. """
  500. Called when the maximum line length has been reached.
  501. Override if it needs to be dealt with in some special way.
  502. The argument 'line' contains the remainder of the buffer, starting
  503. with (at least some part) of the line which is too long. This may
  504. be more than one line, or may be only the initial portion of the
  505. line.
  506. """
  507. return self.transport.loseConnection()
  508. class StringTooLongError(AssertionError):
  509. """
  510. Raised when trying to send a string too long for a length prefixed
  511. protocol.
  512. """
  513. class _RecvdCompatHack(object):
  514. """
  515. Emulates the to-be-deprecated C{IntNStringReceiver.recvd} attribute.
  516. The C{recvd} attribute was where the working buffer for buffering and
  517. parsing netstrings was kept. It was updated each time new data arrived and
  518. each time some of that data was parsed and delivered to application code.
  519. The piecemeal updates to its string value were expensive and have been
  520. removed from C{IntNStringReceiver} in the normal case. However, for
  521. applications directly reading this attribute, this descriptor restores that
  522. behavior. It only copies the working buffer when necessary (ie, when
  523. accessed). This avoids the cost for applications not using the data.
  524. This is a custom descriptor rather than a property, because we still need
  525. the default __set__ behavior in both new-style and old-style subclasses.
  526. """
  527. def __get__(self, oself, type=None):
  528. return oself._unprocessed[oself._compatibilityOffset:]
  529. class IntNStringReceiver(protocol.Protocol, _PauseableMixin):
  530. """
  531. Generic class for length prefixed protocols.
  532. @ivar _unprocessed: bytes received, but not yet broken up into messages /
  533. sent to stringReceived. _compatibilityOffset must be updated when this
  534. value is updated so that the C{recvd} attribute can be generated
  535. correctly.
  536. @type _unprocessed: C{bytes}
  537. @ivar structFormat: format used for struct packing/unpacking. Define it in
  538. subclass.
  539. @type structFormat: C{str}
  540. @ivar prefixLength: length of the prefix, in bytes. Define it in subclass,
  541. using C{struct.calcsize(structFormat)}
  542. @type prefixLength: C{int}
  543. @ivar _compatibilityOffset: the offset within C{_unprocessed} to the next
  544. message to be parsed. (used to generate the recvd attribute)
  545. @type _compatibilityOffset: C{int}
  546. """
  547. MAX_LENGTH = 99999
  548. _unprocessed = b""
  549. _compatibilityOffset = 0
  550. # Backwards compatibility support for applications which directly touch the
  551. # "internal" parse buffer.
  552. recvd = _RecvdCompatHack()
  553. def stringReceived(self, string):
  554. """
  555. Override this for notification when each complete string is received.
  556. @param string: The complete string which was received with all
  557. framing (length prefix, etc) removed.
  558. @type string: C{bytes}
  559. """
  560. raise NotImplementedError
  561. def lengthLimitExceeded(self, length):
  562. """
  563. Callback invoked when a length prefix greater than C{MAX_LENGTH} is
  564. received. The default implementation disconnects the transport.
  565. Override this.
  566. @param length: The length prefix which was received.
  567. @type length: C{int}
  568. """
  569. self.transport.loseConnection()
  570. def dataReceived(self, data):
  571. """
  572. Convert int prefixed strings into calls to stringReceived.
  573. """
  574. # Try to minimize string copying (via slices) by keeping one buffer
  575. # containing all the data we have so far and a separate offset into that
  576. # buffer.
  577. alldata = self._unprocessed + data
  578. currentOffset = 0
  579. prefixLength = self.prefixLength
  580. fmt = self.structFormat
  581. self._unprocessed = alldata
  582. while len(alldata) >= (currentOffset + prefixLength) and not self.paused:
  583. messageStart = currentOffset + prefixLength
  584. length, = unpack(fmt, alldata[currentOffset:messageStart])
  585. if length > self.MAX_LENGTH:
  586. self._unprocessed = alldata
  587. self._compatibilityOffset = currentOffset
  588. self.lengthLimitExceeded(length)
  589. return
  590. messageEnd = messageStart + length
  591. if len(alldata) < messageEnd:
  592. break
  593. # Here we have to slice the working buffer so we can send just the
  594. # netstring into the stringReceived callback.
  595. packet = alldata[messageStart:messageEnd]
  596. currentOffset = messageEnd
  597. self._compatibilityOffset = currentOffset
  598. self.stringReceived(packet)
  599. # Check to see if the backwards compat "recvd" attribute got written
  600. # to by application code. If so, drop the current data buffer and
  601. # switch to the new buffer given by that attribute's value.
  602. if 'recvd' in self.__dict__:
  603. alldata = self.__dict__.pop('recvd')
  604. self._unprocessed = alldata
  605. self._compatibilityOffset = currentOffset = 0
  606. if alldata:
  607. continue
  608. return
  609. # Slice off all the data that has been processed, avoiding holding onto
  610. # memory to store it, and update the compatibility attributes to reflect
  611. # that change.
  612. self._unprocessed = alldata[currentOffset:]
  613. self._compatibilityOffset = 0
  614. def sendString(self, string):
  615. """
  616. Send a prefixed string to the other end of the connection.
  617. @param string: The string to send. The necessary framing (length
  618. prefix, etc) will be added.
  619. @type string: C{bytes}
  620. """
  621. if len(string) >= 2 ** (8 * self.prefixLength):
  622. raise StringTooLongError(
  623. "Try to send %s bytes whereas maximum is %s" % (
  624. len(string), 2 ** (8 * self.prefixLength)))
  625. self.transport.write(
  626. pack(self.structFormat, len(string)) + string)
  627. class Int32StringReceiver(IntNStringReceiver):
  628. """
  629. A receiver for int32-prefixed strings.
  630. An int32 string is a string prefixed by 4 bytes, the 32-bit length of
  631. the string encoded in network byte order.
  632. This class publishes the same interface as NetstringReceiver.
  633. """
  634. structFormat = "!I"
  635. prefixLength = calcsize(structFormat)
  636. class Int16StringReceiver(IntNStringReceiver):
  637. """
  638. A receiver for int16-prefixed strings.
  639. An int16 string is a string prefixed by 2 bytes, the 16-bit length of
  640. the string encoded in network byte order.
  641. This class publishes the same interface as NetstringReceiver.
  642. """
  643. structFormat = "!H"
  644. prefixLength = calcsize(structFormat)
  645. class Int8StringReceiver(IntNStringReceiver):
  646. """
  647. A receiver for int8-prefixed strings.
  648. An int8 string is a string prefixed by 1 byte, the 8-bit length of
  649. the string.
  650. This class publishes the same interface as NetstringReceiver.
  651. """
  652. structFormat = "!B"
  653. prefixLength = calcsize(structFormat)
  654. class StatefulStringProtocol:
  655. """
  656. A stateful string protocol.
  657. This is a mixin for string protocols (L{Int32StringReceiver},
  658. L{NetstringReceiver}) which translates L{stringReceived} into a callback
  659. (prefixed with C{'proto_'}) depending on state.
  660. The state C{'done'} is special; if a C{proto_*} method returns it, the
  661. connection will be closed immediately.
  662. @ivar state: Current state of the protocol. Defaults to C{'init'}.
  663. @type state: C{str}
  664. """
  665. state = 'init'
  666. def stringReceived(self, string):
  667. """
  668. Choose a protocol phase function and call it.
  669. Call back to the appropriate protocol phase; this begins with
  670. the function C{proto_init} and moves on to C{proto_*} depending on
  671. what each C{proto_*} function returns. (For example, if
  672. C{self.proto_init} returns 'foo', then C{self.proto_foo} will be the
  673. next function called when a protocol message is received.
  674. """
  675. try:
  676. pto = 'proto_' + self.state
  677. statehandler = getattr(self, pto)
  678. except AttributeError:
  679. log.msg('callback', self.state, 'not found')
  680. else:
  681. self.state = statehandler(string)
  682. if self.state == 'done':
  683. self.transport.loseConnection()
  684. @implementer(interfaces.IProducer)
  685. class FileSender:
  686. """
  687. A producer that sends the contents of a file to a consumer.
  688. This is a helper for protocols that, at some point, will take a
  689. file-like object, read its contents, and write them out to the network,
  690. optionally performing some transformation on the bytes in between.
  691. """
  692. CHUNK_SIZE = 2 ** 14
  693. lastSent = ''
  694. deferred = None
  695. def beginFileTransfer(self, file, consumer, transform=None):
  696. """
  697. Begin transferring a file
  698. @type file: Any file-like object
  699. @param file: The file object to read data from
  700. @type consumer: Any implementor of IConsumer
  701. @param consumer: The object to write data to
  702. @param transform: A callable taking one string argument and returning
  703. the same. All bytes read from the file are passed through this before
  704. being written to the consumer.
  705. @rtype: C{Deferred}
  706. @return: A deferred whose callback will be invoked when the file has
  707. been completely written to the consumer. The last byte written to the
  708. consumer is passed to the callback.
  709. """
  710. self.file = file
  711. self.consumer = consumer
  712. self.transform = transform
  713. self.deferred = deferred = defer.Deferred()
  714. self.consumer.registerProducer(self, False)
  715. return deferred
  716. def resumeProducing(self):
  717. chunk = ''
  718. if self.file:
  719. chunk = self.file.read(self.CHUNK_SIZE)
  720. if not chunk:
  721. self.file = None
  722. self.consumer.unregisterProducer()
  723. if self.deferred:
  724. self.deferred.callback(self.lastSent)
  725. self.deferred = None
  726. return
  727. if self.transform:
  728. chunk = self.transform(chunk)
  729. self.consumer.write(chunk)
  730. self.lastSent = chunk[-1:]
  731. def pauseProducing(self):
  732. pass
  733. def stopProducing(self):
  734. if self.deferred:
  735. self.deferred.errback(
  736. Exception("Consumer asked us to stop producing"))
  737. self.deferred = None