mod_rest/mod_rest.lua
changeset 3807 dc2b5a412286
parent 3806 f88e07630e4e
child 3808 d74509cd35fb
equal deleted inserted replaced
3806:f88e07630e4e 3807:dc2b5a412286
     1 -- RESTful API
     1 -- RESTful API
     2 --
     2 --
     3 -- Copyright (c) 2019 Kim Alvefur
     3 -- Copyright (c) 2019-2020 Kim Alvefur
     4 --
     4 --
     5 -- This file is MIT/X11 licensed.
     5 -- This file is MIT/X11 licensed.
     6 
     6 
     7 local errors = require "util.error";
     7 local errors = require "util.error";
     8 local http = require "net.http";
     8 local http = require "net.http";
    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"],
   209 
   214 
   210 				if receipt then
   215 				if receipt then
   211 					reply:add_direct_child(receipt);
   216 					reply:add_direct_child(receipt);
   212 				end
   217 				end
   213 
   218 
       
   219 				module:log("debug", "Received[rest]: %s", reply:top_tag());
       
   220 
   214 				origin.send(reply);
   221 				origin.send(reply);
   215 			end);
   222 			end);
   216 
   223 
   217 		return true;
   224 		return true;
   218 	end
   225 	end