_stream_hybi.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. # Copyright 2012, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """This file provides classes and helper functions for parsing/building frames
  30. of the WebSocket protocol (RFC 6455).
  31. Specification:
  32. http://tools.ietf.org/html/rfc6455
  33. """
  34. from collections import deque
  35. import logging
  36. import os
  37. import struct
  38. import time
  39. from mod_pywebsocket import common
  40. from mod_pywebsocket import util
  41. from mod_pywebsocket._stream_base import BadOperationException
  42. from mod_pywebsocket._stream_base import ConnectionTerminatedException
  43. from mod_pywebsocket._stream_base import InvalidFrameException
  44. from mod_pywebsocket._stream_base import InvalidUTF8Exception
  45. from mod_pywebsocket._stream_base import StreamBase
  46. from mod_pywebsocket._stream_base import UnsupportedFrameException
  47. _NOOP_MASKER = util.NoopMasker()
  48. class Frame(object):
  49. def __init__(self, fin=1, rsv1=0, rsv2=0, rsv3=0,
  50. opcode=None, payload=''):
  51. self.fin = fin
  52. self.rsv1 = rsv1
  53. self.rsv2 = rsv2
  54. self.rsv3 = rsv3
  55. self.opcode = opcode
  56. self.payload = payload
  57. # Helper functions made public to be used for writing unittests for WebSocket
  58. # clients.
  59. def create_length_header(length, mask):
  60. """Creates a length header.
  61. Args:
  62. length: Frame length. Must be less than 2^63.
  63. mask: Mask bit. Must be boolean.
  64. Raises:
  65. ValueError: when bad data is given.
  66. """
  67. if mask:
  68. mask_bit = 1 << 7
  69. else:
  70. mask_bit = 0
  71. if length < 0:
  72. raise ValueError('length must be non negative integer')
  73. elif length <= 125:
  74. return chr(mask_bit | length)
  75. elif length < (1 << 16):
  76. return chr(mask_bit | 126) + struct.pack('!H', length)
  77. elif length < (1 << 63):
  78. return chr(mask_bit | 127) + struct.pack('!Q', length)
  79. else:
  80. raise ValueError('Payload is too big for one frame')
  81. def create_header(opcode, payload_length, fin, rsv1, rsv2, rsv3, mask):
  82. """Creates a frame header.
  83. Raises:
  84. Exception: when bad data is given.
  85. """
  86. if opcode < 0 or 0xf < opcode:
  87. raise ValueError('Opcode out of range')
  88. if payload_length < 0 or (1 << 63) <= payload_length:
  89. raise ValueError('payload_length out of range')
  90. if (fin | rsv1 | rsv2 | rsv3) & ~1:
  91. raise ValueError('FIN bit and Reserved bit parameter must be 0 or 1')
  92. header = ''
  93. first_byte = ((fin << 7)
  94. | (rsv1 << 6) | (rsv2 << 5) | (rsv3 << 4)
  95. | opcode)
  96. header += chr(first_byte)
  97. header += create_length_header(payload_length, mask)
  98. return header
  99. def _build_frame(header, body, mask):
  100. if not mask:
  101. return header + body
  102. masking_nonce = os.urandom(4)
  103. masker = util.RepeatedXorMasker(masking_nonce)
  104. return header + masking_nonce + masker.mask(body)
  105. def _filter_and_format_frame_object(frame, mask, frame_filters):
  106. for frame_filter in frame_filters:
  107. frame_filter.filter(frame)
  108. header = create_header(
  109. frame.opcode, len(frame.payload), frame.fin,
  110. frame.rsv1, frame.rsv2, frame.rsv3, mask)
  111. return _build_frame(header, frame.payload, mask)
  112. def create_binary_frame(
  113. message, opcode=common.OPCODE_BINARY, fin=1, mask=False, frame_filters=[]):
  114. """Creates a simple binary frame with no extension, reserved bit."""
  115. frame = Frame(fin=fin, opcode=opcode, payload=message)
  116. return _filter_and_format_frame_object(frame, mask, frame_filters)
  117. def create_text_frame(
  118. message, opcode=common.OPCODE_TEXT, fin=1, mask=False, frame_filters=[]):
  119. """Creates a simple text frame with no extension, reserved bit."""
  120. encoded_message = message.encode('utf-8')
  121. return create_binary_frame(encoded_message, opcode, fin, mask,
  122. frame_filters)
  123. def parse_frame(receive_bytes, logger=None,
  124. ws_version=common.VERSION_HYBI_LATEST,
  125. unmask_receive=True):
  126. """Parses a frame. Returns a tuple containing each header field and
  127. payload.
  128. Args:
  129. receive_bytes: a function that reads frame data from a stream or
  130. something similar. The function takes length of the bytes to be
  131. read. The function must raise ConnectionTerminatedException if
  132. there is not enough data to be read.
  133. logger: a logging object.
  134. ws_version: the version of WebSocket protocol.
  135. unmask_receive: unmask received frames. When received unmasked
  136. frame, raises InvalidFrameException.
  137. Raises:
  138. ConnectionTerminatedException: when receive_bytes raises it.
  139. InvalidFrameException: when the frame contains invalid data.
  140. """
  141. if not logger:
  142. logger = logging.getLogger()
  143. logger.log(common.LOGLEVEL_FINE, 'Receive the first 2 octets of a frame')
  144. received = receive_bytes(2)
  145. first_byte = ord(received[0])
  146. fin = (first_byte >> 7) & 1
  147. rsv1 = (first_byte >> 6) & 1
  148. rsv2 = (first_byte >> 5) & 1
  149. rsv3 = (first_byte >> 4) & 1
  150. opcode = first_byte & 0xf
  151. second_byte = ord(received[1])
  152. mask = (second_byte >> 7) & 1
  153. payload_length = second_byte & 0x7f
  154. logger.log(common.LOGLEVEL_FINE,
  155. 'FIN=%s, RSV1=%s, RSV2=%s, RSV3=%s, opcode=%s, '
  156. 'Mask=%s, Payload_length=%s',
  157. fin, rsv1, rsv2, rsv3, opcode, mask, payload_length)
  158. if (mask == 1) != unmask_receive:
  159. raise InvalidFrameException(
  160. 'Mask bit on the received frame did\'nt match masking '
  161. 'configuration for received frames')
  162. # The HyBi and later specs disallow putting a value in 0x0-0xFFFF
  163. # into the 8-octet extended payload length field (or 0x0-0xFD in
  164. # 2-octet field).
  165. valid_length_encoding = True
  166. length_encoding_bytes = 1
  167. if payload_length == 127:
  168. logger.log(common.LOGLEVEL_FINE,
  169. 'Receive 8-octet extended payload length')
  170. extended_payload_length = receive_bytes(8)
  171. payload_length = struct.unpack(
  172. '!Q', extended_payload_length)[0]
  173. if payload_length > 0x7FFFFFFFFFFFFFFF:
  174. raise InvalidFrameException(
  175. 'Extended payload length >= 2^63')
  176. if ws_version >= 13 and payload_length < 0x10000:
  177. valid_length_encoding = False
  178. length_encoding_bytes = 8
  179. logger.log(common.LOGLEVEL_FINE,
  180. 'Decoded_payload_length=%s', payload_length)
  181. elif payload_length == 126:
  182. logger.log(common.LOGLEVEL_FINE,
  183. 'Receive 2-octet extended payload length')
  184. extended_payload_length = receive_bytes(2)
  185. payload_length = struct.unpack(
  186. '!H', extended_payload_length)[0]
  187. if ws_version >= 13 and payload_length < 126:
  188. valid_length_encoding = False
  189. length_encoding_bytes = 2
  190. logger.log(common.LOGLEVEL_FINE,
  191. 'Decoded_payload_length=%s', payload_length)
  192. if not valid_length_encoding:
  193. logger.warning(
  194. 'Payload length is not encoded using the minimal number of '
  195. 'bytes (%d is encoded using %d bytes)',
  196. payload_length,
  197. length_encoding_bytes)
  198. if mask == 1:
  199. logger.log(common.LOGLEVEL_FINE, 'Receive mask')
  200. masking_nonce = receive_bytes(4)
  201. masker = util.RepeatedXorMasker(masking_nonce)
  202. logger.log(common.LOGLEVEL_FINE, 'Mask=%r', masking_nonce)
  203. else:
  204. masker = _NOOP_MASKER
  205. logger.log(common.LOGLEVEL_FINE, 'Receive payload data')
  206. if logger.isEnabledFor(common.LOGLEVEL_FINE):
  207. receive_start = time.time()
  208. raw_payload_bytes = receive_bytes(payload_length)
  209. if logger.isEnabledFor(common.LOGLEVEL_FINE):
  210. logger.log(
  211. common.LOGLEVEL_FINE,
  212. 'Done receiving payload data at %s MB/s',
  213. payload_length / (time.time() - receive_start) / 1000 / 1000)
  214. logger.log(common.LOGLEVEL_FINE, 'Unmask payload data')
  215. if logger.isEnabledFor(common.LOGLEVEL_FINE):
  216. unmask_start = time.time()
  217. unmasked_bytes = masker.mask(raw_payload_bytes)
  218. if logger.isEnabledFor(common.LOGLEVEL_FINE):
  219. logger.log(
  220. common.LOGLEVEL_FINE,
  221. 'Done unmasking payload data at %s MB/s',
  222. payload_length / (time.time() - unmask_start) / 1000 / 1000)
  223. return opcode, unmasked_bytes, fin, rsv1, rsv2, rsv3
  224. class FragmentedFrameBuilder(object):
  225. """A stateful class to send a message as fragments."""
  226. def __init__(self, mask, frame_filters=[], encode_utf8=True):
  227. """Constructs an instance."""
  228. self._mask = mask
  229. self._frame_filters = frame_filters
  230. # This is for skipping UTF-8 encoding when building text type frames
  231. # from compressed data.
  232. self._encode_utf8 = encode_utf8
  233. self._started = False
  234. # Hold opcode of the first frame in messages to verify types of other
  235. # frames in the message are all the same.
  236. self._opcode = common.OPCODE_TEXT
  237. def build(self, payload_data, end, binary):
  238. if binary:
  239. frame_type = common.OPCODE_BINARY
  240. else:
  241. frame_type = common.OPCODE_TEXT
  242. if self._started:
  243. if self._opcode != frame_type:
  244. raise ValueError('Message types are different in frames for '
  245. 'the same message')
  246. opcode = common.OPCODE_CONTINUATION
  247. else:
  248. opcode = frame_type
  249. self._opcode = frame_type
  250. if end:
  251. self._started = False
  252. fin = 1
  253. else:
  254. self._started = True
  255. fin = 0
  256. if binary or not self._encode_utf8:
  257. return create_binary_frame(
  258. payload_data, opcode, fin, self._mask, self._frame_filters)
  259. else:
  260. return create_text_frame(
  261. payload_data, opcode, fin, self._mask, self._frame_filters)
  262. def _create_control_frame(opcode, body, mask, frame_filters):
  263. frame = Frame(opcode=opcode, payload=body)
  264. for frame_filter in frame_filters:
  265. frame_filter.filter(frame)
  266. if len(frame.payload) > 125:
  267. raise BadOperationException(
  268. 'Payload data size of control frames must be 125 bytes or less')
  269. header = create_header(
  270. frame.opcode, len(frame.payload), frame.fin,
  271. frame.rsv1, frame.rsv2, frame.rsv3, mask)
  272. return _build_frame(header, frame.payload, mask)
  273. def create_ping_frame(body, mask=False, frame_filters=[]):
  274. return _create_control_frame(common.OPCODE_PING, body, mask, frame_filters)
  275. def create_pong_frame(body, mask=False, frame_filters=[]):
  276. return _create_control_frame(common.OPCODE_PONG, body, mask, frame_filters)
  277. def create_close_frame(body, mask=False, frame_filters=[]):
  278. return _create_control_frame(
  279. common.OPCODE_CLOSE, body, mask, frame_filters)
  280. def create_closing_handshake_body(code, reason):
  281. body = ''
  282. if code is not None:
  283. if (code > common.STATUS_USER_PRIVATE_MAX or
  284. code < common.STATUS_NORMAL_CLOSURE):
  285. raise BadOperationException('Status code is out of range')
  286. if (code == common.STATUS_NO_STATUS_RECEIVED or
  287. code == common.STATUS_ABNORMAL_CLOSURE or
  288. code == common.STATUS_TLS_HANDSHAKE):
  289. raise BadOperationException('Status code is reserved pseudo '
  290. 'code')
  291. encoded_reason = reason.encode('utf-8')
  292. body = struct.pack('!H', code) + encoded_reason
  293. return body
  294. class StreamOptions(object):
  295. """Holds option values to configure Stream objects."""
  296. def __init__(self):
  297. """Constructs StreamOptions."""
  298. # Filters applied to frames.
  299. self.outgoing_frame_filters = []
  300. self.incoming_frame_filters = []
  301. # Filters applied to messages. Control frames are not affected by them.
  302. self.outgoing_message_filters = []
  303. self.incoming_message_filters = []
  304. self.encode_text_message_to_utf8 = True
  305. self.mask_send = False
  306. self.unmask_receive = True
  307. class Stream(StreamBase):
  308. """A class for parsing/building frames of the WebSocket protocol
  309. (RFC 6455).
  310. """
  311. def __init__(self, request, options):
  312. """Constructs an instance.
  313. Args:
  314. request: mod_python request.
  315. """
  316. StreamBase.__init__(self, request)
  317. self._logger = util.get_class_logger(self)
  318. self._options = options
  319. self._request.client_terminated = False
  320. self._request.server_terminated = False
  321. # Holds body of received fragments.
  322. self._received_fragments = []
  323. # Holds the opcode of the first fragment.
  324. self._original_opcode = None
  325. self._writer = FragmentedFrameBuilder(
  326. self._options.mask_send, self._options.outgoing_frame_filters,
  327. self._options.encode_text_message_to_utf8)
  328. self._ping_queue = deque()
  329. def _receive_frame(self):
  330. """Receives a frame and return data in the frame as a tuple containing
  331. each header field and payload separately.
  332. Raises:
  333. ConnectionTerminatedException: when read returns empty
  334. string.
  335. InvalidFrameException: when the frame contains invalid data.
  336. """
  337. def _receive_bytes(length):
  338. return self.receive_bytes(length)
  339. return parse_frame(receive_bytes=_receive_bytes,
  340. logger=self._logger,
  341. ws_version=self._request.ws_version,
  342. unmask_receive=self._options.unmask_receive)
  343. def _receive_frame_as_frame_object(self):
  344. opcode, unmasked_bytes, fin, rsv1, rsv2, rsv3 = self._receive_frame()
  345. return Frame(fin=fin, rsv1=rsv1, rsv2=rsv2, rsv3=rsv3,
  346. opcode=opcode, payload=unmasked_bytes)
  347. def receive_filtered_frame(self):
  348. """Receives a frame and applies frame filters and message filters.
  349. The frame to be received must satisfy following conditions:
  350. - The frame is not fragmented.
  351. - The opcode of the frame is TEXT or BINARY.
  352. DO NOT USE this method except for testing purpose.
  353. """
  354. frame = self._receive_frame_as_frame_object()
  355. if not frame.fin:
  356. raise InvalidFrameException(
  357. 'Segmented frames must not be received via '
  358. 'receive_filtered_frame()')
  359. if (frame.opcode != common.OPCODE_TEXT and
  360. frame.opcode != common.OPCODE_BINARY):
  361. raise InvalidFrameException(
  362. 'Control frames must not be received via '
  363. 'receive_filtered_frame()')
  364. for frame_filter in self._options.incoming_frame_filters:
  365. frame_filter.filter(frame)
  366. for message_filter in self._options.incoming_message_filters:
  367. frame.payload = message_filter.filter(frame.payload)
  368. return frame
  369. def send_message(self, message, end=True, binary=False):
  370. """Send message.
  371. Args:
  372. message: text in unicode or binary in str to send.
  373. binary: send message as binary frame.
  374. Raises:
  375. BadOperationException: when called on a server-terminated
  376. connection or called with inconsistent message type or
  377. binary parameter.
  378. """
  379. if self._request.server_terminated:
  380. raise BadOperationException(
  381. 'Requested send_message after sending out a closing handshake')
  382. if binary and isinstance(message, unicode):
  383. raise BadOperationException(
  384. 'Message for binary frame must be instance of str')
  385. for message_filter in self._options.outgoing_message_filters:
  386. message = message_filter.filter(message, end, binary)
  387. try:
  388. # Set this to any positive integer to limit maximum size of data in
  389. # payload data of each frame.
  390. MAX_PAYLOAD_DATA_SIZE = -1
  391. if MAX_PAYLOAD_DATA_SIZE <= 0:
  392. self._write(self._writer.build(message, end, binary))
  393. return
  394. bytes_written = 0
  395. while True:
  396. end_for_this_frame = end
  397. bytes_to_write = len(message) - bytes_written
  398. if (MAX_PAYLOAD_DATA_SIZE > 0 and
  399. bytes_to_write > MAX_PAYLOAD_DATA_SIZE):
  400. end_for_this_frame = False
  401. bytes_to_write = MAX_PAYLOAD_DATA_SIZE
  402. frame = self._writer.build(
  403. message[bytes_written:bytes_written + bytes_to_write],
  404. end_for_this_frame,
  405. binary)
  406. self._write(frame)
  407. bytes_written += bytes_to_write
  408. # This if must be placed here (the end of while block) so that
  409. # at least one frame is sent.
  410. if len(message) <= bytes_written:
  411. break
  412. except ValueError, e:
  413. raise BadOperationException(e)
  414. def _get_message_from_frame(self, frame):
  415. """Gets a message from frame. If the message is composed of fragmented
  416. frames and the frame is not the last fragmented frame, this method
  417. returns None. The whole message will be returned when the last
  418. fragmented frame is passed to this method.
  419. Raises:
  420. InvalidFrameException: when the frame doesn't match defragmentation
  421. context, or the frame contains invalid data.
  422. """
  423. if frame.opcode == common.OPCODE_CONTINUATION:
  424. if not self._received_fragments:
  425. if frame.fin:
  426. raise InvalidFrameException(
  427. 'Received a termination frame but fragmentation '
  428. 'not started')
  429. else:
  430. raise InvalidFrameException(
  431. 'Received an intermediate frame but '
  432. 'fragmentation not started')
  433. if frame.fin:
  434. # End of fragmentation frame
  435. self._received_fragments.append(frame.payload)
  436. message = ''.join(self._received_fragments)
  437. self._received_fragments = []
  438. return message
  439. else:
  440. # Intermediate frame
  441. self._received_fragments.append(frame.payload)
  442. return None
  443. else:
  444. if self._received_fragments:
  445. if frame.fin:
  446. raise InvalidFrameException(
  447. 'Received an unfragmented frame without '
  448. 'terminating existing fragmentation')
  449. else:
  450. raise InvalidFrameException(
  451. 'New fragmentation started without terminating '
  452. 'existing fragmentation')
  453. if frame.fin:
  454. # Unfragmented frame
  455. self._original_opcode = frame.opcode
  456. return frame.payload
  457. else:
  458. # Start of fragmentation frame
  459. if common.is_control_opcode(frame.opcode):
  460. raise InvalidFrameException(
  461. 'Control frames must not be fragmented')
  462. self._original_opcode = frame.opcode
  463. self._received_fragments.append(frame.payload)
  464. return None
  465. def _process_close_message(self, message):
  466. """Processes close message.
  467. Args:
  468. message: close message.
  469. Raises:
  470. InvalidFrameException: when the message is invalid.
  471. """
  472. self._request.client_terminated = True
  473. # Status code is optional. We can have status reason only if we
  474. # have status code. Status reason can be empty string. So,
  475. # allowed cases are
  476. # - no application data: no code no reason
  477. # - 2 octet of application data: has code but no reason
  478. # - 3 or more octet of application data: both code and reason
  479. if len(message) == 0:
  480. self._logger.debug('Received close frame (empty body)')
  481. self._request.ws_close_code = (
  482. common.STATUS_NO_STATUS_RECEIVED)
  483. elif len(message) == 1:
  484. raise InvalidFrameException(
  485. 'If a close frame has status code, the length of '
  486. 'status code must be 2 octet')
  487. elif len(message) >= 2:
  488. self._request.ws_close_code = struct.unpack(
  489. '!H', message[0:2])[0]
  490. self._request.ws_close_reason = message[2:].decode(
  491. 'utf-8', 'replace')
  492. self._logger.debug(
  493. 'Received close frame (code=%d, reason=%r)',
  494. self._request.ws_close_code,
  495. self._request.ws_close_reason)
  496. # As we've received a close frame, no more data is coming over the
  497. # socket. We can now safely close the socket without worrying about
  498. # RST sending.
  499. if self._request.server_terminated:
  500. self._logger.debug(
  501. 'Received ack for server-initiated closing handshake')
  502. return
  503. self._logger.debug(
  504. 'Received client-initiated closing handshake')
  505. code = common.STATUS_NORMAL_CLOSURE
  506. reason = ''
  507. if hasattr(self._request, '_dispatcher'):
  508. dispatcher = self._request._dispatcher
  509. code, reason = dispatcher.passive_closing_handshake(
  510. self._request)
  511. if code is None and reason is not None and len(reason) > 0:
  512. self._logger.warning(
  513. 'Handler specified reason despite code being None')
  514. reason = ''
  515. if reason is None:
  516. reason = ''
  517. self._send_closing_handshake(code, reason)
  518. self._logger.debug(
  519. 'Acknowledged closing handshake initiated by the peer '
  520. '(code=%r, reason=%r)', code, reason)
  521. def _process_ping_message(self, message):
  522. """Processes ping message.
  523. Args:
  524. message: ping message.
  525. """
  526. try:
  527. handler = self._request.on_ping_handler
  528. if handler:
  529. handler(self._request, message)
  530. return
  531. except AttributeError, e:
  532. pass
  533. self._send_pong(message)
  534. def _process_pong_message(self, message):
  535. """Processes pong message.
  536. Args:
  537. message: pong message.
  538. """
  539. # TODO(tyoshino): Add ping timeout handling.
  540. inflight_pings = deque()
  541. while True:
  542. try:
  543. expected_body = self._ping_queue.popleft()
  544. if expected_body == message:
  545. # inflight_pings contains pings ignored by the
  546. # other peer. Just forget them.
  547. self._logger.debug(
  548. 'Ping %r is acked (%d pings were ignored)',
  549. expected_body, len(inflight_pings))
  550. break
  551. else:
  552. inflight_pings.append(expected_body)
  553. except IndexError, e:
  554. # The received pong was unsolicited pong. Keep the
  555. # ping queue as is.
  556. self._ping_queue = inflight_pings
  557. self._logger.debug('Received a unsolicited pong')
  558. break
  559. try:
  560. handler = self._request.on_pong_handler
  561. if handler:
  562. handler(self._request, message)
  563. except AttributeError, e:
  564. pass
  565. def receive_message(self):
  566. """Receive a WebSocket frame and return its payload as a text in
  567. unicode or a binary in str.
  568. Returns:
  569. payload data of the frame
  570. - as unicode instance if received text frame
  571. - as str instance if received binary frame
  572. or None iff received closing handshake.
  573. Raises:
  574. BadOperationException: when called on a client-terminated
  575. connection.
  576. ConnectionTerminatedException: when read returns empty
  577. string.
  578. InvalidFrameException: when the frame contains invalid
  579. data.
  580. UnsupportedFrameException: when the received frame has
  581. flags, opcode we cannot handle. You can ignore this
  582. exception and continue receiving the next frame.
  583. """
  584. if self._request.client_terminated:
  585. raise BadOperationException(
  586. 'Requested receive_message after receiving a closing '
  587. 'handshake')
  588. while True:
  589. # mp_conn.read will block if no bytes are available.
  590. # Timeout is controlled by TimeOut directive of Apache.
  591. frame = self._receive_frame_as_frame_object()
  592. # Check the constraint on the payload size for control frames
  593. # before extension processes the frame.
  594. # See also http://tools.ietf.org/html/rfc6455#section-5.5
  595. if (common.is_control_opcode(frame.opcode) and
  596. len(frame.payload) > 125):
  597. raise InvalidFrameException(
  598. 'Payload data size of control frames must be 125 bytes or '
  599. 'less')
  600. for frame_filter in self._options.incoming_frame_filters:
  601. frame_filter.filter(frame)
  602. if frame.rsv1 or frame.rsv2 or frame.rsv3:
  603. raise UnsupportedFrameException(
  604. 'Unsupported flag is set (rsv = %d%d%d)' %
  605. (frame.rsv1, frame.rsv2, frame.rsv3))
  606. message = self._get_message_from_frame(frame)
  607. if message is None:
  608. continue
  609. for message_filter in self._options.incoming_message_filters:
  610. message = message_filter.filter(message)
  611. if self._original_opcode == common.OPCODE_TEXT:
  612. # The WebSocket protocol section 4.4 specifies that invalid
  613. # characters must be replaced with U+fffd REPLACEMENT
  614. # CHARACTER.
  615. try:
  616. return message.decode('utf-8')
  617. except UnicodeDecodeError, e:
  618. raise InvalidUTF8Exception(e)
  619. elif self._original_opcode == common.OPCODE_BINARY:
  620. return message
  621. elif self._original_opcode == common.OPCODE_CLOSE:
  622. self._process_close_message(message)
  623. return None
  624. elif self._original_opcode == common.OPCODE_PING:
  625. self._process_ping_message(message)
  626. elif self._original_opcode == common.OPCODE_PONG:
  627. self._process_pong_message(message)
  628. else:
  629. raise UnsupportedFrameException(
  630. 'Opcode %d is not supported' % self._original_opcode)
  631. def _send_closing_handshake(self, code, reason):
  632. body = create_closing_handshake_body(code, reason)
  633. frame = create_close_frame(
  634. body, mask=self._options.mask_send,
  635. frame_filters=self._options.outgoing_frame_filters)
  636. self._request.server_terminated = True
  637. self._write(frame)
  638. def close_connection(self, code=common.STATUS_NORMAL_CLOSURE, reason='',
  639. wait_response=True):
  640. """Closes a WebSocket connection.
  641. Args:
  642. code: Status code for close frame. If code is None, a close
  643. frame with empty body will be sent.
  644. reason: string representing close reason.
  645. wait_response: True when caller want to wait the response.
  646. Raises:
  647. BadOperationException: when reason is specified with code None
  648. or reason is not an instance of both str and unicode.
  649. """
  650. if self._request.server_terminated:
  651. self._logger.debug(
  652. 'Requested close_connection but server is already terminated')
  653. return
  654. if code is None:
  655. if reason is not None and len(reason) > 0:
  656. raise BadOperationException(
  657. 'close reason must not be specified if code is None')
  658. reason = ''
  659. else:
  660. if not isinstance(reason, str) and not isinstance(reason, unicode):
  661. raise BadOperationException(
  662. 'close reason must be an instance of str or unicode')
  663. self._send_closing_handshake(code, reason)
  664. self._logger.debug(
  665. 'Initiated closing handshake (code=%r, reason=%r)',
  666. code, reason)
  667. if (code == common.STATUS_GOING_AWAY or
  668. code == common.STATUS_PROTOCOL_ERROR) or not wait_response:
  669. # It doesn't make sense to wait for a close frame if the reason is
  670. # protocol error or that the server is going away. For some of
  671. # other reasons, it might not make sense to wait for a close frame,
  672. # but it's not clear, yet.
  673. return
  674. # TODO(ukai): 2. wait until the /client terminated/ flag has been set,
  675. # or until a server-defined timeout expires.
  676. #
  677. # For now, we expect receiving closing handshake right after sending
  678. # out closing handshake.
  679. message = self.receive_message()
  680. if message is not None:
  681. raise ConnectionTerminatedException(
  682. 'Didn\'t receive valid ack for closing handshake')
  683. # TODO: 3. close the WebSocket connection.
  684. # note: mod_python Connection (mp_conn) doesn't have close method.
  685. def send_ping(self, body=''):
  686. frame = create_ping_frame(
  687. body,
  688. self._options.mask_send,
  689. self._options.outgoing_frame_filters)
  690. self._write(frame)
  691. self._ping_queue.append(body)
  692. def _send_pong(self, body):
  693. frame = create_pong_frame(
  694. body,
  695. self._options.mask_send,
  696. self._options.outgoing_frame_filters)
  697. self._write(frame)
  698. def get_last_received_opcode(self):
  699. """Returns the opcode of the WebSocket message which the last received
  700. frame belongs to. The return value is valid iff immediately after
  701. receive_message call.
  702. """
  703. return self._original_opcode
  704. # vi:sts=4 sw=4 et