Skip to content

Commit

Permalink
Write to byte buffer first, then to response (#694)
Browse files Browse the repository at this point in the history
* Write to byte buffer first, then to response

* Fix golint

* Another lint fix
  • Loading branch information
m4gus88 authored Feb 25, 2022
1 parent 4370976 commit a526414
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit a526414

Please sign in to comment.