[lxc-devel] [lxd/master] Stream multi-part file responses

stgraber on Github lxc-bot at linuxcontainers.org
Thu Jun 25 21:04:08 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 491 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200625/67dc2687/attachment.bin>
-------------- next part --------------
From f895b0c937a27d27ac82cfd34492f4b0d0b09054 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 25 Jun 2020 17:02:39 -0400
Subject: [PATCH 1/2] client: Handle unknown image sizes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 client/lxd_images.go | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/client/lxd_images.go b/client/lxd_images.go
index 0203b0ebbf..1d1932dcc3 100644
--- a/client/lxd_images.go
+++ b/client/lxd_images.go
@@ -180,15 +180,25 @@ func lxdDownloadImage(fingerprint string, uri string, userAgent string, client *
 	// Handle the data
 	body := response.Body
 	if req.ProgressHandler != nil {
-		body = &ioprogress.ProgressReader{
+		reader := &ioprogress.ProgressReader{
 			ReadCloser: response.Body,
 			Tracker: &ioprogress.ProgressTracker{
 				Length: response.ContentLength,
-				Handler: func(percent int64, speed int64) {
-					req.ProgressHandler(ioprogress.ProgressData{Text: fmt.Sprintf("%d%% (%s/s)", percent, units.GetByteSizeString(speed, 2))})
-				},
 			},
 		}
+
+		if response.ContentLength > 0 {
+			reader.Tracker.Handler = func(percent int64, speed int64) {
+				req.ProgressHandler(ioprogress.ProgressData{Text: fmt.Sprintf("%d%% (%s/s)", percent, units.GetByteSizeString(speed, 2))})
+			}
+		} else {
+			reader.Tracker.Handler = func(received int64, speed int64) {
+				req.ProgressHandler(ioprogress.ProgressData{Text: fmt.Sprintf("%s (%s/s)", units.GetByteSizeString(received, 2), units.GetByteSizeString(speed, 2))})
+
+			}
+		}
+
+		body = reader
 	}
 
 	// Hashing

From 6575b2dd9b9ec386b1ab12943fee513870f9dffe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 25 Jun 2020 17:02:59 -0400
Subject: [PATCH 2/2] lxd/response: Stream multi-part responses
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: St├ęphane Graber <stgraber at ubuntu.com>
---
 lxd/response/response.go | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/lxd/response/response.go b/lxd/response/response.go
index 5270c489f3..302c4eae09 100644
--- a/lxd/response/response.go
+++ b/lxd/response/response.go
@@ -301,9 +301,12 @@ func (r *fileResponse) Render(w http.ResponseWriter) error {
 		return nil
 	}
 
-	// Now the complex multipart answer
-	body := &bytes.Buffer{}
-	mw := multipart.NewWriter(body)
+	// Now the complex multipart answer.
+	mw := multipart.NewWriter(w)
+	defer mw.Close()
+
+	w.Header().Set("Content-Type", mw.FormDataContentType())
+	w.Header().Set("Transfer-Encoding", "chunked")
 
 	for _, entry := range r.files {
 		var rd io.Reader
@@ -329,13 +332,8 @@ func (r *fileResponse) Render(w http.ResponseWriter) error {
 			return err
 		}
 	}
-	mw.Close()
-
-	w.Header().Set("Content-Type", mw.FormDataContentType())
-	w.Header().Set("Content-Length", fmt.Sprintf("%d", body.Len()))
 
-	_, err := io.Copy(w, body)
-	return err
+	return nil
 }
 
 func (r *fileResponse) String() string {


More information about the lxc-devel mailing list