Exemplo n.º 1
0
    def test_info_entropy(self):
        route = self.make_one()

        response = route.info(self.request)
        entropy1 = protocol.decode(response.body)['entropy']

        response = route.info(self.request)
        entropy2 = protocol.decode(response.body)['entropy']

        self.assertFalse(entropy1 == entropy2)
Exemplo n.º 2
0
    def test_info(self):
        route = self.make_one()

        response = route.info(self.request)
        info = protocol.decode(response.body)

        self.assertTrue(info['websocket'])
        self.assertTrue(info['cookie_needed'])
Exemplo n.º 3
0
    def receive():
        while True:
            try:
                message = websocket.receive()
            except:
                session.close()
                break

            if session.state == STATE_CLOSING:
                try:
                    websocket.send(close_frame(3000, 'Go away!'))
                    websocket.close()
                except:
                    pass
                session.closed()
                break
            elif session.state == STATE_CLOSED:
                break

            if message == '':
                continue

            if message is None:
                session.close()
                session.closed()
                websocket.close()
                break

            try:
                if message.startswith('['):
                    message = message[1:-1]
                decoded_message = decode(message)
            except:
                try:
                    websocket.close(message='broken json')
                except:
                    pass
                session.close()
                session.closed()
                break

            if decoded_message:
                session.message(decoded_message)

        session.release()
Exemplo n.º 4
0
    def receive():
        while True:
            try:
                message = websocket.receive()
            except:
                session.close()
                break

            if session.state == STATE_CLOSING:
                try:
                    websocket.send(close_frame(3000, 'Go away!'))
                    websocket.close()
                except:
                    pass
                session.closed()
                break
            elif session.state == STATE_CLOSED:
                break

            if message == '':
                continue

            if message is None:
                session.close()
                session.closed()
                websocket.close()
                break

            try:
                if message.startswith('['):
                    message = message[1:-1]
                decoded_message = decode(message)
            except:
                try:
                    websocket.close(message='broken json')
                except:
                    pass
                session.close()
                session.closed()
                break

            if decoded_message:
                session.message(decoded_message)

        session.release()
Exemplo n.º 5
0
    def process(self, session, request):
        response = request.response
        response.content_type = 'text/plain; charset=UTF-8'
        response.headerlist.extend(cors_headers(request))

        data = request.body_file.read()
        if not data:
            return HTTPServerError("Payload expected.")

        try:
            messages = decode(data)
        except:
            return HTTPServerError("Broken JSON encoding.")

        for msg in messages:
            session.message(msg)

        response.status = 204
Exemplo n.º 6
0
    def process(self, session, request):
        response = request.response
        response.content_type = "text/plain; charset=UTF-8"
        response.headerlist.extend(cors_headers(request))
        response.headerlist.append(("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"))

        data = request.body_file.read()
        if not data:
            return HTTPServerError("Payload expected.")

        try:
            messages = decode(data)
        except:
            return HTTPServerError("Broken JSON encoding.")

        for msg in messages:
            session.message(msg)

        response.status = 204
Exemplo n.º 7
0
    def process(self, session, request):
        response = request.response
        response.content_type = 'text/plain; charset=UTF-8'
        response.headerlist.extend(cors_headers(request))
        response.headerlist.append(
            ('Cache-Control',
             'no-store, no-cache, must-revalidate, max-age=0'))

        data = request.body_file.read()
        if not data:
            return HTTPServerError("Payload expected.")

        try:
            messages = decode(data)
        except:
            return HTTPServerError("Broken JSON encoding.")

        for msg in messages:
            session.message(msg)

        response.status = 204
Exemplo n.º 8
0
def JSONPolling(session, request):
    meth = request.method
    response = request.response
    response.headers['Content-Type'] = 'application/javascript; charset=UTF-8'
    response.headerlist.append(
        ('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0'))
    session_cookie(request)

    if session.state == STATE_NEW:
        callback = request.GET.get('c', None)
        if callback is None:
            return HTTPServerError('"callback" parameter required')

        response.text = '%s("o");\r\n' % callback
        session.open()
        session.release()

    elif meth == "GET":
        callback = request.GET.get('c', None)
        if callback is None:
            return HTTPServerError('"callback" parameter required')

        if session.state in (STATE_CLOSING, STATE_CLOSED):
            response.text = "%s(%s);\r\n" % (
                callback, encode(close_frame(3000, 'Go away!')))
            if session.state == STATE_CLOSING:
                session.closed()
        else:
            messages = []
            try:
                messages.append(session.get_transport_message(timeout=timing))
                while True:
                    try:
                        messages.append(
                            session.get_transport_message(block=False))
                    except Empty:
                        break

            except Empty:
                messages = HEARTBEAT
                session.heartbeat()
            else:
                messages = message_frame(messages)

            response.text = "%s(%s);\r\n" % (callback, encode(messages))

        session.release()

    elif meth == "POST":
        data = request.body_file.read()

        ctype = request.headers.get('Content-Type', '').lower()
        if ctype == 'application/x-www-form-urlencoded':
            if not data.startswith('d='):
                return HTTPServerError("Payload expected.")

            data = unquote_plus(data[2:])

        if not data:
            return HTTPServerError("Payload expected.")

        try:
            messages = decode(data)
        except:
            return HTTPServerError("Broken JSON encoding.")

        for msg in messages:
            session.message(msg)

        response.status = 200
        response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
        response.body = 'ok'
    else:
        raise Exception("No support for such method: %s" % meth)

    return response
Exemplo n.º 9
0
    def test_decode(self):
        from pyramid_sockjs import protocol

        self.assertEqual(protocol.decode(json.dumps({})), {})
        self.assertEqual(protocol.decode(json.dumps(['test'])), ['test'])
        self.assertEqual(protocol.decode(json.dumps('"test"')), '"test"')
Exemplo n.º 10
0
    def test_decode(self):
        from pyramid_sockjs import protocol

        self.assertEqual(protocol.decode(json.dumps({})), {})
        self.assertEqual(protocol.decode(json.dumps(['test'])), ['test'])
        self.assertEqual(protocol.decode(json.dumps('"test"')), '"test"')
Exemplo n.º 11
0
def JSONPolling(session, request):
    meth = request.method
    response = request.response
    response.headers['Content-Type'] = 'application/javascript; charset=UTF-8'
    response.headerlist.append(
        ('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0'))
    session_cookie(request)

    if session.state == STATE_NEW:
        callback = request.GET.get('c', None)
        if callback is None:
            return HTTPServerError('"callback" parameter required')

        response.text = '%s("o");\r\n' % callback
        session.open()
        session.release()

    elif meth == "GET":
        callback = request.GET.get('c', None)
        if callback is None:
            return HTTPServerError('"callback" parameter required')

        if session.state in (STATE_CLOSING, STATE_CLOSED):
            response.text = "%s(%s);\r\n"%(
                callback, encode(close_frame(3000, 'Go away!')))
            if session.state == STATE_CLOSING:
                session.closed()
        else:
            messages = []
            try:
                messages.append(session.get_transport_message(timeout=timing))
                while True:
                    try:
                        messages.append(
                            session.get_transport_message(block=False))
                    except Empty:
                        break

            except Empty:
                messages = HEARTBEAT
                session.heartbeat()
            else:
                messages = message_frame(messages)

            response.text = "%s(%s);\r\n"%(callback, encode(messages))

        session.release()

    elif meth == "POST":
        data = request.body_file.read()

        ctype = request.headers.get('Content-Type', '').lower()
        if ctype == 'application/x-www-form-urlencoded':
            if not data.startswith('d='):
                return HTTPServerError("Payload expected.")

            data = unquote_plus(data[2:])

        if not data:
            return HTTPServerError("Payload expected.")

        try:
            messages = decode(data)
        except:
            return HTTPServerError("Broken JSON encoding.")

        for msg in messages:
            session.message(msg)

        response.status = 200
        response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
        response.body = 'ok'
    else:
        raise Exception("No support for such method: %s"%meth)

    return response