Software /
code /
prosody-modules
Comparison
mod_rest/jsonmap.lib.lua @ 3817:937f8c463be6
mod_rest: Stricter type checks in JSON mapping
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Jan 2020 17:16:40 +0100 |
parent | 3813:aa1ad69c7c10 |
child | 3818:a607c69d0804 |
comparison
equal
deleted
inserted
replaced
3816:8473fd2d09c1 | 3817:937f8c463be6 |
---|---|
29 "func", "http://jabber.org/protocol/xhtml-im", "html", | 29 "func", "http://jabber.org/protocol/xhtml-im", "html", |
30 function (s) --> json string | 30 function (s) --> json string |
31 return tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")); | 31 return tostring(s:get_child("body", "http://www.w3.org/1999/xhtml")); |
32 end; | 32 end; |
33 function (s) --> xml | 33 function (s) --> xml |
34 return xml.parse([[<html xmlns='http://jabber.org/protocol/xhtml-im'>]]..s..[[</html>]]); | 34 if type(s) == "string" then |
35 return xml.parse([[<html xmlns='http://jabber.org/protocol/xhtml-im'>]]..s..[[</html>]]); | |
36 end | |
35 end; | 37 end; |
36 }; | 38 }; |
37 | 39 |
38 -- XEP-0199 | 40 -- XEP-0199 |
39 ping = {"bool_tag", "urn:xmpp:ping", "ping"}, | 41 ping = {"bool_tag", "urn:xmpp:ping", "ping"}, |
93 oob_url = {"func", "jabber:iq:oob", "query", | 95 oob_url = {"func", "jabber:iq:oob", "query", |
94 function (s) | 96 function (s) |
95 return s:get_child_text("url"); | 97 return s:get_child_text("url"); |
96 end; | 98 end; |
97 function (s) | 99 function (s) |
98 return st.stanza("query", { xmlns = "jabber:iq:oob" }):text_tag("url", s); | 100 if type(s) == "string" then |
101 return st.stanza("query", { xmlns = "jabber:iq:oob" }):text_tag("url", s); | |
102 end | |
99 end; | 103 end; |
100 }; | 104 }; |
101 }; | 105 }; |
102 | 106 |
103 local implied_kinds = { | 107 local implied_kinds = { |
186 end | 190 end |
187 | 191 |
188 return t; | 192 return t; |
189 end | 193 end |
190 | 194 |
195 local function str(s) | |
196 if type(s) == "string" then | |
197 return s; | |
198 end | |
199 end | |
200 | |
191 local function json2st(t) | 201 local function json2st(t) |
192 local kind = t.kind or kind_by_type[t.type]; | 202 if type(t) ~= "table" or not str(next(t)) then |
203 return nil, "invalid-json"; | |
204 end | |
205 local kind = str(t.kind) or kind_by_type[str(t.type)]; | |
193 if not kind then | 206 if not kind then |
194 for k, implied in pairs(implied_kinds) do | 207 for k, implied in pairs(implied_kinds) do |
195 if t[k] then | 208 if t[k] then |
196 kind = implied; | 209 kind = implied; |
197 break | 210 break |
198 end | 211 end |
199 end | 212 end |
200 end | 213 end |
201 | 214 |
202 local s = st.stanza(kind or "message", { | 215 local s = st.stanza(kind or "message", { |
203 type = t.type ~= "available" and t.type or nil, | 216 type = t.type ~= "available" and str(t.type) or nil, |
204 to = jid.prep(t.to); | 217 to = str(t.to) and jid.prep(t.to); |
205 from = jid.prep(t.from); | 218 from = str(t.to) and jid.prep(t.from); |
206 id = t.id, | 219 id = str(t.id), |
207 }); | 220 }); |
208 | 221 |
209 if t.to and not s.attr.to then | 222 if t.to and not s.attr.to then |
210 return nil, "invalid-jid-to"; | 223 return nil, "invalid-jid-to"; |
211 end | 224 end |
212 if t.from and not s.attr.from then | 225 if t.from and not s.attr.from then |
213 return nil, "invalid-jid-from"; | 226 return nil, "invalid-jid-from"; |
214 end | 227 end |
215 | 228 |
216 if t.error then | 229 if type(t.error) == "table" then |
217 return st.error_reply(st.reply(s), t.error.type, t.error.condition, t.error.text); | 230 return st.error_reply(st.reply(s), str(t.error.type), str(t.error.condition), str(t.error.text)); |
218 elseif t.type == "error" then | 231 elseif t.type == "error" then |
219 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); | 232 s:text_tag("error", t.body, { code = t.error_code and tostring(t.error_code) }); |
220 return s; | 233 return s; |
221 end | 234 end |
222 | 235 |