From 42ac2dddd0682b42f2dbc824ac61f934265fce35 Mon Sep 17 00:00:00 2001 From: MarkLark86 Date: Wed, 15 Feb 2023 16:41:17 +1100 Subject: [PATCH] [NHUB-231] fix(ws): Filter consumers on push not working when proxy is used (#2430) * [NHUB-231] fix(ws): Filter consumers on push not working when proxy is used * fix(mypy) --- superdesk/io/feeding_services/http_base_service.py | 4 ++-- superdesk/websockets_comms.py | 10 +++++----- tests/websockets_test.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/superdesk/io/feeding_services/http_base_service.py b/superdesk/io/feeding_services/http_base_service.py index 35a1afa9ff..30027a585e 100644 --- a/superdesk/io/feeding_services/http_base_service.py +++ b/superdesk/io/feeding_services/http_base_service.py @@ -8,7 +8,7 @@ # AUTHORS and LICENSE files distributed with this source code, or # at https://www.sourcefabric.org/superdesk/license -from typing import List, Dict, Optional +from typing import List, Dict, Optional, Union import traceback import requests from superdesk.errors import IngestApiError, SuperdeskIngestError @@ -73,7 +73,7 @@ class HTTPFeedingServiceBase(FeedingService): # Set to True if authentication is mandatory, False if there is no authentication # and None to add authentication if user and password are defined. # If auth_required is defined in config fields, it will override this value. - HTTP_AUTH = True + HTTP_AUTH: Union[bool, None] = True # use this when auth is always required AUTH_FIELDS: List[Dict] = [ diff --git a/superdesk/websockets_comms.py b/superdesk/websockets_comms.py index 8c8d01c0b0..421afcb03e 100644 --- a/superdesk/websockets_comms.py +++ b/superdesk/websockets_comms.py @@ -18,7 +18,7 @@ import websockets from websockets.server import WebSocketServerProtocol import signal -from urllib.parse import parse_qs +from urllib.parse import urlparse, parse_qs from uuid import UUID from datetime import timedelta, datetime @@ -191,12 +191,12 @@ def _add_client(self, websocket: WebSocketServerProtocol): self.clients.add(websocket) # Store client URL args for use with message filters - if not websocket.path.startswith(self.subscribe_prefix): + if self.subscribe_prefix not in websocket.path: self.client_url_args[websocket.id] = {} else: - self.client_url_args[websocket.id] = { - key: val[0] for key, val in parse_qs(websocket.path.replace(self.subscribe_prefix, "")).items() - } + parsed_url = urlparse(websocket.path) + url_params = parse_qs(parsed_url.query) + self.client_url_args[websocket.id] = {key: val[0] for key, val in url_params.items()} def _remove_client(self, websocket: WebSocketServerProtocol): self.clients.remove(websocket) diff --git a/tests/websockets_test.py b/tests/websockets_test.py index a2833c5ead..27a0549215 100644 --- a/tests/websockets_test.py +++ b/tests/websockets_test.py @@ -56,9 +56,9 @@ def test_broadcast_specific_recipients(self): com = SocketCommunication("host", "port", "url") client_no_user = TestClient("") - client_user_abc123_sess_12345 = TestClient("/subscribe?user=abc123&session=12345") - client_user_abc123_sess_67890 = TestClient("/subscribe?user=abc123&session=67890") - client_user_def456 = TestClient("/subscribe?user=def456") + client_user_abc123_sess_12345 = TestClient("/ws/subscribe?user=abc123&session=12345") + client_user_abc123_sess_67890 = TestClient("/ws/subscribe?user=abc123&session=67890") + client_user_def456 = TestClient("/ws/subscribe?user=def456") com._add_client(client_no_user) com._add_client(client_user_abc123_sess_12345) com._add_client(client_user_abc123_sess_67890)