aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-08-08 13:13:50 +0100
committerMatthew Wild <mwild1@gmail.com>2020-08-08 13:13:50 +0100
commitf3796f6c5261135d68ebd345690836e40fd6b562 (patch)
tree29127ae0b64bc5de6a449bb04ca373fd64039bec
parentcf70cc45479dc7cefee0c4b18c5712e97b7f7aa1 (diff)
downloadprosody-f3796f6c5261135d68ebd345690836e40fd6b562.tar.gz
prosody-f3796f6c5261135d68ebd345690836e40fd6b562.zip
net.http: Add request:cancel() method
This is a new API that should be used in preference to http.destroy_request() when possible, as it ensures the callback is always called (with an error of course). APIs that have edge-cases where they don't call callbacks have, from experience, shown to be difficult to work with and often lead to unintentional leaks when the callback was expected to free up certain resources.
-rw-r--r--net/http.lua11
1 files changed, 11 insertions, 0 deletions
diff --git a/net/http.lua b/net/http.lua
index 14107bf4..ae9d2974 100644
--- a/net/http.lua
+++ b/net/http.lua
@@ -56,6 +56,16 @@ local function destroy_request(request)
end
end
+local function cancel_request(request, reason)
+ if request.callback then
+ request.callback(reason or "cancelled", 0, request);
+ request.callback = nil;
+ end
+ if request.conn then
+ destroy_request(request);
+ end
+end
+
local function request_reader(request, data, err)
if not request.parser then
local function error_cb(reason)
@@ -105,6 +115,7 @@ function listener.onconnect(conn)
end
req.reader = request_reader;
req.state = "status";
+ req.cancel = cancel_request;
requests[req.conn] = req;