frame.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import struct
  2. import socket
  3. from os import urandom
  4. from string import printable
  5. OPCODE_CONTINUATION = 0x0
  6. OPCODE_TEXT = 0x1
  7. OPCODE_BINARY = 0x2
  8. OPCODE_CLOSE = 0x8
  9. OPCODE_PING = 0x9
  10. OPCODE_PONG = 0xA
  11. CLOSE_NORMAL = 1000
  12. CLOSE_GOING_AWAY = 1001
  13. CLOSE_PROTOCOL_ERROR = 1002
  14. CLOSE_NOACCEPT_DTYPE = 1003
  15. CLOSE_INVALID_DATA = 1007
  16. CLOSE_POLICY = 1008
  17. CLOSE_MESSAGE_TOOBIG = 1009
  18. CLOSE_MISSING_EXTENSIONS = 1010
  19. CLOSE_UNABLE = 1011
  20. line_printable = [c for c in printable if c not in '\r\n\x0b\x0c']
  21. def printstr(s):
  22. return ''.join(c if c in line_printable else '.' for c in str(s))
  23. class Frame(object):
  24. """
  25. A Frame instance represents a web socket data frame as defined in RFC 6455.
  26. To encoding a frame for sending it over a socket, use Frame.pack(). To
  27. receive and decode a frame from a socket, use receive_frame().
  28. """
  29. def __init__(self, opcode, payload, masking_key='', mask=False, final=True,
  30. rsv1=False, rsv2=False, rsv3=False):
  31. """
  32. Create a new frame.
  33. `opcode` is one of the constants as defined above.
  34. `payload` is a string of bytes containing the data sendt in the frame.
  35. `masking_key` is an optional custom key to use for masking, or `mask`
  36. can be used instead to let this constructor generate a random masking
  37. key.
  38. `final` is a boolean indicating whether this frame is the last in a
  39. chain of fragments.
  40. `rsv1`, `rsv2` and `rsv3` are booleans indicating bit values for RSV1,
  41. RVS2 and RSV3, which are only non-zero if defined so by extensions.
  42. """
  43. if mask:
  44. masking_key = urandom(4)
  45. if len(masking_key) not in (0, 4):
  46. raise ValueError('invalid masking key "%s"' % masking_key)
  47. self.final = final
  48. self.rsv1 = rsv1
  49. self.rsv2 = rsv2
  50. self.rsv3 = rsv3
  51. self.opcode = opcode
  52. self.masking_key = masking_key
  53. self.payload = payload
  54. def pack(self):
  55. """
  56. Pack the frame into a string according to the following scheme:
  57. +-+-+-+-+-------+-+-------------+-------------------------------+
  58. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  59. |I|S|S|S| (4) |A| (7) | (16/64) |
  60. |N|V|V|V| |S| | (if payload len==126/127) |
  61. | |1|2|3| |K| | |
  62. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  63. | Extended payload length continued, if payload len == 127 |
  64. + - - - - - - - - - - - - - - - +-------------------------------+
  65. | |Masking-key, if MASK set to 1 |
  66. +-------------------------------+-------------------------------+
  67. | Masking-key (continued) | Payload Data |
  68. +-------------------------------- - - - - - - - - - - - - - - - +
  69. : Payload Data continued ... :
  70. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  71. | Payload Data continued ... |
  72. +---------------------------------------------------------------+
  73. """
  74. header = struct.pack('!B', (self.final << 7) | (self.rsv1 << 6)
  75. | (self.rsv2 << 5) | (self.rsv3 << 4)
  76. | (self.opcode & 0xf))
  77. mask = bool(self.masking_key) << 7
  78. payload_len = len(self.payload)
  79. if payload_len <= 125:
  80. header += struct.pack('!B', mask | payload_len)
  81. elif payload_len < (1 << 16):
  82. header += struct.pack('!BH', mask | 126, payload_len)
  83. elif payload_len < (1 << 63):
  84. header += struct.pack('!BQ', mask | 127, payload_len)
  85. else:
  86. # FIXME: RFC 6455 defines an action for this...
  87. raise Exception('the payload length is too damn high!')
  88. if mask:
  89. return header + self.masking_key + self.mask_payload()
  90. return header + self.payload
  91. def mask_payload(self):
  92. return mask(self.masking_key, self.payload)
  93. def fragment(self, fragment_size, mask=False):
  94. """
  95. Fragment the frame into a chain of fragment frames:
  96. - An initial frame with non-zero opcode
  97. - Zero or more frames with opcode = 0 and final = False
  98. - A final frame with opcode = 0 and final = True
  99. The first and last frame may be the same frame, having a non-zero
  100. opcode and final = True. Thus, this function returns a list containing
  101. at least a single frame.
  102. `fragment_size` indicates the maximum payload size of each fragment.
  103. The payload of the original frame is split into one or more parts, and
  104. each part is converted to a Frame instance.
  105. `mask` is a boolean (default False) indicating whether the payloads
  106. should be masked. If True, each frame is assigned a randomly generated
  107. masking key.
  108. """
  109. frames = []
  110. for start in xrange(0, len(self.payload), fragment_size):
  111. payload = self.payload[start:start + fragment_size]
  112. frames.append(Frame(OPCODE_CONTINUATION, payload, mask=mask,
  113. final=False))
  114. frames[0].opcode = self.opcode
  115. frames[-1].final = True
  116. return frames
  117. def __str__(self):
  118. s = '<%s opcode=0x%X len=%d' \
  119. % (self.__class__.__name__, self.opcode, len(self.payload))
  120. if self.masking_key:
  121. s += ' masking_key=%4s' % printstr(self.masking_key)
  122. max_pl_disp = 30
  123. pl = printstr(self.payload)[:max_pl_disp]
  124. if len(self.payload) > max_pl_disp:
  125. pl += '...'
  126. s += ' payload=%s' % pl
  127. if self.rsv1:
  128. s += ' rsv1'
  129. if self.rsv2:
  130. s += ' rsv2'
  131. if self.rsv3:
  132. s += ' rsv3'
  133. return s + '>'
  134. class ControlFrame(Frame):
  135. """
  136. A control frame is a frame with an opcode OPCODE_CLOSE, OPCODE_PING or
  137. OPCODE_PONG. These frames must be handled as defined by RFC 6455, and
  138. """
  139. def fragment(self, fragment_size, mask=False):
  140. """
  141. Control frames must not be fragmented.
  142. """
  143. raise TypeError('control frames must not be fragmented')
  144. def pack(self):
  145. """
  146. Same as Frame.pack(), but asserts that the payload size does not exceed
  147. 125 bytes.
  148. """
  149. if len(self.payload) > 125:
  150. raise ValueError('control frames must not be larger than 125 '
  151. 'bytes')
  152. return Frame.pack(self)
  153. def unpack_close(self):
  154. """
  155. Unpack a close message into a status code and a reason. If no payload
  156. is given, the code is None and the reason is an empty string.
  157. """
  158. if self.payload:
  159. code = struct.unpack('!H', str(self.payload[:2]))[0]
  160. reason = str(self.payload[2:])
  161. else:
  162. code = None
  163. reason = ''
  164. return code, reason
  165. def decode_frame(reader):
  166. b1, b2 = struct.unpack('!BB', reader.readn(2))
  167. final = bool(b1 & 0x80)
  168. rsv1 = bool(b1 & 0x40)
  169. rsv2 = bool(b1 & 0x20)
  170. rsv3 = bool(b1 & 0x10)
  171. opcode = b1 & 0x0F
  172. masked = bool(b2 & 0x80)
  173. payload_len = b2 & 0x7F
  174. if payload_len == 126:
  175. payload_len = struct.unpack('!H', reader.readn(2))
  176. elif payload_len == 127:
  177. payload_len = struct.unpack('!Q', reader.readn(8))
  178. if masked:
  179. masking_key = reader.readn(4)
  180. payload = mask(masking_key, reader.readn(payload_len))
  181. else:
  182. masking_key = ''
  183. payload = reader.readn(payload_len)
  184. # Control frames have most significant bit 1
  185. cls = ControlFrame if opcode & 0x8 else Frame
  186. return cls(opcode, payload, masking_key=masking_key, final=final,
  187. rsv1=rsv1, rsv2=rsv2, rsv3=rsv3)
  188. def receive_frame(sock):
  189. return decode_frame(SocketReader(sock))
  190. def read_frame(data):
  191. reader = BufferReader(data)
  192. frame = decode_frame(reader)
  193. return frame, reader.offset
  194. def pop_frame(data):
  195. frame, size = read_frame(data)
  196. return frame, data[size:]
  197. class BufferReader(object):
  198. def __init__(self, data):
  199. self.data = data
  200. self.offset = 0
  201. def readn(self, n):
  202. assert len(self.data) - self.offset >= n
  203. self.offset += n
  204. return self.data[self.offset - n:self.offset]
  205. class SocketReader(object):
  206. def __init__(self, sock):
  207. self.sock = sock
  208. def readn(self, n):
  209. """
  210. Keep receiving data until exactly `n` bytes have been read.
  211. """
  212. data = ''
  213. while len(data) < n:
  214. received = self.sock.recv(n - len(data))
  215. if not len(received):
  216. raise socket.error('no data read from socket')
  217. data += received
  218. return data
  219. def contains_frame(data):
  220. """
  221. Read the frame length from the start of `data` and check if the data is
  222. long enough to contain the entire frame.
  223. """
  224. if len(data) < 2:
  225. return False
  226. b2 = struct.unpack('!B', data[1])[0]
  227. payload_len = b2 & 0x7F
  228. payload_start = 2
  229. if payload_len == 126:
  230. if len(data) > 4:
  231. payload_len = struct.unpack('!H', data[2:4])
  232. payload_start = 4
  233. elif payload_len == 127:
  234. if len(data) > 12:
  235. payload_len = struct.unpack('!Q', data[4:12])
  236. payload_start = 12
  237. return len(data) >= payload_len + payload_start
  238. def mask(key, original):
  239. """
  240. Mask an octet string using the given masking key.
  241. The following masking algorithm is used, as defined in RFC 6455:
  242. for each octet:
  243. j = i MOD 4
  244. transformed-octet-i = original-octet-i XOR masking-key-octet-j
  245. """
  246. if len(key) != 4:
  247. raise ValueError('invalid masking key "%s"' % key)
  248. key = map(ord, key)
  249. masked = bytearray(original)
  250. for i in xrange(len(masked)):
  251. masked[i] ^= key[i % 4]
  252. return masked
  253. def create_close_frame(code, reason):
  254. payload = '' if code is None else struct.pack('!H', code) + reason
  255. return ControlFrame(OPCODE_CLOSE, payload)