Software /
code /
prosody
Comparison
net/http.lua @ 11016:5176d9f727f6 0.11
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.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 08 Aug 2020 13:13:50 +0100 |
parent | 11015:355eae2f9ba8 |
child | 11017:1f41f38a92f7 |
child | 11063:30d3f6f85eb8 |
comparison
equal
deleted
inserted
replaced
11015:355eae2f9ba8 | 11016:5176d9f727f6 |
---|---|
51 local function destroy_request(request) | 51 local function destroy_request(request) |
52 local conn = request.conn; | 52 local conn = request.conn; |
53 if conn then | 53 if conn then |
54 request.conn = nil; | 54 request.conn = nil; |
55 conn:close() | 55 conn:close() |
56 end | |
57 end | |
58 | |
59 local function cancel_request(request, reason) | |
60 if request.callback then | |
61 request.callback(reason or "cancelled", 0, request); | |
62 request.callback = nil; | |
63 end | |
64 if request.conn then | |
65 destroy_request(request); | |
56 end | 66 end |
57 end | 67 end |
58 | 68 |
59 local function request_reader(request, data, err) | 69 local function request_reader(request, data, err) |
60 if not request.parser then | 70 if not request.parser then |
103 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); | 113 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); |
104 return log_if_failed(req.id, xpcall(callback, handleerr, content, code, response, request)); | 114 return log_if_failed(req.id, xpcall(callback, handleerr, content, code, response, request)); |
105 end | 115 end |
106 req.reader = request_reader; | 116 req.reader = request_reader; |
107 req.state = "status"; | 117 req.state = "status"; |
118 req.cancel = cancel_request; | |
108 | 119 |
109 requests[req.conn] = req; | 120 requests[req.conn] = req; |
110 | 121 |
111 -- Validate certificate | 122 -- Validate certificate |
112 if not req.insecure and conn:ssl() then | 123 if not req.insecure and conn:ssl() then |