2020-12-02 10:09:37 -05:00
|
|
|
package upload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
|
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
|
2022-03-15 08:07:44 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/upload/destination"
|
2020-12-02 10:09:37 -05:00
|
|
|
)
|
|
|
|
|
2022-02-14 04:13:38 -05:00
|
|
|
// RequestBody is a request middleware. It will store the request body to
|
|
|
|
// a location by determined an api.Response value. It then forwards the
|
|
|
|
// request to gitlab-rails without the original request body.
|
|
|
|
func RequestBody(rails PreAuthorizer, h http.Handler, p Preparer) http.Handler {
|
2020-12-02 10:09:37 -05:00
|
|
|
return rails.PreAuthorizeHandler(func(w http.ResponseWriter, r *http.Request, a *api.Response) {
|
|
|
|
opts, verifier, err := p.Prepare(a)
|
|
|
|
if err != nil {
|
2022-02-14 04:13:38 -05:00
|
|
|
helper.Fail500(w, r, fmt.Errorf("RequestBody: preparation failed: %v", err))
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
fh, err := destination.Upload(r.Context(), r.Body, r.ContentLength, opts)
|
2020-12-02 10:09:37 -05:00
|
|
|
if err != nil {
|
2022-02-14 04:13:38 -05:00
|
|
|
helper.Fail500(w, r, fmt.Errorf("RequestBody: upload failed: %v", err))
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if verifier != nil {
|
|
|
|
if err := verifier.Verify(fh); err != nil {
|
2022-02-14 04:13:38 -05:00
|
|
|
helper.Fail500(w, r, fmt.Errorf("RequestBody: verification failed: %v", err))
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := url.Values{}
|
|
|
|
fields, err := fh.GitLabFinalizeFields("file")
|
|
|
|
if err != nil {
|
2022-02-14 04:13:38 -05:00
|
|
|
helper.Fail500(w, r, fmt.Errorf("RequestBody: finalize fields failed: %v", err))
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fields {
|
|
|
|
data.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack body
|
|
|
|
body := data.Encode()
|
|
|
|
r.Body = ioutil.NopCloser(strings.NewReader(body))
|
|
|
|
r.ContentLength = int64(len(body))
|
|
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
sft := SavedFileTracker{Request: r}
|
|
|
|
sft.Track("file", fh.LocalPath)
|
|
|
|
if err := sft.Finalize(r.Context()); err != nil {
|
2022-02-14 04:13:38 -05:00
|
|
|
helper.Fail500(w, r, fmt.Errorf("RequestBody: finalize failed: %v", err))
|
2020-12-02 10:09:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// And proxy the request
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}, "/authorize")
|
|
|
|
}
|