frame.py 8.5 KB

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