comparison mod_rest/mod_rest.lua @ 3805:683b06c0348f

mod_rest: Validate stanza kind earlier
author Kim Alvefur <zash@zash.se>
date Wed, 01 Jan 2020 07:59:18 +0100
parents d74509cd35fb
children c0caee8f54bb
comparison
equal deleted inserted replaced
3804:d74509cd35fb 3805:683b06c0348f
38 -- parse fail 38 -- parse fail
39 return errors.new({ code = 400, text = err }); 39 return errors.new({ code = 400, text = err });
40 end 40 end
41 if payload.attr.xmlns then 41 if payload.attr.xmlns then
42 return errors.new({ code = 422, text = "'xmlns' attribute must be empty" }); 42 return errors.new({ code = 422, text = "'xmlns' attribute must be empty" });
43 elseif payload.name ~= "message" and payload.name ~= "presence" and payload.name ~= "iq" then
44 return errors.new({ code = 422, text = "Invalid stanza, must be 'message', 'presence' or 'iq'." });
43 end 45 end
44 local to = jid.prep(payload.attr.to); 46 local to = jid.prep(payload.attr.to);
45 if not to then 47 if not to then
46 return errors.new({ code = 422, text = "Invalid destination JID" }); 48 return errors.new({ code = 422, text = "Invalid destination JID" });
47 end 49 end
80 return tostring(error.context.stanza); 82 return tostring(error.context.stanza);
81 else 83 else
82 return error; 84 return error;
83 end 85 end
84 end); 86 end);
85 elseif payload.name == "message" or payload.name == "presence" then 87 else
86 local origin = {}; 88 local origin = {};
87 function origin.send(stanza) 89 function origin.send(stanza)
88 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); 90 module:log("debug", "Sending[rest]: %s", stanza:top_tag());
89 response:send(tostring(stanza)); 91 response:send(tostring(stanza));
90 return true; 92 return true;
93 if module:send(payload, origin) then 95 if module:send(payload, origin) then
94 return 202; 96 return 202;
95 else 97 else
96 return 500; 98 return 500;
97 end 99 end
98 else
99 return errors.new({ code = 400, text = "Invalid stanza, must be 'message', 'presence' or 'iq'." });
100 end 100 end
101 end 101 end
102 102
103 -- Handle stanzas submitted via HTTP 103 -- Handle stanzas submitted via HTTP
104 module:depends("http"); 104 module:depends("http");