Explorar el Código

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 hace 5 años
padre
commit
710307725b

+ 21
- 21
resources/prosody-plugins/mod_muc_poltergeist.lua Ver fichero

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

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

+ 2
- 25
resources/prosody-plugins/util.lib.lua Ver fichero

59
     end
59
     end
60
 end
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
     if not have_async then
63
     if not have_async then
66
         module:log("error", "requires a version of Prosody with util.async");
64
         module:log("error", "requires a version of Prosody with util.async");
67
         return nil;
65
         return nil;
68
     end
66
     end
69
 
67
 
70
     local runner = async.runner;
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
     -- Grab a local response so that we can send the http response when
70
     -- Grab a local response so that we can send the http response when
93
     -- the handler is done.
71
     -- the handler is done.
94
     local response = event.response;
72
     local response = event.response;
197
 return {
175
 return {
198
     is_feature_allowed = is_feature_allowed;
176
     is_feature_allowed = is_feature_allowed;
199
     get_room_from_jid = get_room_from_jid;
177
     get_room_from_jid = get_room_from_jid;
200
-    wrap_async_run = wrap_async_run;
201
     async_handler_wrapper = async_handler_wrapper;
178
     async_handler_wrapper = async_handler_wrapper;
202
     room_jid_match_rewrite = room_jid_match_rewrite;
179
     room_jid_match_rewrite = room_jid_match_rewrite;
203
     update_presence_identity = update_presence_identity;
180
     update_presence_identity = update_presence_identity;

Loading…
Cancelar
Guardar