From 710307725b138babfab3300322a848acd6facec8 Mon Sep 17 00:00:00 2001 From: Aaron van Meerten Date: Thu, 23 Jan 2020 13:31:05 -0600 Subject: [PATCH] fixes async_handler_wrapper (#5001) * fixes async_handler_wrapper adds missing runner variable from async to async_handler_wrapper removes redundant have_async definition in wrap_async_run, defined at top of module * only use async handler wrapper, remove async_wrap_run --- .../prosody-plugins/mod_muc_poltergeist.lua | 42 +++++++++---------- resources/prosody-plugins/mod_muc_size.lua | 22 +++++----- resources/prosody-plugins/util.lib.lua | 27 +----------- 3 files changed, 34 insertions(+), 57 deletions(-) diff --git a/resources/prosody-plugins/mod_muc_poltergeist.lua b/resources/prosody-plugins/mod_muc_poltergeist.lua index fe03dc5bd..4d6538bd8 100644 --- a/resources/prosody-plugins/mod_muc_poltergeist.lua +++ b/resources/prosody-plugins/mod_muc_poltergeist.lua @@ -11,7 +11,7 @@ if not have_async then return; end -local wrap_async_run = module:require "util".wrap_async_run; +local async_handler_wrapper = module:require "util".async_handler_wrapper; -- Options local poltergeist_component @@ -175,7 +175,7 @@ module:hook( -- @return GET response, containing a json with response details function handle_create_poltergeist (event) if (not event.request.url.query) then - return 400; + return { status_code = 400; }; end local params = parse(event.request.url.query); @@ -189,7 +189,7 @@ function handle_create_poltergeist (event) local session = {}; if not verify_token(params["token"], room_name, group, session) then - return 403; + return { status_code = 403; }; end -- If the provided room conference doesn't exist then we @@ -197,7 +197,7 @@ function handle_create_poltergeist (event) local room = get_room(room_name, group); if (not room) then log("error", "no room found %s", room_name); - return 404; + return { status_code = 404; }; end -- If the poltergiest is already in the conference then it will @@ -210,7 +210,7 @@ function handle_create_poltergeist (event) username, room_name ); - return 202; + return { status_code = 202; }; end local context = { @@ -228,7 +228,7 @@ function handle_create_poltergeist (event) end poltergeist.add_to_muc(room, user_id, name, avatar, context, status, resources) - return 200; + return { status_code = 200; }; end --- Handles request for updating poltergeists status @@ -236,7 +236,7 @@ end -- @return GET response, containing a json with response details function handle_update_poltergeist (event) if (not event.request.url.query) then - return 400; + return { status_code = 400; }; end local params = parse(event.request.url.query); @@ -252,18 +252,18 @@ function handle_update_poltergeist (event) end if not verify_token(params["token"], room_name, group, {}) then - return 403; + return { status_code = 403; }; end local room = get_room(room_name, group); if (not room) then log("error", "no room found %s", room_name); - return 404; + return { status_code = 404; }; end local username = poltergeist.get_username(room, user_id); if (not username) then - return 404; + return { status_code = 404; }; end local call_details = { @@ -273,11 +273,11 @@ function handle_update_poltergeist (event) local nick = poltergeist.create_nick(username); if (not poltergeist.occupies(room, nick)) then - return 404; + return { status_code = 404; }; end poltergeist.update(room, nick, status, call_details); - return 200; + return { status_code = 200; }; end --- Handles remove poltergeists @@ -285,7 +285,7 @@ end -- @return GET response, containing a json with response details function handle_remove_poltergeist (event) if (not event.request.url.query) then - return 400; + return { status_code = 400; }; end local params = parse(event.request.url.query); @@ -294,27 +294,27 @@ function handle_remove_poltergeist (event) local group = params["group"]; if not verify_token(params["token"], room_name, group, {}) then - return 403; + return { status_code = 403; }; end local room = get_room(room_name, group); if (not room) then log("error", "no room found %s", room_name); - return 404; + return { status_code = 404; }; end local username = poltergeist.get_username(room, user_id); if (not username) then - return 404; + return { status_code = 404; }; end local nick = poltergeist.create_nick(username); if (not poltergeist.occupies(room, nick)) then - return 404; + return { status_code = 404; }; end poltergeist.remove(room, nick, false); - return 200; + return { status_code = 200; }; end log("info", "Loading poltergeist service"); @@ -323,8 +323,8 @@ module:provides("http", { default_path = "/"; name = "poltergeist"; route = { - ["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end; - ["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end; - ["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end; + ["GET /poltergeist/create"] = function (event) return async_handler_wrapper(event,handle_create_poltergeist) end; + ["GET /poltergeist/update"] = function (event) return async_handler_wrapper(event,handle_update_poltergeist) end; + ["GET /poltergeist/remove"] = function (event) return async_handler_wrapper(event,handle_remove_poltergeist) end; }; }); diff --git a/resources/prosody-plugins/mod_muc_size.lua b/resources/prosody-plugins/mod_muc_size.lua index abe7269bc..f87f827ee 100644 --- a/resources/prosody-plugins/mod_muc_size.lua +++ b/resources/prosody-plugins/mod_muc_size.lua @@ -14,7 +14,7 @@ if not have_async then return; end -local wrap_async_run = module:require "util".wrap_async_run; +local async_handler_wrapper = module:require "util".async_handler_wrapper; local tostring = tostring; local neturl = require "net.url"; @@ -79,7 +79,7 @@ end -- tha value is without counting the focus. function handle_get_room_size(event) if (not event.request.url.query) then - return 400; + return { status_code = 400; }; end local params = parse(event.request.url.query); @@ -95,7 +95,7 @@ function handle_get_room_size(event) end if not verify_token(params["token"], room_address) then - return 403; + return { status_code = 403; }; end local room = get_room_from_jid(room_address); @@ -112,14 +112,14 @@ function handle_get_room_size(event) "there are %s occupants in room", tostring(participant_count)); else log("debug", "no such room exists"); - return 404; + return { status_code = 404; }; end if participant_count > 1 then participant_count = participant_count - 1; end - return [[{"participants":]]..participant_count..[[}]]; + return { status_code = 200; body = [[{"participants":]]..participant_count..[[}]] }; end --- Handles request for retrieving the room participants details @@ -127,7 +127,7 @@ end -- @return GET response, containing a json with participants details function handle_get_room (event) if (not event.request.url.query) then - return 400; + return { status_code = 400; }; end local params = parse(event.request.url.query); @@ -142,7 +142,7 @@ function handle_get_room (event) end if not verify_token(params["token"], room_address) then - return 403; + return { status_code = 403; }; end local room = get_room_from_jid(room_address); @@ -173,14 +173,14 @@ function handle_get_room (event) "there are %s occupants in room", tostring(participant_count)); else log("debug", "no such room exists"); - return 404; + return { status_code = 404; }; end if participant_count > 1 then participant_count = participant_count - 1; end - return json.encode(occupants_json); + return { status_code = 200; body = json.encode(occupants_json); }; end; function module.load() @@ -188,9 +188,9 @@ function module.load() module:provides("http", { default_path = "/"; route = { - ["GET room-size"] = function (event) return wrap_async_run(event,handle_get_room_size) end; + ["GET room-size"] = function (event) return async_handler_wrapper(event,handle_get_room_size) end; ["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end; - ["GET room"] = function (event) return wrap_async_run(event,handle_get_room) end; + ["GET room"] = function (event) return async_handler_wrapper(event,handle_get_room) end; }; }); end diff --git a/resources/prosody-plugins/util.lib.lua b/resources/prosody-plugins/util.lib.lua index 70e00a45a..086cc7ff9 100644 --- a/resources/prosody-plugins/util.lib.lua +++ b/resources/prosody-plugins/util.lib.lua @@ -59,36 +59,14 @@ function get_room_from_jid(room_jid) end end - -function wrap_async_run(event,handler) - local have_async = pcall(require, "util.async"); +function async_handler_wrapper(event, handler) if not have_async then module:log("error", "requires a version of Prosody with util.async"); return nil; end local runner = async.runner; - -- Grab a local response so that we can send the http response when - -- the handler is done. - local response = event.response; - local async_func = runner(function (event) - local result = handler(event); - -- if it is a number, it is a status code, else it is the body - -- result we will return - if (tonumber(result) ~= nil) then - response.status_code = result; - else - response.body = result; - end; - -- Send the response to the waiting http client. - response:send(); - end) - async_func:run(event) - -- return true to keep the client http connection open. - return true; -end - -function async_handler_wrapper(event, handler) + -- Grab a local response so that we can send the http response when -- the handler is done. local response = event.response; @@ -197,7 +175,6 @@ end return { is_feature_allowed = is_feature_allowed; get_room_from_jid = get_room_from_jid; - wrap_async_run = wrap_async_run; async_handler_wrapper = async_handler_wrapper; room_jid_match_rewrite = room_jid_match_rewrite; update_presence_identity = update_presence_identity;