comparison mod_rest/mod_rest.lua @ 3804:d74509cd35fb

mod_rest: Use HTTP status 422 for stanza problems Means the payload can't be processed for some reason other than syntax errors.
author Kim Alvefur <zash@zash.se>
date Wed, 01 Jan 2020 07:55:48 +0100
parents dc2b5a412286
children 683b06c0348f
comparison
equal deleted inserted replaced
3803:dc2b5a412286 3804:d74509cd35fb
37 if not payload then 37 if not payload then
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 = 400, text = "'xmlns' attribute must be empty" }); 42 return errors.new({ code = 422, text = "'xmlns' attribute must be empty" });
43 end 43 end
44 local to = jid.prep(payload.attr.to); 44 local to = jid.prep(payload.attr.to);
45 if not to then 45 if not to then
46 return errors.new({ code = 400, text = "Invalid destination JID" }); 46 return errors.new({ code = 422, text = "Invalid destination JID" });
47 end 47 end
48 local from = module.host; 48 local from = module.host;
49 if allow_any_source and payload.attr.from then 49 if allow_any_source and payload.attr.from then
50 from = jid.prep(payload.attr.from); 50 from = jid.prep(payload.attr.from);
51 if not from then 51 if not from then
52 return errors.new({ code = 400, text = "Invalid source JID" }); 52 return errors.new({ code = 422, text = "Invalid source JID" });
53 end 53 end
54 if validate_from_addresses and not jid.compare(from, module.host) then 54 if validate_from_addresses and not jid.compare(from, module.host) then
55 return errors.new({ code = 403, text = "Source JID must belong to current host" }); 55 return errors.new({ code = 403, text = "Source JID must belong to current host" });
56 end 56 end
57 end 57 end
63 ["xml:lang"] = payload.attr["xml:lang"], 63 ["xml:lang"] = payload.attr["xml:lang"],
64 }; 64 };
65 module:log("debug", "Received[rest]: %s", payload:top_tag()); 65 module:log("debug", "Received[rest]: %s", payload:top_tag());
66 if payload.name == "iq" then 66 if payload.name == "iq" then
67 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then 67 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then
68 return errors.new({ code = 400, text = "'iq' stanza must be of type 'get' or 'set'" }); 68 return errors.new({ code = 422, text = "'iq' stanza must be of type 'get' or 'set'" });
69 end 69 end
70 return module:send_iq(payload):next( 70 return module:send_iq(payload):next(
71 function (result) 71 function (result)
72 response.headers.content_type = "application/xmpp+xml"; 72 response.headers.content_type = "application/xmpp+xml";
73 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); 73 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag());