瀏覽代碼

Creating a new async prosody http wrapper.

master
jmacelroy 6 年之前
父節點
當前提交
944cf4272d
共有 1 個檔案被更改,包括 35 行新增0 行删除
  1. 35
    0
      resources/prosody-plugins/util.lib.lua

+ 35
- 0
resources/prosody-plugins/util.lib.lua 查看文件

@@ -82,6 +82,40 @@ function wrap_async_run(event,handler)
82 82
     return true;
83 83
 end
84 84
 
85
+function async_handler_wrapper(event, handler)
86
+    -- Grab a local response so that we can send the http response when
87
+    -- the handler is done.
88
+    local response = event.response;
89
+    local async_func = runner(
90
+        function (event)
91
+            local result = handler(event)
92
+
93
+            -- If there is a status code in the result from the
94
+            -- wrapped handler then add it to the response.
95
+            if tonumber(result.status_code) ~= nil then
96
+                response.status_code = result.status_code
97
+            end
98
+
99
+            -- If there are headers in the result from the
100
+            -- wrapped handler then add them to the response.
101
+            if result.headers ~= nil then
102
+                response.headers = result.headers
103
+            end
104
+
105
+            -- Send the response to the waiting http client with
106
+            -- or without the body from the wrapped handler.
107
+            if result.body ~= nil then
108
+                response:send(result.body)
109
+            else
110
+                response:send();
111
+            end
112
+        end
113
+    )
114
+    async_func:run(event)
115
+    -- return true to keep the client http connection open.
116
+    return true;
117
+end
118
+
85 119
 --- Updates presence stanza, by adding identity node
86 120
 -- @param stanza the presence stanza
87 121
 -- @param user the user to which presence we are updating identity
@@ -158,6 +192,7 @@ return {
158 192
     is_feature_allowed = is_feature_allowed;
159 193
     get_room_from_jid = get_room_from_jid;
160 194
     wrap_async_run = wrap_async_run;
195
+    async_handler_wrapper = async_handler_wrapper;
161 196
     room_jid_match_rewrite = room_jid_match_rewrite;
162 197
     update_presence_identity = update_presence_identity;
163 198
 };

Loading…
取消
儲存