Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove try/except from add_subscription #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 33 additions & 36 deletions amqtt/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,43 +762,40 @@ def retain_message(
del self._retained_messages[topic_name]

async def add_subscription(self, subscription, session):
try:
a_filter = subscription[0]
if "#" in a_filter and not a_filter.endswith("#"):
# [MQTT-4.7.1-2] Wildcard character '#' is only allowed as last character in filter
return 0x80
if a_filter != "+":
if "+" in a_filter:
if "/+" not in a_filter and "+/" not in a_filter:
# [MQTT-4.7.1-3] + wildcard character must occupy entire level
return 0x80
# Check if the client is authorised to connect to the topic
permitted = await self.topic_filtering(session, topic=a_filter)
if not permitted:
return 0x80
qos = subscription[1]
if "max-qos" in self.config and qos > self.config["max-qos"]:
qos = self.config["max-qos"]
if a_filter not in self._subscriptions:
self._subscriptions[a_filter] = []
already_subscribed = next(
(
s
for (s, qos) in self._subscriptions[a_filter]
if s.client_id == session.client_id
),
None,
)
if not already_subscribed:
self._subscriptions[a_filter].append((session, qos))
else:
self.logger.debug(
"Client %s has already subscribed to %s"
% (format_client_message(session=session), a_filter)
)
return qos
except KeyError:
a_filter = subscription[0]
if "#" in a_filter and not a_filter.endswith("#"):
# [MQTT-4.7.1-2] Wildcard character '#' is only allowed as last character in filter
return 0x80
if a_filter != "+":
if "+" in a_filter:
if "/+" not in a_filter and "+/" not in a_filter:
# [MQTT-4.7.1-3] + wildcard character must occupy entire level
return 0x80
# Check if the client is authorised to connect to the topic
permitted = await self.topic_filtering(session, topic=a_filter)
if not permitted:
return 0x80
qos = subscription[1]
if "max-qos" in self.config and qos > self.config["max-qos"]:
qos = self.config["max-qos"]
if a_filter not in self._subscriptions:
self._subscriptions[a_filter] = []
already_subscribed = next(
(
s
for (s, qos) in self._subscriptions[a_filter]
if s.client_id == session.client_id
),
None,
)
if not already_subscribed:
self._subscriptions[a_filter].append((session, qos))
else:
self.logger.debug(
"Client %s has already subscribed to %s"
% (format_client_message(session=session), a_filter)
)
return qos

def _del_subscription(self, a_filter: str, session: Session) -> int:
"""
Expand Down