Browse Source

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
master
Aaron van Meerten 5 years ago
parent
commit
710307725b

+ 21
- 21
resources/prosody-plugins/mod_muc_poltergeist.lua View File

@@ -11,7 +11,7 @@ if not have_async then
11 11
     return;
12 12
 end
13 13
 
14
-local wrap_async_run = module:require "util".wrap_async_run;
14
+local async_handler_wrapper = module:require "util".async_handler_wrapper;
15 15
 
16 16
 -- Options
17 17
 local poltergeist_component
@@ -175,7 +175,7 @@ module:hook(
175 175
 -- @return GET response, containing a json with response details
176 176
 function handle_create_poltergeist (event)
177 177
     if (not event.request.url.query) then
178
-        return 400;
178
+        return { status_code = 400; };
179 179
     end
180 180
 
181 181
     local params = parse(event.request.url.query);
@@ -189,7 +189,7 @@ function handle_create_poltergeist (event)
189 189
     local session = {};
190 190
 
191 191
     if not verify_token(params["token"], room_name, group, session) then
192
-        return 403;
192
+        return { status_code = 403; };
193 193
     end
194 194
 
195 195
     -- If the provided room conference doesn't exist then we
@@ -197,7 +197,7 @@ function handle_create_poltergeist (event)
197 197
     local room = get_room(room_name, group);
198 198
     if (not room) then
199 199
         log("error", "no room found %s", room_name);
200
-        return 404;
200
+        return { status_code = 404; };
201 201
     end
202 202
 
203 203
     -- If the poltergiest is already in the conference then it will
@@ -210,7 +210,7 @@ function handle_create_poltergeist (event)
210 210
             username,
211 211
             room_name
212 212
         );
213
-        return 202;
213
+        return { status_code = 202; };
214 214
     end
215 215
 
216 216
     local context = {
@@ -228,7 +228,7 @@ function handle_create_poltergeist (event)
228 228
     end
229 229
 
230 230
     poltergeist.add_to_muc(room, user_id, name, avatar, context, status, resources)
231
-    return 200;
231
+    return { status_code = 200; };
232 232
 end
233 233
 
234 234
 --- Handles request for updating poltergeists status
@@ -236,7 +236,7 @@ end
236 236
 -- @return GET response, containing a json with response details
237 237
 function handle_update_poltergeist (event)
238 238
     if (not event.request.url.query) then
239
-        return 400;
239
+        return { status_code = 400; };
240 240
     end
241 241
 
242 242
     local params = parse(event.request.url.query);
@@ -252,18 +252,18 @@ function handle_update_poltergeist (event)
252 252
     end
253 253
 
254 254
     if not verify_token(params["token"], room_name, group, {}) then
255
-        return 403;
255
+        return { status_code = 403; };
256 256
     end
257 257
 
258 258
     local room = get_room(room_name, group);
259 259
     if (not room) then
260 260
         log("error", "no room found %s", room_name);
261
-        return 404;
261
+        return { status_code = 404; };
262 262
     end
263 263
 
264 264
     local username = poltergeist.get_username(room, user_id);
265 265
     if (not username) then
266
-        return 404;
266
+        return { status_code = 404; };
267 267
     end
268 268
 
269 269
     local call_details = {
@@ -273,11 +273,11 @@ function handle_update_poltergeist (event)
273 273
 
274 274
     local nick = poltergeist.create_nick(username);
275 275
     if (not poltergeist.occupies(room, nick)) then
276
-       return 404;
276
+       return { status_code = 404; };
277 277
     end
278 278
 
279 279
     poltergeist.update(room, nick, status, call_details);
280
-    return 200;
280
+    return { status_code = 200; };
281 281
 end
282 282
 
283 283
 --- Handles remove poltergeists
@@ -285,7 +285,7 @@ end
285 285
 -- @return GET response, containing a json with response details
286 286
 function handle_remove_poltergeist (event)
287 287
     if (not event.request.url.query) then
288
-        return 400;
288
+        return { status_code = 400; };
289 289
     end
290 290
 
291 291
     local params = parse(event.request.url.query);
@@ -294,27 +294,27 @@ function handle_remove_poltergeist (event)
294 294
     local group = params["group"];
295 295
 
296 296
     if not verify_token(params["token"], room_name, group, {}) then
297
-        return 403;
297
+        return { status_code = 403; };
298 298
     end
299 299
 
300 300
     local room = get_room(room_name, group);
301 301
     if (not room) then
302 302
         log("error", "no room found %s", room_name);
303
-        return 404;
303
+        return { status_code = 404; };
304 304
     end
305 305
 
306 306
     local username = poltergeist.get_username(room, user_id);
307 307
     if (not username) then
308
-        return 404;
308
+        return { status_code = 404; };
309 309
     end
310 310
 
311 311
     local nick = poltergeist.create_nick(username);
312 312
     if (not poltergeist.occupies(room, nick)) then
313
-       return 404;
313
+       return { status_code = 404; };
314 314
     end
315 315
 
316 316
     poltergeist.remove(room, nick, false);
317
-    return 200;
317
+    return { status_code = 200; };
318 318
 end
319 319
 
320 320
 log("info", "Loading poltergeist service");
@@ -323,8 +323,8 @@ module:provides("http", {
323 323
     default_path = "/";
324 324
     name = "poltergeist";
325 325
     route = {
326
-        ["GET /poltergeist/create"] = function (event) return wrap_async_run(event,handle_create_poltergeist) end;
327
-        ["GET /poltergeist/update"] = function (event) return wrap_async_run(event,handle_update_poltergeist) end;
328
-        ["GET /poltergeist/remove"] = function (event) return wrap_async_run(event,handle_remove_poltergeist) end;
326
+        ["GET /poltergeist/create"] = function (event) return async_handler_wrapper(event,handle_create_poltergeist) end;
327
+        ["GET /poltergeist/update"] = function (event) return async_handler_wrapper(event,handle_update_poltergeist) end;
328
+        ["GET /poltergeist/remove"] = function (event) return async_handler_wrapper(event,handle_remove_poltergeist) end;
329 329
     };
330 330
 });

+ 11
- 11
resources/prosody-plugins/mod_muc_size.lua View File

@@ -14,7 +14,7 @@ if not have_async then
14 14
     return;
15 15
 end
16 16
 
17
-local wrap_async_run = module:require "util".wrap_async_run;
17
+local async_handler_wrapper = module:require "util".async_handler_wrapper;
18 18
 
19 19
 local tostring = tostring;
20 20
 local neturl = require "net.url";
@@ -79,7 +79,7 @@ end
79 79
 --         tha value is without counting the focus.
80 80
 function handle_get_room_size(event)
81 81
     if (not event.request.url.query) then
82
-        return 400;
82
+        return { status_code = 400; };
83 83
     end
84 84
 
85 85
 	local params = parse(event.request.url.query);
@@ -95,7 +95,7 @@ function handle_get_room_size(event)
95 95
     end
96 96
 
97 97
     if not verify_token(params["token"], room_address) then
98
-        return 403;
98
+        return { status_code = 403; };
99 99
     end
100 100
 
101 101
 	local room = get_room_from_jid(room_address);
@@ -112,14 +112,14 @@ function handle_get_room_size(event)
112 112
             "there are %s occupants in room", tostring(participant_count));
113 113
 	else
114 114
 		log("debug", "no such room exists");
115
-		return 404;
115
+		return { status_code = 404; };
116 116
 	end
117 117
 
118 118
 	if participant_count > 1 then
119 119
 		participant_count = participant_count - 1;
120 120
 	end
121 121
 
122
-	return [[{"participants":]]..participant_count..[[}]];
122
+	return { status_code = 200; body = [[{"participants":]]..participant_count..[[}]] };
123 123
 end
124 124
 
125 125
 --- Handles request for retrieving the room participants details
@@ -127,7 +127,7 @@ end
127 127
 -- @return GET response, containing a json with participants details
128 128
 function handle_get_room (event)
129 129
     if (not event.request.url.query) then
130
-        return 400;
130
+        return { status_code = 400; };
131 131
     end
132 132
 
133 133
 	local params = parse(event.request.url.query);
@@ -142,7 +142,7 @@ function handle_get_room (event)
142 142
     end
143 143
 
144 144
     if not verify_token(params["token"], room_address) then
145
-        return 403;
145
+        return { status_code = 403; };
146 146
     end
147 147
 
148 148
 	local room = get_room_from_jid(room_address);
@@ -173,14 +173,14 @@ function handle_get_room (event)
173 173
             "there are %s occupants in room", tostring(participant_count));
174 174
 	else
175 175
 		log("debug", "no such room exists");
176
-		return 404;
176
+		return { status_code = 404; };
177 177
 	end
178 178
 
179 179
 	if participant_count > 1 then
180 180
 		participant_count = participant_count - 1;
181 181
 	end
182 182
 
183
-	return json.encode(occupants_json);
183
+	return { status_code = 200; body = json.encode(occupants_json); };
184 184
 end;
185 185
 
186 186
 function module.load()
@@ -188,9 +188,9 @@ function module.load()
188 188
 	module:provides("http", {
189 189
 		default_path = "/";
190 190
 		route = {
191
-			["GET room-size"] = function (event) return wrap_async_run(event,handle_get_room_size) end;
191
+			["GET room-size"] = function (event) return async_handler_wrapper(event,handle_get_room_size) end;
192 192
 			["GET sessions"] = function () return tostring(it.count(it.keys(prosody.full_sessions))); end;
193
-			["GET room"] = function (event) return wrap_async_run(event,handle_get_room) end;
193
+			["GET room"] = function (event) return async_handler_wrapper(event,handle_get_room) end;
194 194
 		};
195 195
 	});
196 196
 end

+ 2
- 25
resources/prosody-plugins/util.lib.lua View File

@@ -59,36 +59,14 @@ function get_room_from_jid(room_jid)
59 59
     end
60 60
 end
61 61
 
62
-
63
-function wrap_async_run(event,handler)
64
-    local have_async = pcall(require, "util.async");
62
+function async_handler_wrapper(event, handler)
65 63
     if not have_async then
66 64
         module:log("error", "requires a version of Prosody with util.async");
67 65
         return nil;
68 66
     end
69 67
 
70 68
     local runner = async.runner;
71
-    -- Grab a local response so that we can send the http response when
72
-    -- the handler is done.
73
-    local response = event.response;
74
-    local async_func = runner(function (event)
75
-        local result = handler(event);
76
-        -- if it is a number, it is a status code, else it is the body
77
-        -- result we will return
78
-        if (tonumber(result) ~= nil) then
79
-            response.status_code = result;
80
-        else
81
-            response.body = result;
82
-        end;
83
-        -- Send the response to the waiting http client.
84
-        response:send();
85
-    end)
86
-    async_func:run(event)
87
-    -- return true to keep the client http connection open.
88
-    return true;
89
-end
90
-
91
-function async_handler_wrapper(event, handler)
69
+    
92 70
     -- Grab a local response so that we can send the http response when
93 71
     -- the handler is done.
94 72
     local response = event.response;
@@ -197,7 +175,6 @@ end
197 175
 return {
198 176
     is_feature_allowed = is_feature_allowed;
199 177
     get_room_from_jid = get_room_from_jid;
200
-    wrap_async_run = wrap_async_run;
201 178
     async_handler_wrapper = async_handler_wrapper;
202 179
     room_jid_match_rewrite = room_jid_match_rewrite;
203 180
     update_presence_identity = update_presence_identity;

Loading…
Cancel
Save