From a5264140b24c5e996c5f0ec5b0624c45e637f392 Mon Sep 17 00:00:00 2001 From: Gergo Schmall Date: Fri, 25 Feb 2022 21:44:26 +0000 Subject: [PATCH] Write to byte buffer first, then to response (#694) * Write to byte buffer first, then to response * Fix golint * Another lint fix --- fakestorage/server.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fakestorage/server.go b/fakestorage/server.go index 0d069f8c12..607a833489 100644 --- a/fakestorage/server.go +++ b/fakestorage/server.go @@ -11,6 +11,7 @@ import ( "context" "fmt" "io" + "io/ioutil" "mime" "mime/multipart" "net" @@ -26,6 +27,7 @@ import ( "github.com/fsouza/fake-gcs-server/internal/notification" "github.com/gorilla/handlers" "github.com/gorilla/mux" + "github.com/sirupsen/logrus" "golang.org/x/oauth2/google" "google.golang.org/api/option" ) @@ -308,7 +310,8 @@ func (s *Server) handleBatchCall(w http.ResponseWriter, r *http.Request) { return } - mw := multipart.NewWriter(w) + var b bytes.Buffer + mw := multipart.NewWriter(&b) defer mw.Close() w.Header().Set("Content-Type", "multipart/mixed; boundary="+mw.Boundary()) @@ -337,7 +340,8 @@ func (s *Server) handleBatchCall(w http.ResponseWriter, r *http.Request) { continue } - content, err := loadContent(part) + content, err := ioutil.ReadAll(part) + part.Close() if err != nil { http.Error(partResponseWriter, "unable to process request", http.StatusBadRequest) writeMultipartResponse(partResponseWriter.Result(), partWriter, contentID) @@ -354,6 +358,13 @@ func (s *Server) handleBatchCall(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(partResponseWriter, partRequest) writeMultipartResponse(partResponseWriter.Result(), partWriter, contentID) } + mw.Close() + + _, err = b.WriteTo(w) + if err != nil { + logrus.New().Error(err) + http.Error(w, "unable to process request", http.StatusBadRequest) + } } func writeMultipartResponse(r *http.Response, w io.Writer, contentId string) {