Fix ASGI websockets when serving on sub-path under site root

This commit is contained in:
Lance Edgar 2024-04-16 23:26:46 -05:00
parent a95cc2b9e8
commit 7fa39d42e2
2 changed files with 12 additions and 7 deletions

View file

@ -5,6 +5,9 @@ CHANGELOG
Unreleased Unreleased
---------- ----------
* Fix ASGI websockets when serving on sub-path under site root.
0.9.94 (2024-04-16) 0.9.94 (2024-04-16)
------------------- -------------------

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2022 Lance Edgar # Copyright © 2010-2024 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,14 +24,10 @@
ASGI App Utilities ASGI App Utilities
""" """
from __future__ import unicode_literals, absolute_import
import os import os
import configparser
import logging import logging
import six
from six.moves import configparser
from rattail.util import load_object from rattail.util import load_object
from asgiref.wsgi import WsgiToAsgi from asgiref.wsgi import WsgiToAsgi
@ -49,6 +45,12 @@ class TailboneWsgiToAsgi(WsgiToAsgi):
protocol = scope['type'] protocol = scope['type']
path = scope['path'] path = scope['path']
# strip off the root path, if non-empty. needed for serving
# under /poser or anything other than true site root
root_path = scope['root_path']
if root_path and path.startswith(root_path):
path = path[len(root_path):]
if protocol == 'websocket': if protocol == 'websocket':
websockets = self.wsgi_application.registry.get( websockets = self.wsgi_application.registry.get(
'tailbone_websockets', {}) 'tailbone_websockets', {})
@ -85,7 +87,7 @@ def make_asgi_app(main_app=None):
# parse the settings needed for pyramid app # parse the settings needed for pyramid app
settings = dict(parser.items('app:main')) settings = dict(parser.items('app:main'))
if isinstance(main_app, six.string_types): if isinstance(main_app, str):
make_wsgi_app = load_object(main_app) make_wsgi_app = load_object(main_app)
elif callable(main_app): elif callable(main_app):
make_wsgi_app = main_app make_wsgi_app = main_app