Mercurial > prosody-modules
comparison mod_rest/mod_rest.lua @ 3812:f027b8b1e794
mod_rest: Factor out serialization of outgoing stanzas
More preparation for content negotiation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Jan 2020 16:19:10 +0100 |
parents | eb25110696cd |
children | aa1ad69c7c10 |
comparison
equal
deleted
inserted
replaced
3811:eb25110696cd | 3812:f027b8b1e794 |
---|---|
32 return nil, "unknown-payload-type"; | 32 return nil, "unknown-payload-type"; |
33 end | 33 end |
34 | 34 |
35 local function decide_type() | 35 local function decide_type() |
36 return "application/xmpp+xml"; | 36 return "application/xmpp+xml"; |
37 end | |
38 | |
39 local function encode(type, s) | |
40 return tostring(s); | |
37 end | 41 end |
38 | 42 |
39 local function handle_post(event) | 43 local function handle_post(event) |
40 local request, response = event.request, event.response; | 44 local request, response = event.request, event.response; |
41 if not request.headers.authorization then | 45 if not request.headers.authorization then |
83 end | 87 end |
84 return module:send_iq(payload):next( | 88 return module:send_iq(payload):next( |
85 function (result) | 89 function (result) |
86 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); | 90 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); |
87 response.headers.content_type = send_type; | 91 response.headers.content_type = send_type; |
88 return tostring(result.stanza); | 92 return encode(send_type, result.stanza); |
89 end, | 93 end, |
90 function (error) | 94 function (error) |
91 if error.context.stanza then | 95 if error.context.stanza then |
92 response.headers.content_type = send_type; | 96 response.headers.content_type = send_type; |
93 module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag()); | 97 module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag()); |
94 return tostring(error.context.stanza); | 98 return encode(send_type, error.context.stanza); |
95 else | 99 else |
96 return error; | 100 return error; |
97 end | 101 end |
98 end); | 102 end); |
99 else | 103 else |
100 local origin = {}; | 104 local origin = {}; |
101 function origin.send(stanza) | 105 function origin.send(stanza) |
102 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); | 106 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
103 response.headers.content_type = send_type; | 107 response.headers.content_type = send_type; |
104 response:send(tostring(stanza)); | 108 response:send(encode(send_type, stanza)); |
105 return true; | 109 return true; |
106 end | 110 end |
107 if module:send(payload, origin) then | 111 if module:send(payload, origin) then |
108 return 202; | 112 return 202; |
109 else | 113 else |
159 if stanza.name == "message" and stanza.attr.id and stanza:get_child("urn:xmpp:receipts", "request") then | 163 if stanza.name == "message" and stanza.attr.id and stanza:get_child("urn:xmpp:receipts", "request") then |
160 reply_needed = true; | 164 reply_needed = true; |
161 receipt = st.stanza("received", { xmlns = "urn:xmpp:receipts", id = stanza.id }); | 165 receipt = st.stanza("received", { xmlns = "urn:xmpp:receipts", id = stanza.id }); |
162 end | 166 end |
163 | 167 |
164 local request_body = tostring(stanza); | 168 local request_body = encode(send_type, stanza); |
165 | 169 |
166 -- Keep only the top level element and let the rest be GC'd | 170 -- Keep only the top level element and let the rest be GC'd |
167 stanza = st.clone(stanza, true); | 171 stanza = st.clone(stanza, true); |
168 | 172 |
169 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); | 173 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |