Comparison

mod_rest/mod_rest.lua @ 4242:6a91d217acc9

mod_rest: Add whitespace to improve readability, code navigation I use {} in vim a lot.
author Kim Alvefur <zash@zash.se>
date Thu, 12 Nov 2020 20:37:54 +0100
parent 4066:07ae583bc565
child 4244:07c11080027e
comparison
equal deleted inserted replaced
4241:5e8b54deeb30 4242:6a91d217acc9
184 if err == "unknown-payload-type" then 184 if err == "unknown-payload-type" then
185 return errors.new("mediatype", ctx, post_errors); 185 return errors.new("mediatype", ctx, post_errors);
186 end 186 end
187 return errors.new("parse", ctx, post_errors); 187 return errors.new("parse", ctx, post_errors);
188 end 188 end
189
189 if payload.attr.xmlns then 190 if payload.attr.xmlns then
190 return errors.new("xmlns", nil, post_errors); 191 return errors.new("xmlns", nil, post_errors);
191 elseif payload.name ~= "message" and payload.name ~= "presence" and payload.name ~= "iq" then 192 elseif payload.name ~= "message" and payload.name ~= "presence" and payload.name ~= "iq" then
192 return errors.new("name", nil, post_errors); 193 return errors.new("name", nil, post_errors);
193 end 194 end
195
194 local to = jid.prep(payload.attr.to); 196 local to = jid.prep(payload.attr.to);
195 if not to then 197 if not to then
196 return errors.new("to", nil, post_errors); 198 return errors.new("to", nil, post_errors);
197 end 199 end
200
198 if payload.attr.from then 201 if payload.attr.from then
199 local requested_from = jid.prep(payload.attr.from); 202 local requested_from = jid.prep(payload.attr.from);
200 if not requested_from then 203 if not requested_from then
201 return errors.new("from", nil, post_errors); 204 return errors.new("from", nil, post_errors);
202 end 205 end
204 from = requested_from; 207 from = requested_from;
205 else 208 else
206 return errors.new("from_auth", nil, post_errors); 209 return errors.new("from_auth", nil, post_errors);
207 end 210 end
208 end 211 end
212
209 payload.attr = { 213 payload.attr = {
210 from = from, 214 from = from,
211 to = to, 215 to = to,
212 id = payload.attr.id or id.medium(), 216 id = payload.attr.id or id.medium(),
213 type = payload.attr.type, 217 type = payload.attr.type,
214 ["xml:lang"] = payload.attr["xml:lang"], 218 ["xml:lang"] = payload.attr["xml:lang"],
215 }; 219 };
220
216 module:log("debug", "Received[rest]: %s", payload:top_tag()); 221 module:log("debug", "Received[rest]: %s", payload:top_tag());
217 local send_type = decide_type((request.headers.accept or "") ..",".. request.headers.content_type, supported_outputs) 222 local send_type = decide_type((request.headers.accept or "") ..",".. request.headers.content_type, supported_outputs)
218 if payload.name == "iq" then 223 if payload.name == "iq" then
219 function origin.send(stanza) 224 function origin.send(stanza)
220 module:send(stanza); 225 module:send(stanza);
221 end 226 end
227
222 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then 228 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then
223 return errors.new("iq_type", nil, post_errors); 229 return errors.new("iq_type", nil, post_errors);
224 elseif #payload.tags ~= 1 then 230 elseif #payload.tags ~= 1 then
225 return errors.new("iq_tags", nil, post_errors); 231 return errors.new("iq_tags", nil, post_errors);
226 end 232 end
233
227 return module:send_iq(payload, origin):next( 234 return module:send_iq(payload, origin):next(
228 function (result) 235 function (result)
229 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); 236 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag());
230 response.headers.content_type = send_type; 237 response.headers.content_type = send_type;
231 return encode(send_type, result.stanza); 238 return encode(send_type, result.stanza);
247 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); 254 module:log("debug", "Sending[rest]: %s", stanza:top_tag());
248 response.headers.content_type = send_type; 255 response.headers.content_type = send_type;
249 response:send(encode(send_type, stanza)); 256 response:send(encode(send_type, stanza));
250 return true; 257 return true;
251 end 258 end
259
252 module:send(payload, origin); 260 module:send(payload, origin);
253 return 202; 261 return 202;
254 end 262 end
255 end 263 end
256 264