Comparison

net/http.lua @ 11185:409ce7686c11

net.http: Add support for streaming chunked/large responses
author Matthew Wild <mwild1@gmail.com>
date Wed, 21 Oct 2020 10:40:11 +0100
parent 11068:988ddd57e851
child 11220:9b25eecde9e6
comparison
equal deleted inserted replaced
11184:2ede7f43ccfe 11185:409ce7686c11
81 if not data then 81 if not data then
82 error_cb(err); 82 error_cb(err);
83 return; 83 return;
84 end 84 end
85 85
86 local finalize_sink;
86 local function success_cb(r) 87 local function success_cb(r)
88 if r.partial then
89 -- Request should be streamed
90 log("debug", "Request '%s': partial response (%s%s)",
91 request.id,
92 r.chunked and "chunked, " or "",
93 r.body_length and ("%d bytes"):format(r.body_length) or "unknown length"
94 );
95 if request.streaming_handler then
96 log("debug", "Request '%s': Streaming via handler");
97 r.body_sink, finalize_sink = request.streaming_handler(r);
98 end
99 return;
100 elseif finalize_sink then
101 log("debug", "Request '%s': Finalizing response stream");
102 finalize_sink(r);
103 end
87 if request.callback then 104 if request.callback then
88 request.callback(r.body, r.code, r, request); 105 request.callback(r.body, r.code, r, request);
89 request.callback = nil; 106 request.callback = nil;
90 end 107 end
91 destroy_request(request); 108 destroy_request(request);
254 headers[k] = v; 271 headers[k] = v;
255 end 272 end
256 end 273 end
257 req.insecure = ex.insecure; 274 req.insecure = ex.insecure;
258 req.suppress_errors = ex.suppress_errors; 275 req.suppress_errors = ex.suppress_errors;
276 req.streaming_handler = ex.streaming_handler;
259 end 277 end
260 278
261 log("debug", "Making %s %s request '%s' to %s", req.scheme:upper(), method or "GET", req.id, (ex and ex.suppress_url and host_header) or u); 279 log("debug", "Making %s %s request '%s' to %s", req.scheme:upper(), method or "GET", req.id, (ex and ex.suppress_url and host_header) or u);
262 280
263 -- Attach to request object 281 -- Attach to request object