60 to = to, |
60 to = to, |
61 id = payload.attr.id or id.medium(), |
61 id = payload.attr.id or id.medium(), |
62 type = payload.attr.type, |
62 type = payload.attr.type, |
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 if payload.name == "iq" then |
66 if payload.name == "iq" then |
66 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then |
67 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then |
67 return errors.new({ code = 400, text = "'iq' stanza must be of type 'get' or 'set'" }); |
68 return errors.new({ code = 400, text = "'iq' stanza must be of type 'get' or 'set'" }); |
68 end |
69 end |
69 return module:send_iq(payload):next( |
70 return module:send_iq(payload):next( |
70 function (result) |
71 function (result) |
71 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()); |
72 return tostring(result.stanza); |
74 return tostring(result.stanza); |
73 end, |
75 end, |
74 function (error) |
76 function (error) |
75 if error.context.stanza then |
77 if error.context.stanza then |
76 response.headers.content_type = "application/xmpp+xml"; |
78 response.headers.content_type = "application/xmpp+xml"; |
|
79 module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag()); |
77 return tostring(error.context.stanza); |
80 return tostring(error.context.stanza); |
78 else |
81 else |
79 return error; |
82 return error; |
80 end |
83 end |
81 end); |
84 end); |
82 elseif payload.name == "message" or payload.name == "presence" then |
85 elseif payload.name == "message" or payload.name == "presence" then |
83 local origin = {}; |
86 local origin = {}; |
84 function origin.send(stanza) |
87 function origin.send(stanza) |
|
88 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
85 response:send(tostring(stanza)); |
89 response:send(tostring(stanza)); |
86 return true; |
90 return true; |
87 end |
91 end |
88 response.headers.content_type = "application/xmpp+xml"; |
92 response.headers.content_type = "application/xmpp+xml"; |
89 if module:send(payload, origin) then |
93 if module:send(payload, origin) then |
147 local request_body = tostring(stanza); |
151 local request_body = tostring(stanza); |
148 |
152 |
149 -- Keep only the top level element and let the rest be GC'd |
153 -- Keep only the top level element and let the rest be GC'd |
150 stanza = st.clone(stanza, true); |
154 stanza = st.clone(stanza, true); |
151 |
155 |
|
156 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
152 http.request(rest_url, { |
157 http.request(rest_url, { |
153 body = request_body, |
158 body = request_body, |
154 headers = { |
159 headers = { |
155 ["Content-Type"] = "application/xmpp+xml", |
160 ["Content-Type"] = "application/xmpp+xml", |
156 ["Content-Language"] = stanza.attr["xml:lang"], |
161 ["Content-Language"] = stanza.attr["xml:lang"], |