From ccf99fc8d0eeee5eb08a5e4eab514ed8813abc6b Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sat, 20 Aug 2016 21:42:50 +0200 Subject: Feature added: prosody module extended to request delete token --- .../mod_http_upload_external.lua | 238 +++++++++++++-------- 1 file changed, 153 insertions(+), 85 deletions(-) (limited to 'prosody-module/mod_http_upload_external') diff --git a/prosody-module/mod_http_upload_external/mod_http_upload_external.lua b/prosody-module/mod_http_upload_external/mod_http_upload_external.lua index 768a956..757a59c 100644 --- a/prosody-module/mod_http_upload_external/mod_http_upload_external.lua +++ b/prosody-module/mod_http_upload_external/mod_http_upload_external.lua @@ -22,7 +22,7 @@ local json = require"util.json"; module:depends("disco"); -- namespace -local xmlns_http_upload = "urn:xmpp:http:upload"; +local xmlns_http_upload = "urn:xmpp:filetransfer:http"; module:add_feature(xmlns_http_upload); @@ -42,95 +42,163 @@ module:hook("iq/host/"..xmlns_http_upload..":request", function (event) origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); return true; end - -- validate - local filename = request:get_child_text("filename"); - if not filename then - origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename")); - return true; - end - local filesize = tonumber(request:get_child_text("size")); - if not filesize then - origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size")); - return true; - end + local slot_type = request.attr.type; + if not slot_type or slot_type ~= "upload" then + -- validate + local filename = request:get_child_text("filename"); + if not filename then + origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename")); + return true; + end + local filesize = tonumber(request:get_child_text("size")); + if not filesize then + origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size")); + return true; + end - local content_type = request:get_child_text("content-type"); - - -- build the body - local reqbody = "xmpp_server_key=" .. xmpp_server_key .. "&size=" .. filesize .. "&filename=" .. filename .. "&user_jid=" .. orig_from; - if content_type then - reqbody = reqbody .. "&content_type=" .. content_type; - end + local content_type = request:get_child_text("content-type"); + + -- build the body + local reqbody = "xmpp_server_key=" .. xmpp_server_key .. "&size=" .. filesize .. "&filename=" .. filename .. "&user_jid=" .. orig_from; + if content_type then + reqbody = reqbody .. "&content_type=" .. content_type; + end - -- the request - local respbody, statuscode = http.request(external_url, reqbody); - respbody = string.gsub(respbody, "\\/", "/") + -- the request + local respbody, statuscode = http.request(external_url, reqbody); + respbody = string.gsub(respbody, "\\/", "/") - local get_url = nil; - local put_url = nil; + local get_url = nil; + local put_url = nil; - -- check the response - if statuscode == 500 then - origin.send(st.error_reply(stanza, "cancel", "service-unavailable", respbody)); - elseif statuscode == 406 or statuscode == 400 or statuscode == 403 then - local errobj, pos, err = json.decode(respbody); - if err then - origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); - return true; - else - if errobj["err_code"] ~= nil and errobj["msg"] ~= nil then - if errobj.err_code == 1 then - origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg)); - return true; - elseif errobj.err_code == 2 then - origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg, - st.stanza("file-too-large", {xmlns=xmlns_http_upload}) - :tag("max-size"):text(errobj.parameters.max_file_size))); - return true; - elseif errobj.err_code == 3 then - origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg, - st.stanza("invalid-character", {xmlns=xmlns_http_upload}) - :text(errobj.parameters.invalid_character))); - return true; - elseif errobj.err_code == 4 then - origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg, - st.stanza("missing-parameter", {xmlns=xmlns_http_upload}) - :text(errobj.parameters.missing_parameter))); - return true; - else - origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "unknown err_code")); - return true; - end - elseif statuscode == 403 and errobj["msg"] ~= nil then - origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg)); - else - origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "msg or err_code not found")); - return true; - end - end - elseif statuscode == 200 then - local respobj, pos, err = json.decode(respbody); - if err then - origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); - return true; - else - if respobj["get"] ~= nil and respobj["put"] ~= nil then - get_url = respobj.get; - put_url = respobj.put; - else - origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "get or put not found")); - return true; - end - end + -- check the response + if statuscode == 500 then + origin.send(st.error_reply(stanza, "cancel", "service-unavailable", respbody)); + elseif statuscode == 406 or statuscode == 400 or statuscode == 403 then + local errobj, pos, err = json.decode(respbody); + if err then + origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); + return true; + else + if errobj["err_code"] ~= nil and errobj["msg"] ~= nil then + if errobj.err_code == 1 then + origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg)); + return true; + elseif errobj.err_code == 2 then + origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg, + st.stanza("file-too-large", {xmlns=xmlns_http_upload}) + :tag("max-size"):text(errobj.parameters.max_file_size))); + return true; + elseif errobj.err_code == 3 then + origin.send(st.error_reply(stanza, "modify", "not-acceptable", errobj.msg, + st.stanza("invalid-character", {xmlns=xmlns_http_upload}) + :text(errobj.parameters.invalid_character))); + return true; + elseif errobj.err_code == 4 then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg, + st.stanza("missing-parameter", {xmlns=xmlns_http_upload}) + :text(errobj.parameters.missing_parameter))); + return true; + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "unknown err_code")); + return true; + end + elseif statuscode == 403 and errobj["msg"] ~= nil then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg)); + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "msg or err_code not found")); + return true; + end + end + elseif statuscode == 200 then + local respobj, pos, err = json.decode(respbody); + if err then + origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); + return true; + else + if respobj["get"] ~= nil and respobj["put"] ~= nil then + get_url = respobj.get; + put_url = respobj.put; + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "get or put not found")); + return true; + end + end + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody)); + return true; + end + + local reply = st.reply(stanza); + reply:tag("slot", { xmlns = xmlns_http_upload }); + reply:tag("get"):text(get_url):up(); + reply:tag("put"):text(put_url):up(); + origin.send(reply); + elseif slot_type ~= "delete" then + -- validate + local fileurl = request:get_child_text("fileurl"); + if not fileurl then + origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid fileurl")); + return true; + end + -- build the body + local reqbody = "xmpp_server_key=" .. xmpp_server_key .. "&size=" .. filesize .. "&filename=" .. filename .. "&user_jid=" .. orig_from; + -- the request + local respbody, statuscode = http.request(external_url, reqbody); + respbody = string.gsub(respbody, "\\/", "/") + + local delete_token = nil; + -- check the response + if statuscode == 500 then + origin.send(st.error_reply(stanza, "cancel", "service-unavailable", respbody)); + elseif statuscode == 406 or statuscode == 400 or statuscode == 403 then + local errobj, pos, err = json.decode(respbody); + if err then + origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); + return true; + else + if errobj["err_code"] ~= nil and errobj["msg"] ~= nil then + if errobj.err_code == 4 then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg, + st.stanza("missing-parameter", {xmlns=xmlns_http_upload}) + :text(errobj.parameters.missing_parameter))); + return true; + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "unknown err_code")); + return true; + end + elseif statuscode == 403 and errobj["msg"] ~= nil then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", errobj.msg)); + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "msg or err_code not found")); + return true; + end + end + elseif statuscode == 200 then + local respobj, pos, err = json.decode(respbody); + if err then + origin.send(st.error_reply(stanza, "wait", "internal-server-error", err)); + return true; + else + if respobj["deletetoken"] ~= nil then + delete_token = respobj.deletetoken; + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "deletetoken not found")); + return true; + end + end + else + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody)); + return true; + end + + local reply = st.reply(stanza); + reply:tag("slot", { xmlns = xmlns_http_upload }); + reply:tag("deletetoken"):text(deletetoken):up(); + origin.send(reply); else - origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody)); - return true; + origin.send(st.error_reply(stanza, "cancel", "undefined-condition", "status code: " .. statuscode .. " response: " ..respbody)); + return true; end - - local reply = st.reply(stanza); - reply:tag("slot", { xmlns = xmlns_http_upload }); - reply:tag("get"):text(get_url):up(); - reply:tag("put"):text(put_url):up(); - origin.send(reply); return true; end); -- cgit v1.2.3