-
Notifications
You must be signed in to change notification settings - Fork 10
/
basic.py
506 lines (406 loc) · 16.1 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# -*- test-case-name: twisted.test.test_protocols -*-
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Basic protocols, such as line-oriented, netstring, and int prefixed strings.
Maintainer: Itamar Shtull-Trauring
"""
# System imports
import re
import struct
from zope.interface import implements
# Twisted imports
from twisted.internet import protocol, defer, interfaces, error
from twisted.python import log
LENGTH, DATA, COMMA = range(3)
NUMBER = re.compile('(\d*)(:?)')
DEBUG = 0
class NetstringParseError(ValueError):
"""The incoming data is not in valid Netstring format."""
pass
class NetstringReceiver(protocol.Protocol):
"""This uses djb's Netstrings protocol to break up the input into strings.
Each string makes a callback to stringReceived, with a single
argument of that string.
Security features:
1. Messages are limited in size, useful if you don't want someone
sending you a 500MB netstring (change MAX_LENGTH to the maximum
length you wish to accept).
2. The connection is lost if an illegal message is received.
"""
MAX_LENGTH = 99999
brokenPeer = 0
_readerState = LENGTH
_readerLength = 0
def stringReceived(self, line):
"""
Override this.
"""
raise NotImplementedError
def doData(self):
buffer,self.__data = self.__data[:int(self._readerLength)],self.__data[int(self._readerLength):]
self._readerLength = self._readerLength - len(buffer)
self.__buffer = self.__buffer + buffer
if self._readerLength != 0:
return
self.stringReceived(self.__buffer)
self._readerState = COMMA
def doComma(self):
self._readerState = LENGTH
if self.__data[0] != ',':
if DEBUG:
raise NetstringParseError(repr(self.__data))
else:
raise NetstringParseError
self.__data = self.__data[1:]
def doLength(self):
m = NUMBER.match(self.__data)
if not m.end():
if DEBUG:
raise NetstringParseError(repr(self.__data))
else:
raise NetstringParseError
self.__data = self.__data[m.end():]
if m.group(1):
try:
self._readerLength = self._readerLength * (10**len(m.group(1))) + long(m.group(1))
except OverflowError:
raise NetstringParseError, "netstring too long"
if self._readerLength > self.MAX_LENGTH:
raise NetstringParseError, "netstring too long"
if m.group(2):
self.__buffer = ''
self._readerState = DATA
def dataReceived(self, data):
self.__data = data
try:
while self.__data:
if self._readerState == DATA:
self.doData()
elif self._readerState == COMMA:
self.doComma()
elif self._readerState == LENGTH:
self.doLength()
else:
raise RuntimeError, "mode is not DATA, COMMA or LENGTH"
except NetstringParseError:
self.transport.loseConnection()
self.brokenPeer = 1
def sendString(self, data):
self.transport.write('%d:%s,' % (len(data), data))
class SafeNetstringReceiver(NetstringReceiver):
"""This class is deprecated, use NetstringReceiver instead.
"""
class LineOnlyReceiver(protocol.Protocol):
"""A protocol that receives only lines.
This is purely a speed optimisation over LineReceiver, for the
cases that raw mode is known to be unnecessary.
@cvar delimiter: The line-ending delimiter to use. By default this is
'\\r\\n'.
@cvar MAX_LENGTH: The maximum length of a line to allow (If a
sent line is longer than this, the connection is dropped).
Default is 16384.
"""
_buffer = ''
delimiter = '\r\n'
MAX_LENGTH = 16384
def dataReceived(self, data):
"""Translates bytes into lines, and calls lineReceived."""
lines = (self._buffer+data).split(self.delimiter)
self._buffer = lines.pop(-1)
for line in lines:
if self.transport.disconnecting:
# this is necessary because the transport may be told to lose
# the connection by a line within a larger packet, and it is
# important to disregard all the lines in that packet following
# the one that told it to close.
return
if len(line) > self.MAX_LENGTH:
return self.lineLengthExceeded(line)
else:
self.lineReceived(line)
if len(self._buffer) > self.MAX_LENGTH:
return self.lineLengthExceeded(self._buffer)
def lineReceived(self, line):
"""Override this for when each line is received.
"""
raise NotImplementedError
def sendLine(self, line):
"""Sends a line to the other end of the connection.
"""
return self.transport.writeSequence((line,self.delimiter))
def lineLengthExceeded(self, line):
"""Called when the maximum line length has been reached.
Override if it needs to be dealt with in some special way.
"""
return error.ConnectionLost('Line length exceeded')
class _PauseableMixin:
paused = False
def pauseProducing(self):
self.paused = True
self.transport.pauseProducing()
def resumeProducing(self):
self.paused = False
self.transport.resumeProducing()
self.dataReceived('')
def stopProducing(self):
self.paused = True
self.transport.stopProducing()
class LineReceiver(protocol.Protocol, _PauseableMixin):
"""A protocol that receives lines and/or raw data, depending on mode.
In line mode, each line that's received becomes a callback to
L{lineReceived}. In raw data mode, each chunk of raw data becomes a
callback to L{rawDataReceived}. The L{setLineMode} and L{setRawMode}
methods switch between the two modes.
This is useful for line-oriented protocols such as IRC, HTTP, POP, etc.
@cvar delimiter: The line-ending delimiter to use. By default this is
'\\r\\n'.
@cvar MAX_LENGTH: The maximum length of a line to allow (If a
sent line is longer than this, the connection is dropped).
Default is 16384.
"""
line_mode = 1
__buffer = ''
delimiter = '\r\n'
MAX_LENGTH = 16384
def clearLineBuffer(self):
"""
Clear buffered data.
@return: All of the cleared buffered data.
@rtype: C{str}
"""
b = self.__buffer
self.__buffer = ""
return b
def dataReceived(self, data):
"""Protocol.dataReceived.
Translates bytes into lines, and calls lineReceived (or
rawDataReceived, depending on mode.)
"""
self.__buffer = self.__buffer+data
while self.line_mode and not self.paused:
try:
line, self.__buffer = self.__buffer.split(self.delimiter, 1)
except ValueError:
if len(self.__buffer) > self.MAX_LENGTH:
line, self.__buffer = self.__buffer, ''
return self.lineLengthExceeded(line)
break
else:
linelength = len(line)
if linelength > self.MAX_LENGTH:
exceeded = line + self.__buffer
self.__buffer = ''
return self.lineLengthExceeded(exceeded)
why = self.lineReceived(line)
if why or self.transport and self.transport.disconnecting:
return why
else:
if not self.paused:
data=self.__buffer
self.__buffer=''
if data:
return self.rawDataReceived(data)
def setLineMode(self, extra=''):
"""Sets the line-mode of this receiver.
If you are calling this from a rawDataReceived callback,
you can pass in extra unhandled data, and that data will
be parsed for lines. Further data received will be sent
to lineReceived rather than rawDataReceived.
Do not pass extra data if calling this function from
within a lineReceived callback.
"""
self.line_mode = 1
if extra:
return self.dataReceived(extra)
def setRawMode(self):
"""Sets the raw mode of this receiver.
Further data received will be sent to rawDataReceived rather
than lineReceived.
"""
self.line_mode = 0
def rawDataReceived(self, data):
"""Override this for when raw data is received.
"""
raise NotImplementedError
def lineReceived(self, line):
"""Override this for when each line is received.
"""
raise NotImplementedError
def sendLine(self, line):
"""Sends a line to the other end of the connection.
"""
return self.transport.write(line + self.delimiter)
def lineLengthExceeded(self, line):
"""Called when the maximum line length has been reached.
Override if it needs to be dealt with in some special way.
The argument 'line' contains the remainder of the buffer, starting
with (at least some part) of the line which is too long. This may
be more than one line, or may be only the initial portion of the
line.
"""
return self.transport.loseConnection()
class StringTooLongError(AssertionError):
"""
Raised when trying to send a string too long for a length prefixed
protocol.
"""
class IntNStringReceiver(protocol.Protocol, _PauseableMixin):
"""
Generic class for length prefixed protocols.
@ivar recvd: buffer holding received data when splitted.
@type recvd: C{str}
@ivar structFormat: format used for struct packing/unpacking. Define it in
subclass.
@type structFormat: C{str}
@ivar prefixLength: length of the prefix, in bytes. Define it in subclass,
using C{struct.calcsize(structFormat)}
@type prefixLength: C{int}
"""
MAX_LENGTH = 99999
recvd = ""
def stringReceived(self, msg):
"""
Override this.
"""
raise NotImplementedError
def lengthLimitExceeded(self, length):
"""
Callback invoked when a length prefix greater than C{MAX_LENGTH} is
received. The default implementation disconnects the transport.
Override this.
@param length: The length prefix which was received.
@type length: C{int}
"""
self.transport.loseConnection()
def dataReceived(self, recd):
"""
Convert int prefixed strings into calls to stringReceived.
"""
self.recvd = self.recvd + recd
while len(self.recvd) >= self.prefixLength and not self.paused:
length ,= struct.unpack(
self.structFormat, self.recvd[:self.prefixLength])
if length > self.MAX_LENGTH:
self.lengthLimitExceeded(length)
return
if len(self.recvd) < length + self.prefixLength:
break
packet = self.recvd[self.prefixLength:length + self.prefixLength]
self.recvd = self.recvd[length + self.prefixLength:]
self.stringReceived(packet)
def sendString(self, data):
"""
Send an prefixed string to the other end of the connection.
@type data: C{str}
"""
if len(data) >= 2 ** (8 * self.prefixLength):
raise StringTooLongError(
"Try to send %s bytes whereas maximum is %s" % (
len(data), 2 ** (8 * self.prefixLength)))
self.transport.write(struct.pack(self.structFormat, len(data)) + data)
class Int32StringReceiver(IntNStringReceiver):
"""
A receiver for int32-prefixed strings.
An int32 string is a string prefixed by 4 bytes, the 32-bit length of
the string encoded in network byte order.
This class publishes the same interface as NetstringReceiver.
"""
structFormat = "!I"
prefixLength = struct.calcsize(structFormat)
class Int16StringReceiver(IntNStringReceiver):
"""
A receiver for int16-prefixed strings.
An int16 string is a string prefixed by 2 bytes, the 16-bit length of
the string encoded in network byte order.
This class publishes the same interface as NetstringReceiver.
"""
structFormat = "!H"
prefixLength = struct.calcsize(structFormat)
class Int8StringReceiver(IntNStringReceiver):
"""
A receiver for int8-prefixed strings.
An int8 string is a string prefixed by 1 byte, the 8-bit length of
the string.
This class publishes the same interface as NetstringReceiver.
"""
structFormat = "!B"
prefixLength = struct.calcsize(structFormat)
class StatefulStringProtocol:
"""
A stateful string protocol.
This is a mixin for string protocols (Int32StringReceiver,
NetstringReceiver) which translates stringReceived into a callback
(prefixed with 'proto_') depending on state.
The state 'done' is special; if a proto_* method returns it, the
connection will be closed immediately.
"""
state = 'init'
def stringReceived(self,string):
"""Choose a protocol phase function and call it.
Call back to the appropriate protocol phase; this begins with
the function proto_init and moves on to proto_* depending on
what each proto_* function returns. (For example, if
self.proto_init returns 'foo', then self.proto_foo will be the
next function called when a protocol message is received.
"""
try:
pto = 'proto_'+self.state
statehandler = getattr(self,pto)
except AttributeError:
log.msg('callback',self.state,'not found')
else:
self.state = statehandler(string)
if self.state == 'done':
self.transport.loseConnection()
class FileSender:
"""A producer that sends the contents of a file to a consumer.
This is a helper for protocols that, at some point, will take a
file-like object, read its contents, and write them out to the network,
optionally performing some transformation on the bytes in between.
"""
implements(interfaces.IProducer)
CHUNK_SIZE = 2 ** 14
lastSent = ''
deferred = None
def beginFileTransfer(self, file, consumer, transform = None):
"""Begin transferring a file
@type file: Any file-like object
@param file: The file object to read data from
@type consumer: Any implementor of IConsumer
@param consumer: The object to write data to
@param transform: A callable taking one string argument and returning
the same. All bytes read from the file are passed through this before
being written to the consumer.
@rtype: C{Deferred}
@return: A deferred whose callback will be invoked when the file has been
completely written to the consumer. The last byte written to the consumer
is passed to the callback.
"""
self.file = file
self.consumer = consumer
self.transform = transform
self.deferred = deferred = defer.Deferred()
self.consumer.registerProducer(self, False)
return deferred
def resumeProducing(self):
chunk = ''
if self.file:
chunk = self.file.read(self.CHUNK_SIZE)
if not chunk:
self.file = None
self.consumer.unregisterProducer()
if self.deferred:
self.deferred.callback(self.lastSent)
self.deferred = None
return
if self.transform:
chunk = self.transform(chunk)
self.consumer.write(chunk)
self.lastSent = chunk[-1]
def pauseProducing(self):
pass
def stopProducing(self):
if self.deferred:
self.deferred.errback(Exception("Consumer asked us to stop producing"))
self.deferred = None