@@ -45,31 +45,26 @@ _build_json_send() {
45
45
' {jsonrpc:"2.0", method:"send", params:{($name):$value, message:$message}}'
46
46
}
47
47
48
- #
49
- # base64url encoding is required when a base64-encoded string
50
- # (e.g. group-id) is used as part of an mqtt topic.
51
- #
52
-
53
48
# ######################################
54
- # Convert a text from base64 to base64url .
49
+ # Encode a text to percent-encoding .
55
50
# Arguments:
56
- # 1: Text in base64 encoding
51
+ # 1: Text
57
52
# Outputs:
58
- # Text in base64url encoding
53
+ # Text in percent- encoding
59
54
# ######################################
60
- _to_base64url () {
61
- printf ' %s' " ${1} " | tr ' /+ ' ' _- '
55
+ percent_encode () {
56
+ printf ' %s' " ${1} " | jq -sRr @uri
62
57
}
63
58
64
59
# ######################################
65
- # Convert a text from base64url to base64 .
60
+ # Decode a text from percent-encoding .
66
61
# Arguments:
67
- # 1: Text in base64url encoding
62
+ # 1: Text in percent- encoding
68
63
# Outputs:
69
- # Text in base64 encoding
64
+ # Decoded Text
70
65
# ######################################
71
- _from_base64url () {
72
- printf ' %s' " ${1} " | tr ' _- ' ' /+ '
66
+ percent_decode () {
67
+ printf ' %s' " ${1} " | printf ' %b\n ' " $( sed -E -e ' s/\+/ /g ' -e ' s/%([0-9a-fA-F]{2})/\\x\1/g ' ) "
73
68
}
74
69
75
70
# ######################################
@@ -121,7 +116,7 @@ mqtt_publish() {
121
116
topic=" ${topic} /timestamp/${timestamp} "
122
117
fi
123
118
if [ " ${group_id} " ]; then
124
- topic=" ${topic} /group/$( _to_base64url " ${group_id} " ) "
119
+ topic=" ${topic} /group/$( percent_encode " ${group_id} " ) "
125
120
fi
126
121
if [ " ${quote_timestamp} " ]; then
127
122
topic=" ${topic} /quote/${quote_timestamp} "
@@ -150,7 +145,7 @@ mqtt_subscribe() {
150
145
case " ${topic} " in
151
146
" ${MQTT_SUBSCRIBE_TOPIC} " ) printf ' %s\n' " ${payload} " ;;
152
147
" ${MQTT_SUBSCRIBE_TOPIC} " /group/* )
153
- _build_json_send " ${payload} " groupId " $( _from_base64url " ${topic##*/ } " ) "
148
+ _build_json_send " ${payload} " groupId " $( percent_decode " ${topic##*/ } " ) "
154
149
;;
155
150
" ${MQTT_SUBSCRIBE_TOPIC} " /* )
156
151
_build_json_send " ${payload} " recipient " +${topic##*/ } "
0 commit comments