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-v3.patch

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

define a convenience send method that we can just re-use everywhere

  • 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..021973c 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)
     175
     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)
    178183
    179         #self.msg("Encoded: %s", repr(header + buf))
     184    def send_hybi(self, buf, opcode, base64=False, record=None):
     185        """ Send a HyBi style WebSocket frame. """
     186        header, buf = self.encode_hybi(buf, opcode, base64)
     187        if record:
     188            record.write("%s,\n" % repr("{%s{" % tdelta + encbufs[1]))
     189        self.request.send(header)
     190        self.request.send(buf)
    180191
    181         return header + buf, len(header), 0
    182192
    183193    @staticmethod
    184194    def decode_hybi(buf, base64=False, logger=None, strict=True):
    class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    310320
    311321        if bufs:
    312322            for buf in bufs:
    313                 if self.base64:
    314                     encbuf, lenhead, lentail = self.encode_hybi(buf, opcode=1, base64=True)
    315                 else:
    316                     encbuf, lenhead, lentail = self.encode_hybi(buf, opcode=2, base64=False)
    317 
    318                 if self.rec:
    319                     self.rec.write("%s,\n" %
    320                             repr("{%s{" % tdelta
    321                                 + encbuf[lenhead:len(encbuf)-lentail]))
    322 
    323                 self.send_parts.append(encbuf)
     323                opcode = 2-int(self.base64)     #based64: opcode=1, binary: opcode=2
     324                encbufs = self.send_hybi(buf, opcode, base64=self.base64, record=self.rec)
    324325
    325326        while self.send_parts:
    326327            # Send pending frames
    class WebSocketRequestHandler(SimpleHTTPRequestHandler): 
    412413        """ Send a WebSocket orderly close frame. """
    413414
    414415        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)
     416        self.send_hybi(msg, opcode=0x08)
    417417
    418418    def send_pong(self, data=''):
    419419        """ Send a WebSocket pong frame. """
    420         buf, h, t = self.encode_hybi(s2b(data), opcode=0x0A, base64=False)
    421         self.request.send(buf)
     420        self.send_hybi(s2b(data), opcode=0x0A)
    422421
    423422    def send_ping(self, data=''):
    424423        """ Send a WebSocket ping frame. """
    425         buf, h, t = self.encode_hybi(s2b(data), opcode=0x09, base64=False)
    426         self.request.send(buf)
     424        self.send_hybi(s2b(data), opcode=0x09)
    427425
    428426    def do_websocket_handshake(self):
    429427        h = self.headers