# HG changeset patch # User Matthew Wild # Date 1603273211 -3600 # Node ID 409ce7686c112f1b4b95fbbf8cca81e3e298fe08 # Parent 2ede7f43ccfebd3719843e81ae077652e0af11f3 net.http: Add support for streaming chunked/large responses diff -r 2ede7f43ccfe -r 409ce7686c11 net/http.lua --- a/net/http.lua Wed Oct 21 10:34:16 2020 +0100 +++ b/net/http.lua Wed Oct 21 10:40:11 2020 +0100 @@ -83,7 +83,24 @@ return; end + local finalize_sink; local function success_cb(r) + if r.partial then + -- Request should be streamed + log("debug", "Request '%s': partial response (%s%s)", + request.id, + r.chunked and "chunked, " or "", + r.body_length and ("%d bytes"):format(r.body_length) or "unknown length" + ); + if request.streaming_handler then + log("debug", "Request '%s': Streaming via handler"); + r.body_sink, finalize_sink = request.streaming_handler(r); + end + return; + elseif finalize_sink then + log("debug", "Request '%s': Finalizing response stream"); + finalize_sink(r); + end if request.callback then request.callback(r.body, r.code, r, request); request.callback = nil; @@ -256,6 +273,7 @@ end req.insecure = ex.insecure; req.suppress_errors = ex.suppress_errors; + req.streaming_handler = ex.streaming_handler; end 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);