xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #1134: nojoin-v2.patch

File nojoin-v2.patch, 4.6 KB (added by Antoine Martin, 6 years ago)

split the header formatting into its own function

  • tests/test_websocket.py

    diff --git a/tests/test_websocket.py b/tests/test_websocket.py
    index 9d538db..58b38ef 100644
    a b class HyBiEncodeDecodeTestCase(unittest.TestCase): 
    409409
    410410    def test_encode_hybi_basic(self):
    411411        res = websocket.WebSocketRequestHandler.encode_hybi(b'Hello', 0x1)
    412         expected = (b'\x81\x05\x48\x65\x6c\x6c\x6f', 2, 0)
     412        expected_header = (b'\x81\x05')
     413        expected_data = (b'\x48\x65\x6c\x6c\x6f')
    413414
    414         self.assertEqual(res, expected)
     415        self.assertEqual(res[0], expected_header)
     416        self.assertEqual(res[1], expected_data)
    415417
    416418    def test_strict_mode_refuses_unmasked_client_frames(self):
    417419        buf = b'\x81\x05\x48\x65\x6c\x6c\x6f'
  • websockify/websocket.py

    diff --git a/websockify/websocket.py b/websockify/websocket.py
    index 16e1dc4..54c4708 100644
    a b class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    154154            return data.tostring()
    155155
    156156    @staticmethod
    157     def encode_hybi(buf, opcode, base64=False):
    158         """ Encode a HyBi style WebSocket frame.
     157    def encode_hybi_header(payload_len, opcode):
     158        """ Encode a HyBi style WebSocket frame header.
    159159        Optional opcode:
    160160            0x0 - continuation
    161161            0x1 - text frame (base64 encode buf)
    class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    164164            0x9 - ping
    165165            0xA - pong
    166166        """
    167         if base64:
    168             buf = b64encode(buf)
    169 
    170167        b1 = 0x80 | (opcode & 0x0f) # FIN + opcode
    171         payload_len = len(buf)
    172168        if payload_len <= 125:
    173             header = pack('>BB', b1, payload_len)
     169            return pack('>BB', b1, payload_len)
    174170        elif payload_len > 125 and payload_len < 65536:
    175             header = pack('>BBH', b1, 126, payload_len)
    176         elif payload_len >= 65536:
    177             header = pack('>BBQ', b1, 127, payload_len)
     171            return pack('>BBH', b1, 126, payload_len)
     172        else:
     173            assert payload_len >= 65536
     174            return pack('>BBQ', b1, 127, payload_len)
    178175
    179         #self.msg("Encoded: %s", repr(header + buf))
     176    @staticmethod
     177    def encode_hybi(buf, opcode, base64=False):
     178        """ Encode a HyBi style WebSocket frame. """
     179        if base64:
     180            buf = b64encode(buf)
     181        header = WebSocketRequestHandler.encode_hybi_header(len(buf), opcode)
     182        return [header, buf]
    180183
    181         return header + buf, len(header), 0
    182184
    183185    @staticmethod
    184186    def decode_hybi(buf, base64=False, logger=None, strict=True):
    class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    311313        if bufs:
    312314            for buf in bufs:
    313315                if self.base64:
    314                     encbuf, lenhead, lentail = self.encode_hybi(buf, opcode=1, base64=True)
     316                    encbufs = self.encode_hybi(buf, opcode=1, base64=True)
    315317                else:
    316                     encbuf, lenhead, lentail = self.encode_hybi(buf, opcode=2, base64=False)
     318                    encbufs = self.encode_hybi(buf, opcode=2, base64=False)
    317319
    318320                if self.rec:
    319321                    self.rec.write("%s,\n" %
    320                             repr("{%s{" % tdelta
    321                                 + encbuf[lenhead:len(encbuf)-lentail]))
     322                            repr("{%s{" % tdelta + encbufs[1]))
    322323
    323                 self.send_parts.append(encbuf)
     324                for encbuf in encbufs:
     325                    self.send_parts.append(encbuf)
    324326
    325327        while self.send_parts:
    326328            # Send pending frames
    class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    412414        """ Send a WebSocket orderly close frame. """
    413415
    414416        msg = pack(">H%ds" % len(reason), code, s2b(reason))
    415         buf, h, t = self.encode_hybi(msg, opcode=0x08, base64=False)
    416         self.request.send(buf)
     417        bufs = self.encode_hybi(msg, opcode=0x08, base64=False)
     418        for buf in bufs:
     419            self.request.send(buf)
    417420
    418421    def send_pong(self, data=''):
    419422        """ Send a WebSocket pong frame. """
    420         buf, h, t = self.encode_hybi(s2b(data), opcode=0x0A, base64=False)
    421         self.request.send(buf)
     423        bufs = self.encode_hybi(s2b(data), opcode=0x0A, base64=False)
     424        for buf in bufs:
     425            self.request.send(buf)
    422426
    423427    def send_ping(self, data=''):
    424428        """ Send a WebSocket ping frame. """
    425         buf, h, t = self.encode_hybi(s2b(data), opcode=0x09, base64=False)
    426         self.request.send(buf)
     429        bufs = self.encode_hybi(s2b(data), opcode=0x09, base64=False)
     430        for buf in bufs:
     431            self.request.send(buf)
    427432
    428433    def do_websocket_handshake(self):
    429434        h = self.headers