Commit e122950d authored by Jacob Vosmaer's avatar Jacob Vosmaer

Put buffering method in helper package

parent 6ba53d9d
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api" "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper" "gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
...@@ -26,10 +25,10 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response ...@@ -26,10 +25,10 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response
// We must drain the request body before writing the response, to avoid // We must drain the request body before writing the response, to avoid
// upsetting NGINX. // upsetting NGINX.
remainder, err := bufferInTempfile(r.Body) remainder, err := helper.ReadAllTempfile(r.Body)
if err != nil { if err != nil {
fail500(w) fail500(w)
return writtenIn, fmt.Errorf("bufferInTempfile: %v", err) return writtenIn, fmt.Errorf("ReadAllTempfile: %v", err)
} }
defer remainder.Close() defer remainder.Close()
...@@ -83,24 +82,3 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response ...@@ -83,24 +82,3 @@ func handleUploadPack(w *GitHttpResponseWriter, r *http.Request, a *api.Response
func fail500(w http.ResponseWriter) { func fail500(w http.ResponseWriter) {
helper.Fail500(w, nil, nil) helper.Fail500(w, nil, nil)
} }
func bufferInTempfile(r io.Reader) (io.ReadCloser, error) {
buffer, err := ioutil.TempFile("", "gitlab-workhorse-git-request-body")
if err != nil {
return nil, err
}
if err := os.Remove(buffer.Name()); err != nil {
return nil, err
}
if _, err := io.Copy(buffer, r); err != nil {
return nil, err
}
if _, err := buffer.Seek(0, 0); err != nil {
return nil, err
}
return buffer, nil
}
package helper
import (
"io"
"io/ioutil"
"os"
)
func ReadAllTempfile(r io.Reader) (_ io.ReadCloser, err error) {
tempfile, err := ioutil.TempFile("", "gitlab-workhorse-read-all-tempfile")
if err != nil {
return nil, err
}
defer func() {
if err != nil {
tempfile.Close()
}
}()
if err := os.Remove(tempfile.Name()); err != nil {
return nil, err
}
if _, err := io.Copy(tempfile, r); err != nil {
return nil, err
}
if _, err := tempfile.Seek(0, 0); err != nil {
return nil, err
}
return tempfile, nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment