Example #1
0
    def test_websocket_upgrade(self):
        from pyramid_sockjs.websocket import init_websocket
        request = DummyRequest()

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Can "Upgrade" only to "WebSocket".')

        request.environ['HTTP_UPGRADE'] = 'ssl'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Can "Upgrade" only to "WebSocket".')
    def test_websocket_upgrade(self):
        from pyramid_sockjs.websocket import init_websocket
        request = DummyRequest()

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Can "Upgrade" only to "WebSocket".')

        request.environ['HTTP_UPGRADE'] = 'ssl'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Can "Upgrade" only to "WebSocket".')
Example #3
0
    def handler(self, request):
        matchdict = request.matchdict

        # lookup transport
        tid = matchdict['transport']

        if tid not in handlers or tid in self.disable_transports:
            return HTTPNotFound()

        create, transport = handlers[tid]

        # session
        manager = self.session_manager

        sid = matchdict['session']
        if not sid or '.' in sid or '.' in matchdict['server']:
            return HTTPNotFound()

        if self.per_user:
            sid = (authenticated_userid(request), sid)

        try:
            session = manager.get(sid, create, request=request)
        except KeyError:
            return HTTPNotFound(headers=(session_cookie(request), ))

        request.environ['wsgi.sockjs_session'] = session

        # websocket
        if tid == 'websocket':
            if 'HTTP_SEC_WEBSOCKET_VERSION' in request.environ:
                res = init_websocket(request)
            elif 'HTTP_ORIGIN' in request.environ:
                res = init_websocket_hixie(request)
            else:
                res = init_websocket(request)

            if res is not None:
                return res

        try:
            return transport(session, request)
        except Exception as exc:
            session.release()
            log.exception('Exception in transport: %s' % tid)
            return HTTPBadRequest(str(exc))
Example #4
0
    def handler(self, request):
        matchdict = request.matchdict

        # lookup transport
        tid = matchdict['transport']

        if tid not in handlers or tid in self.disable_transports:
            return HTTPNotFound()

        create, transport = handlers[tid]

        # session
        manager = self.session_manager

        sid = matchdict['session']
        if not sid or '.' in sid or '.' in matchdict['server']:
            return HTTPNotFound()

        if self.per_user:
            sid = (authenticated_userid(request), sid)

        try:
            session = manager.get(sid, create, request=request)
        except KeyError:
            return HTTPNotFound(headers=(session_cookie(request),))

        request.environ['wsgi.sockjs_session'] = session

        # websocket
        if tid == 'websocket':
            if 'HTTP_SEC_WEBSOCKET_VERSION' in request.environ:
                res = init_websocket(request)
            elif 'HTTP_ORIGIN' in request.environ:
                res = init_websocket_hixie(request)
            else:
                res = init_websocket(request)

            if res is not None:
                return res

        try:
            return transport(session, request)
        except Exception as exc:
            session.release()
            log.exception('Exception in transport: %s'%tid)
            return HTTPBadRequest(str(exc))
Example #5
0
    def test_connection_upgrade(self):
        from pyramid_sockjs.websocket import init_websocket
        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'close'})

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, '"Connection" must be "Upgrade".')
Example #6
0
    def test_websocket_version(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'keep-alive, upgrade'})

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Unsupported WebSocket version.')

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'keep-alive, upgrade',
                     'HTTP_SEC_WEBSOCKET_VERSION': '5'})

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Unsupported WebSocket version.')
    def test_connection_upgrade(self):
        from pyramid_sockjs.websocket import init_websocket
        request = DummyRequest(environ={
            'HTTP_UPGRADE': 'websocket',
            'HTTP_CONNECTION': 'close'
        })

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, '"Connection" must be "Upgrade".')
Example #8
0
    def test_method_type(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'keep-alive, upgrade',
                     'HTTP_SEC_WEBSOCKET_VERSION': '8'})
        request.method = 'POST'

        res = init_websocket(request)
        self.assertIsInstance(res, Response)
        self.assertEqual(res.status, '405 Method Not Allowed')
    def test_websocket_version(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(environ={
            'HTTP_UPGRADE': 'websocket',
            'HTTP_CONNECTION': 'keep-alive, upgrade'
        })

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Unsupported WebSocket version.')

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_CONNECTION': 'keep-alive, upgrade',
                'HTTP_SEC_WEBSOCKET_VERSION': '5'
            })

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'Unsupported WebSocket version.')
Example #10
0
    def test_protocol_version(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'keep-alive, upgrade',
                     'HTTP_SEC_WEBSOCKET_VERSION': '8',
                     'SERVER_PROTOCOL': 'HTTP/1.0'})
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'HTTP/1.1 is required')
    def test_method_type(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_CONNECTION': 'keep-alive, upgrade',
                'HTTP_SEC_WEBSOCKET_VERSION': '8'
            })
        request.method = 'POST'

        res = init_websocket(request)
        self.assertIsInstance(res, Response)
        self.assertEqual(res.status, '405 Method Not Allowed')
Example #12
0
    def test_gevent_wsgi_input(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_SEC_WEBSOCKET_VERSION': '8',
                     'SERVER_PROTOCOL': 'HTTP/1.1',
                     'HTTP_SEC_WEBSOCKET_KEY': '5Jfbk3Hf5oLcReU416OxpA==',
                     'HTTP_CONNECTION': 'keep-alive, upgrade'})
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, "socket object is not available")
Example #13
0
    def test_websocket_key(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_CONNECTION': 'keep-alive, upgrade',
                     'HTTP_SEC_WEBSOCKET_VERSION': '8',
                     'SERVER_PROTOCOL': 'HTTP/1.1',
                     'HTTP_SEC_WEBSOCKET_KEY': None,})
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'HTTP_SEC_WEBSOCKET_KEY is invalid key')
    def test_protocol_version(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_CONNECTION': 'keep-alive, upgrade',
                'HTTP_SEC_WEBSOCKET_VERSION': '8',
                'SERVER_PROTOCOL': 'HTTP/1.0'
            })
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'HTTP/1.1 is required')
    def test_gevent_wsgi_input(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_SEC_WEBSOCKET_VERSION': '8',
                'SERVER_PROTOCOL': 'HTTP/1.1',
                'HTTP_SEC_WEBSOCKET_KEY': '5Jfbk3Hf5oLcReU416OxpA==',
                'HTTP_CONNECTION': 'keep-alive, upgrade'
            })
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, "socket object is not available")
    def test_websocket_key(self):
        from pyramid_sockjs.websocket import init_websocket

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_CONNECTION': 'keep-alive, upgrade',
                'HTTP_SEC_WEBSOCKET_VERSION': '8',
                'SERVER_PROTOCOL': 'HTTP/1.1',
                'HTTP_SEC_WEBSOCKET_KEY': None,
            })
        request.method = 'GET'

        res = init_websocket(request)
        self.assertIsInstance(res, HTTPBadRequest)
        self.assertEqual(res.detail, 'HTTP_SEC_WEBSOCKET_KEY is invalid key')
Example #17
0
    def websocket(self, request):
        # session
        manager = self.session_manager

        sid = '%0.9d'%random.randint(1, 2147483647)
        session = manager.get(sid, True, request=request)
        request.environ['wsgi.sockjs_session'] = session

        # websocket
        if 'HTTP_ORIGIN' in request.environ:
            return HTTPNotFound()

        res = init_websocket(request)
        if res is not None:
            return res

        return RawWebSocketTransport(session, request)
Example #18
0
    def websocket(self, request):
        # session
        manager = self.session_manager

        sid = '%0.9d' % random.randint(1, 2147483647)
        if self.per_user:
            sid = (authenticated_userid(request), sid)

        session = manager.get(sid, True, request=request)
        request.environ['wsgi.sockjs_session'] = session

        # websocket
        if 'HTTP_ORIGIN' in request.environ:
            return HTTPNotFound()

        res = init_websocket(request)
        if res is not None:
            return res

        return RawWebSocketTransport(session, request)
    def test_success(self):
        from pyramid_sockjs.websocket import init_websocket

        class WS(object):
            def __init__(self, rfile, environ):
                self.rfile = rfile
                self.environ = environ

        request = DummyRequest(
            environ={
                'HTTP_UPGRADE': 'websocket',
                'HTTP_SEC_WEBSOCKET_VERSION': '8',
                'SERVER_PROTOCOL': 'HTTP/1.1',
                'HTTP_SEC_WEBSOCKET_KEY': '5Jfbk3Hf5oLcReU416OxpA==',
                'HTTP_CONNECTION': 'keep-alive, upgrade',
                'gunicorn.socket': SocketMock()
            })
        request.method = 'GET'

        from pyramid_sockjs import websocket
        orig = websocket.WebSocketHybi
        websocket.WebSocketHybi = WS
        try:
            ws = init_websocket(request)
        finally:
            websocket.WebSocketHybi = orig

        environ = request.environ
        self.assertIn('wsgi.websocket_version', environ)
        self.assertEqual(environ['wsgi.websocket_version'], 'hybi-8')

        self.assertIn('wsgi.websocket', environ)
        self.assertIsInstance(environ['wsgi.websocket'], WS)

        response = request.response
        self.assertEqual(response.status, '101 Switching Protocols')
        self.assertEqual(response.headers['Upgrade'], 'websocket')
        self.assertEqual(response.headers['Connection'], 'Upgrade')
Example #20
0
    def test_success(self):
        from pyramid_sockjs.websocket import init_websocket

        class WS(object):
            def __init__(self, rfile, environ):
                self.rfile = rfile
                self.environ = environ

        request = DummyRequest(
            environ={'HTTP_UPGRADE': 'websocket',
                     'HTTP_SEC_WEBSOCKET_VERSION': '8',
                     'SERVER_PROTOCOL': 'HTTP/1.1',
                     'HTTP_SEC_WEBSOCKET_KEY': '5Jfbk3Hf5oLcReU416OxpA==',
                     'HTTP_CONNECTION': 'keep-alive, upgrade',
                     'gunicorn.socket': SocketMock()})
        request.method = 'GET'

        from pyramid_sockjs import websocket
        orig = websocket.WebSocketHybi
        websocket.WebSocketHybi = WS
        try:
            ws = init_websocket(request)
        finally:
            websocket.WebSocketHybi = orig

        environ = request.environ
        self.assertIn('wsgi.websocket_version', environ)
        self.assertEqual(environ['wsgi.websocket_version'], 'hybi-8')

        self.assertIn('wsgi.websocket', environ)
        self.assertIsInstance(environ['wsgi.websocket'], WS)

        response = request.response
        self.assertEqual(response.status, '101 Switching Protocols')
        self.assertEqual(response.headers['Upgrade'], 'websocket')
        self.assertEqual(response.headers['Connection'], 'Upgrade')