From 74de9799f42fd74dbc53dbac61e930fe07a02179 Mon Sep 17 00:00:00 2001 From: "epieffe.eth" Date: Sun, 29 Dec 2024 20:40:25 +0100 Subject: [PATCH] Replace regex with custom logic for MQTT topic matching --- .../internal/config/MqttConditionMatcher.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/runtime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/config/MqttConditionMatcher.java b/runtime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/config/MqttConditionMatcher.java index 4c0f368a82..92d3f6d83f 100644 --- a/runtime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/config/MqttConditionMatcher.java +++ b/runtime/binding-mqtt/src/main/java/io/aklivity/zilla/runtime/binding/mqtt/internal/config/MqttConditionMatcher.java @@ -113,15 +113,31 @@ private boolean topicMatches(String topic, String pattern, long authorization) pattern = pattern.replace(String.format("{guarded[%s].identity}", g.name), identity); } } - return topic.matches(pattern - .replace("{", "\\{") - .replace("}", "\\}") - .replace("[", "\\[") - .replace("]", "\\]") - .replace(".", "\\.") - .replace("$", "\\$") - .replace("+", "[^/]*") - .replace("#", ".*")); + int topicIndex = 0; + for (int i = 0; i < pattern.length(); ++i) + { + char patternChar = pattern.charAt(i); + if (patternChar == '#') + { + return true; + } + else if (patternChar == '+') + { + while (topicIndex < topic.length()) + { + if (topic.charAt(topicIndex) == '/') + { + break; + } + topicIndex++; + } + } + else if (topicIndex == topic.length() || topic.charAt(topicIndex++) != patternChar) + { + return false; + } + } + return topicIndex == topic.length(); } private static List asWildcardMatcher(