Parse incoming data into frames. :meth:`receive_data` and :meth:`receive_eof` run this generator coroutine until it needs more data or reaches EOF. :meth:`parse` never raises an exception. Instead, it sets the :attr:`parser_exc` and yields control.
(self)
| 560 | # Private methods for receiving data. |
| 561 | |
| 562 | def parse(self) -> Generator[None]: |
| 563 | """ |
| 564 | Parse incoming data into frames. |
| 565 | |
| 566 | :meth:`receive_data` and :meth:`receive_eof` run this generator |
| 567 | coroutine until it needs more data or reaches EOF. |
| 568 | |
| 569 | :meth:`parse` never raises an exception. Instead, it sets the |
| 570 | :attr:`parser_exc` and yields control. |
| 571 | |
| 572 | """ |
| 573 | try: |
| 574 | while True: |
| 575 | if (yield from self.reader.at_eof()): |
| 576 | if self.debug: |
| 577 | self.logger.debug("< EOF") |
| 578 | # If the WebSocket connection is closed cleanly, with a |
| 579 | # closing handhshake, recv_frame() substitutes parse() |
| 580 | # with discard(). This branch is reached only when the |
| 581 | # connection isn't closed cleanly. |
| 582 | raise EOFError("unexpected end of stream") |
| 583 | |
| 584 | max_size = None |
| 585 | |
| 586 | if self.max_message_size is not None: |
| 587 | if self.current_size is None: |
| 588 | max_size = self.max_message_size |
| 589 | else: |
| 590 | max_size = self.max_message_size - self.current_size |
| 591 | |
| 592 | if self.max_fragment_size is not None: |
| 593 | if max_size is None: |
| 594 | max_size = self.max_fragment_size |
| 595 | else: |
| 596 | max_size = min(max_size, self.max_fragment_size) |
| 597 | |
| 598 | # During a normal closure, execution ends here on the next |
| 599 | # iteration of the loop after receiving a close frame. At |
| 600 | # this point, recv_frame() replaced parse() by discard(). |
| 601 | frame = yield from Frame.parse( |
| 602 | self.reader.read_exact, |
| 603 | mask=self.side is SERVER, |
| 604 | max_size=max_size, |
| 605 | extensions=self.extensions, |
| 606 | ) |
| 607 | |
| 608 | if self.debug: |
| 609 | self.logger.debug("< %s", frame) |
| 610 | |
| 611 | self.recv_frame(frame) |
| 612 | |
| 613 | except ProtocolError as exc: |
| 614 | self.fail(CloseCode.PROTOCOL_ERROR, str(exc)) |
| 615 | self.parser_exc = exc |
| 616 | |
| 617 | except EOFError as exc: |
| 618 | self.fail(CloseCode.ABNORMAL_CLOSURE, str(exc)) |
| 619 | self.parser_exc = exc |
no test coverage detected