Beispiel #1
0
    def test_parse_hybi07_pong(self):
        """
        From HyBi-10, 4.7.
        """

        frame = six.b("\x8a\x05Hello")
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (PONG, six.b("Hello")))
        self.assertEqual(buf, six.b(""))
Beispiel #2
0
    def test_parse_hybi07_masked_text(self):
        """
        From HyBi-10, 4.7.
        """

        frame = six.b("\x81\x857\xfa!=\x7f\x9fMQX")
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (NORMAL, six.b("Hello")))
        self.assertEqual(buf, six.b(""))
Beispiel #3
0
    def test_parse_hybi07_unmasked_text(self):
        """
        From HyBi-10, 4.7.
        """

        frame = "\x81\x05Hello"
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (NORMAL, "Hello"))
        self.assertEqual(buf, "")
Beispiel #4
0
    def test_parse_hybi07_unmasked_text(self):
        """
        From HyBi-10, 4.7.
        """

        frame = "\x81\x05Hello"
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (NORMAL, "Hello"))
        self.assertEqual(buf, "")
Beispiel #5
0
    def test_parse_hybi07_ping(self):
        """
        From HyBi-10, 4.7.
        """

        frame = "\x89\x05Hello"
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (PING, "Hello"))
        self.assertEqual(buf, "")
Beispiel #6
0
    def test_parse_hybi07_ping(self):
        """
        From HyBi-10, 4.7.
        """

        frame = "\x89\x05Hello"
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (PING, "Hello"))
        self.assertEqual(buf, "")
Beispiel #7
0
    def test_parse_hybi07_close_empty(self):
        """
        A HyBi-07 close packet may have no body. In that case, it should use
        the generic error code 1000, and have no reason.
        """

        frame = six.b("\x88\x00")
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (CLOSE, (1000, six.b("No reason given"))))
        self.assertEqual(buf, six.b(""))
Beispiel #8
0
    def test_parse_hybi07_close_reason(self):
        """
        A HyBi-07 close packet must have its first two bytes be a numeric
        error code, and may optionally include trailing text explaining why
        the connection was closed.
        """

        frame = six.b("\x88\x0b\x03\xe8No reason")
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 1)
        self.assertEqual(frames[0], (CLOSE, (1000, six.b("No reason"))))
        self.assertEqual(buf, six.b(""))
Beispiel #9
0
    def test_parse_hybi07_unmasked_text_fragments(self):
        """
        We don't care about fragments. We are totally unfazed.

        From HyBi-10, 4.7.
        """

        frame = six.b("\x01\x03Hel\x80\x02lo")
        frames, buf = parse_hybi07_frames(frame)
        self.assertEqual(len(frames), 2)
        self.assertEqual(frames[0], (NORMAL, six.b("Hel")))
        self.assertEqual(frames[1], (NORMAL, six.b("lo")))
        self.assertEqual(buf, six.b(""))
Beispiel #10
0
    def dataReceived(self, data):
        """
        Parse data to read WebSocket frames.

        @param data: data received over the WebSocket connection.
        @type data: C{str}
        """
        if not data:
            return
        frames, buf_start = parse_hybi07_frames(data)
        for frame in frames:
            self.handler.frameReceived(frame[1])
        self._currentFrameLength = len(buf_start)
        self._data = buf_start
        return
        while True:
            endIndex = data.find("\xff")
            if endIndex != -1:
                self._currentFrameLength += endIndex
                if self._currentFrameLength > self.MAX_LENGTH:
                    self.handler.frameLengthExceeded()
                    break
                self._currentFrameLength = 0
                frame = "".join(self._data) + data[:endIndex]
                self._data[:] = []
                if frame[0] != "\x00":
                    self.request.transport.loseConnection()
                    break
                self.handler.frameReceived(frame[1:])
                data = data[endIndex + 1:]
                if not data:
                    break
                if data[0] != "\x00":
                    self.request.transport.loseConnection()
                    break
            else:
                self._currentFrameLength += len(data)
                if self._currentFrameLength > self.MAX_LENGTH + 1:
                    self.handler.frameLengthExceeded()
                else:
                    self._data.append(data)
                break
Beispiel #11
0
 def test_parse_hybi07_partial_no_length(self):
     frame = six.b("\x81")
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, six.b("\x81"))
Beispiel #12
0
 def test_parse_hybi07_partial_truncated_length_int(self):
     frame = "\x81\xfe"
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, "\x81\xfe")
Beispiel #13
0
 def test_parse_hybi07_partial_no_data(self):
     frame = "\x81\x05"
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, "\x81\x05")
Beispiel #14
0
 def test_parse_hybi07_partial_truncated_length_int(self):
     frame = "\x81\xfe"
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, "\x81\xfe")
Beispiel #15
0
 def test_parse_hybi07_partial_truncated_length_double(self):
     frame = six.b("\x81\xff")
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, six.b("\x81\xff"))
Beispiel #16
0
 def test_parse_hybi07_partial_truncated_data(self):
     frame = six.b("\x81\x05Hel")
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, six.b("\x81\x05Hel"))
Beispiel #17
0
 def test_parse_hybi07_partial_no_data(self):
     frame = "\x81\x05"
     frames, buf = parse_hybi07_frames(frame)
     self.assertFalse(frames)
     self.assertEqual(buf, "\x81\x05")