:param dict applications: dict of `name -> task function` :param bool/str cdn: Whether to load front-end static resources from CDN :param callable check_origin_func: check_origin_func(origin, host) -> bool :return: aiohttp Request Handler
(applications, cdn, websocket_settings, reconnect_timeout=0, check_origin_func=_is_same_site)
| 72 | |
| 73 | |
| 74 | def _webio_handler(applications, cdn, websocket_settings, reconnect_timeout=0, check_origin_func=_is_same_site): |
| 75 | """ |
| 76 | :param dict applications: dict of `name -> task function` |
| 77 | :param bool/str cdn: Whether to load front-end static resources from CDN |
| 78 | :param callable check_origin_func: check_origin_func(origin, host) -> bool |
| 79 | :return: aiohttp Request Handler |
| 80 | """ |
| 81 | ws_adaptor.set_expire_second(reconnect_timeout) |
| 82 | |
| 83 | async def wshandle(request: web.Request): |
| 84 | ioloop = asyncio.get_event_loop() |
| 85 | asyncio.get_event_loop().create_task(ws_adaptor.session_clean_task()) |
| 86 | |
| 87 | origin = request.headers.get('origin') |
| 88 | if origin and not check_origin_func(origin=origin, host=request.host): |
| 89 | return web.Response(status=403, text="Cross origin websockets not allowed") |
| 90 | |
| 91 | if request.headers.get("Upgrade", "").lower() != "websocket": |
| 92 | # Backward compatible |
| 93 | if request.query.getone('test', ''): |
| 94 | return web.Response(text="") |
| 95 | |
| 96 | app_name = request.query.getone('app', 'index') |
| 97 | app = applications.get(app_name) or applications['index'] |
| 98 | no_cdn = cdn is True and request.query.getone('_pywebio_cdn', '') == 'false' |
| 99 | html = render_page(app, protocol='ws', cdn=False if no_cdn else cdn) |
| 100 | return web.Response(body=html, content_type='text/html') |
| 101 | |
| 102 | ws = web.WebSocketResponse(**websocket_settings) |
| 103 | await ws.prepare(request) |
| 104 | |
| 105 | app_name = request.query.getone('app', 'index') |
| 106 | application = applications.get(app_name) or applications['index'] |
| 107 | |
| 108 | conn = WebSocketConnection(ws, request, ioloop) |
| 109 | handler = ws_adaptor.WebSocketHandler( |
| 110 | connection=conn, application=application, reconnectable=bool(reconnect_timeout), ioloop=ioloop |
| 111 | ) |
| 112 | |
| 113 | # see: https://github.com/aio-libs/aiohttp/issues/1768 |
| 114 | try: |
| 115 | async for msg in ws: |
| 116 | if msg.type in (web.WSMsgType.text, web.WSMsgType.binary): |
| 117 | handler.send_client_data(msg.data) |
| 118 | elif msg.type == web.WSMsgType.close: |
| 119 | raise asyncio.CancelledError() |
| 120 | finally: |
| 121 | handler.notify_connection_lost() |
| 122 | |
| 123 | return ws |
| 124 | |
| 125 | return wshandle |
| 126 | |
| 127 | |
| 128 | def webio_handler(applications, cdn=True, reconnect_timeout=0, allowed_origins=None, check_origin=None, |
no outgoing calls
no test coverage detected
searching dependent graphs…