Skip to content

Commit

Permalink
mqttgateway: Fix several bugs in topic matching for sendToMS
Browse files Browse the repository at this point in the history
- Fixes matching a topic "test" that has a "test/#" subscription
- Fixes + wildcard matching

Fixes #47

Fixes
  • Loading branch information
christianTF committed Sep 4, 2020
1 parent 08c853b commit 621aa43
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions bin/mqttgateway.pl
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,27 @@ sub received

# LOGDEB "Topic '$topic', " . scalar(@subscriptions) . " Subscriptions, " . scalar(@subscriptions_toms) . " toms elements";

use constant SUBMATCH_FIND => '\+'; # Quoted '+'
use constant SUBMATCH_REPLACE => '\[\^\/\]\+'; # Quoted '[^/]+'

foreach ( @subscriptions ) {
$_ =~ s$\\+$[^/]+$;
$_ =~ s$#$.+$;
# LOGDEB "Regex to query: $_";
my $re = qr/$_/;
my $regex = $_;
# LOGDEB "$_ Regex 0: " . $regex;

## Eval + in subscription
$regex =~ s/SUBMATCH_FIND/SUBMATCH_REPLACE/g;
$regex =~ s/\\//g; # Remoce quotation
# LOGDEB "$_ Regex 1: " . $regex;

## Eval # in subscription
if( $regex eq '#' ) { # If subscription is only #, this is a "match-all"
# LOGDEB "-->Regex is #: $regex";
$regex = ".+";
} elsif ( substr($regex, -1) eq '#' ) { # If subscriptipn ends with #, also fully accept the last hierarchy ( topic test is matched by test/# )
$regex = substr($regex, 0, -2) . '.*';
}
# LOGDEB "$_ Regex to query: $regex";
my $re = qr/$regex/;
if( $topic =~ /$re/ ) {
@toMS = @{$subscriptions_toms[$idx]};
# LOGDEB "$_ matches $topic, send to MS " . join(",", @toMS);
Expand Down

0 comments on commit 621aa43

Please sign in to comment.