Software /
code /
prosody
Comparison
util/jid.lua @ 11056:0b0a42542456
util.jid: Fix special escaping of '\' per XEP-0106
From XEP-0106 §2. Requirements:
> in certain circumstances, the escaping character itself ("\") might
> also be escaped
Later in §4.2 Address Transformation Algorithm it is stated that the
backslash would only be escaped if it forms an escape sequence. Thus
'\foo' is unaltered but '\20' must be escaped into '\5c20'.
Thanks to lovetox and jonas’ for brining up.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 28 Aug 2020 18:44:02 +0200 |
parent | 10358:a77d9b3885bb |
child | 12190:3616128cd2e3 |
comparison
equal
deleted
inserted
replaced
11055:5fb95410f89c | 11056:0b0a42542456 |
---|---|
20 ["/"] = "\\2f"; [":"] = "\\3a"; | 20 ["/"] = "\\2f"; [":"] = "\\3a"; |
21 ["<"] = "\\3c"; [">"] = "\\3e"; | 21 ["<"] = "\\3c"; [">"] = "\\3e"; |
22 ["@"] = "\\40"; ["\\"] = "\\5c"; | 22 ["@"] = "\\40"; ["\\"] = "\\5c"; |
23 }; | 23 }; |
24 local unescapes = {}; | 24 local unescapes = {}; |
25 for k,v in pairs(escapes) do unescapes[v] = k; end | 25 local backslash_escapes = {}; |
26 for k,v in pairs(escapes) do | |
27 unescapes[v] = k; | |
28 backslash_escapes[v] = v:gsub("\\", escapes) | |
29 end | |
26 | 30 |
27 local _ENV = nil; | 31 local _ENV = nil; |
28 -- luacheck: std none | 32 -- luacheck: std none |
29 | 33 |
30 local function split(jid) | 34 local function split(jid) |
105 | 109 |
106 local function resource(jid) | 110 local function resource(jid) |
107 return (select(3, split(jid))); | 111 return (select(3, split(jid))); |
108 end | 112 end |
109 | 113 |
110 local function escape(s) return s and (s:gsub(".", escapes)); end | 114 local function escape(s) return s and (s:gsub("\\%x%x", backslash_escapes):gsub("[\"&'/:<>@ ]", escapes)); end |
111 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end | 115 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end |
112 | 116 |
113 return { | 117 return { |
114 split = split; | 118 split = split; |
115 bare = bare; | 119 bare = bare; |