Mercurial > prosody-modules
comparison mod_rest/mod_rest.lua @ 3811:eb25110696cd
mod_rest: Factor out response content type selection
Preparation for future addition of other encodings
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Jan 2020 16:01:25 +0100 |
parents | 91ff86fc3b20 |
children | f027b8b1e794 |
comparison
equal
deleted
inserted
replaced
3810:91ff86fc3b20 | 3811:eb25110696cd |
---|---|
28 return xml.parse(data); | 28 return xml.parse(data); |
29 elseif mimetype == "text/plain" then | 29 elseif mimetype == "text/plain" then |
30 return st.message({ type = "chat" }, data); | 30 return st.message({ type = "chat" }, data); |
31 end | 31 end |
32 return nil, "unknown-payload-type"; | 32 return nil, "unknown-payload-type"; |
33 end | |
34 | |
35 local function decide_type() | |
36 return "application/xmpp+xml"; | |
33 end | 37 end |
34 | 38 |
35 local function handle_post(event) | 39 local function handle_post(event) |
36 local request, response = event.request, event.response; | 40 local request, response = event.request, event.response; |
37 if not request.headers.authorization then | 41 if not request.headers.authorization then |
70 id = payload.attr.id or id.medium(), | 74 id = payload.attr.id or id.medium(), |
71 type = payload.attr.type, | 75 type = payload.attr.type, |
72 ["xml:lang"] = payload.attr["xml:lang"], | 76 ["xml:lang"] = payload.attr["xml:lang"], |
73 }; | 77 }; |
74 module:log("debug", "Received[rest]: %s", payload:top_tag()); | 78 module:log("debug", "Received[rest]: %s", payload:top_tag()); |
79 local send_type = decide_type(request.headers.accept) | |
75 if payload.name == "iq" then | 80 if payload.name == "iq" then |
76 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then | 81 if payload.attr.type ~= "get" and payload.attr.type ~= "set" then |
77 return errors.new({ code = 422, text = "'iq' stanza must be of type 'get' or 'set'" }); | 82 return errors.new({ code = 422, text = "'iq' stanza must be of type 'get' or 'set'" }); |
78 end | 83 end |
79 return module:send_iq(payload):next( | 84 return module:send_iq(payload):next( |
80 function (result) | 85 function (result) |
81 response.headers.content_type = "application/xmpp+xml"; | |
82 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); | 86 module:log("debug", "Sending[rest]: %s", result.stanza:top_tag()); |
87 response.headers.content_type = send_type; | |
83 return tostring(result.stanza); | 88 return tostring(result.stanza); |
84 end, | 89 end, |
85 function (error) | 90 function (error) |
86 if error.context.stanza then | 91 if error.context.stanza then |
87 response.headers.content_type = "application/xmpp+xml"; | 92 response.headers.content_type = send_type; |
88 module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag()); | 93 module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag()); |
89 return tostring(error.context.stanza); | 94 return tostring(error.context.stanza); |
90 else | 95 else |
91 return error; | 96 return error; |
92 end | 97 end |
93 end); | 98 end); |
94 else | 99 else |
95 local origin = {}; | 100 local origin = {}; |
96 function origin.send(stanza) | 101 function origin.send(stanza) |
97 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); | 102 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
103 response.headers.content_type = send_type; | |
98 response:send(tostring(stanza)); | 104 response:send(tostring(stanza)); |
99 return true; | 105 return true; |
100 end | 106 end |
101 response.headers.content_type = "application/xmpp+xml"; | |
102 if module:send(payload, origin) then | 107 if module:send(payload, origin) then |
103 return 202; | 108 return 202; |
104 else | 109 else |
105 return 500; | 110 return 500; |
106 end | 111 end |
116 }); | 121 }); |
117 | 122 |
118 -- Forward stanzas from XMPP to HTTP and return any reply | 123 -- Forward stanzas from XMPP to HTTP and return any reply |
119 local rest_url = module:get_option_string("rest_callback_url", nil); | 124 local rest_url = module:get_option_string("rest_callback_url", nil); |
120 if rest_url then | 125 if rest_url then |
126 local send_type = module:get_option_string("rest_callback_content_type", "application/xmpp+xml"); | |
121 | 127 |
122 local code2err = { | 128 local code2err = { |
123 [400] = { condition = "bad-request"; type = "modify" }; | 129 [400] = { condition = "bad-request"; type = "modify" }; |
124 [401] = { condition = "not-authorized"; type = "auth" }; | 130 [401] = { condition = "not-authorized"; type = "auth" }; |
125 [402] = { condition = "not-authorized"; type = "auth" }; | 131 [402] = { condition = "not-authorized"; type = "auth" }; |
162 | 168 |
163 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); | 169 module:log("debug", "Sending[rest]: %s", stanza:top_tag()); |
164 http.request(rest_url, { | 170 http.request(rest_url, { |
165 body = request_body, | 171 body = request_body, |
166 headers = { | 172 headers = { |
167 ["Content-Type"] = "application/xmpp+xml", | 173 ["Content-Type"] = send_type, |
168 ["Content-Language"] = stanza.attr["xml:lang"], | 174 ["Content-Language"] = stanza.attr["xml:lang"], |
169 Accept = "application/xmpp+xml, text/plain", | 175 Accept = "application/xmpp+xml, text/plain", |
170 }, | 176 }, |
171 }, function (body, code, response) | 177 }, function (body, code, response) |
172 if (code == 202 or code == 204) and not reply_needed then | 178 if (code == 202 or code == 204) and not reply_needed then |