| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// matchs r2 against r1 following the AMQP rules for topic routing keys |
| 9 |
func topicMatch(r1, r2 string) bool { |
| 10 |
var match bool |
| 11 |
|
| 12 |
bparts := strings.Split(r1, ".") |
| 13 |
rparts := strings.Split(r2, ".") |
| 14 |
|
| 15 |
if len(rparts) > len(bparts) { |
| 16 |
return false |
| 17 |
} |
| 18 |
|
| 19 |
outer: |
| 20 |
for i := 0; i < len(bparts); i++ { |
| 21 |
bp := bparts[i] |
| 22 |
rp := rparts[i] |
| 23 |
|
| 24 |
if len(bp) == 0 { |
| 25 |
return false |
| 26 |
} |
| 27 |
|
| 28 |
var bsi, rsi int |
| 29 |
|
| 30 |
for rsi < len(rp) { |
| 31 |
// fmt.Printf("Testing '%c' and '%c'\n", bp[bsi], rp[rsi]) |
| 32 |
|
| 33 |
// The char '#' matchs none or more chars (everything that is on rp[rsi]) |
| 34 |
// next char, move on |
| 35 |
if bp[bsi] == '#' { |
| 36 |
match = true |
| 37 |
continue outer |
| 38 |
} else if bp[bsi] == '*' { |
| 39 |
// The '*' matchs only one character, then if it's the last char of binding part |
| 40 |
// and isn't the last char of rp, then surely it don't match. |
| 41 |
if bsi == len(bp)-1 && rsi < len(rp)-1 { |
| 42 |
match = false |
| 43 |
break outer |
| 44 |
} |
| 45 |
|
| 46 |
match = true |
| 47 |
|
| 48 |
if bsi < len(bp)-1 { |
| 49 |
bsi++ |
| 50 |
} |
| 51 |
|
| 52 |
rsi++ |
| 53 |
} else if bp[bsi] == rp[rsi] { |
| 54 |
// if it's the last char of binding part and it isn't an '*' or '#', |
| 55 |
// and it isn't the last char of rp, then we can stop here |
| 56 |
// because sure that route don't match the binding |
| 57 |
if bsi == len(bp)-1 && rsi < len(rp)-1 { |
| 58 |
match = false |
| 59 |
break outer |
| 60 |
} |
| 61 |
|
| 62 |
if bsi < len(bp)-1 { |
| 63 |
bsi++ |
| 64 |
} |
| 65 |
|
| 66 |
rsi++ |
| 67 |
|
| 68 |
match = true |
| 69 |
} else { |
| 70 |
match = false |
| 71 |
break outer |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
return match |
| 78 |
} |
| 79 |
|
| 80 |
// match the message headers with the bindings depending on the x-match value and ignorint headers starting with x- |
| 81 |
func headersMatch(b *BindingsMap, d *Delivery) (bool, error) { |
| 82 |
var cmpType string |
| 83 |
var init bool |
| 84 |
|
| 85 |
if val, ok := b.headers["x-match"]; ok { |
| 86 |
cmpType = strings.ToLower(val) |
| 87 |
if cmpType != "any" && cmpType != "all" { |
| 88 |
return false, fmt.Errorf("x-match binding should be set to \"any\" or \"all\" values. got: %s", cmpType) |
| 89 |
} |
| 90 |
} else { |
| 91 |
// when there is no x-match header messages are fanout to all the bindings |
| 92 |
return true, nil |
| 93 |
} |
| 94 |
|
| 95 |
// If it is all the base boolean flag iteration value is true, if it is any is false |
| 96 |
// To simplify the return if all the iteration completes |
| 97 |
init = cmpType == "all" |
| 98 |
|
| 99 |
for key, val := range b.headers { |
| 100 |
if !strings.HasPrefix(key, "x-") { |
| 101 |
switch cmpType { |
| 102 |
case "any": |
| 103 |
if d.headers[key] == val { |
| 104 |
return true, nil |
| 105 |
} |
| 106 |
case "all": |
| 107 |
if d.headers[key] != val { |
| 108 |
return false, nil |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return init, nil |
| 115 |
} |