Commit 2ca18f44 authored by Patrick Bajao's avatar Patrick Bajao

Merge branch 'id-dependency-proxy-set-content-length' into 'master'

Set Content-Length header on Dependency Proxy injection

See merge request gitlab-org/gitlab!72670
parents 7caed114 02fef51f
...@@ -67,6 +67,8 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin ...@@ -67,6 +67,8 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin
return return
} }
w.Header().Set("Content-Length", dependencyResponse.Header.Get("Content-Length"))
teeReader := io.TeeReader(dependencyResponse.Body, w) teeReader := io.TeeReader(dependencyResponse.Body, w)
saveFileRequest, err := http.NewRequestWithContext(r.Context(), "POST", r.URL.String()+"/upload", teeReader) saveFileRequest, err := http.NewRequestWithContext(r.Context(), "POST", r.URL.String()+"/upload", teeReader)
if err != nil { if err != nil {
...@@ -75,8 +77,6 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin ...@@ -75,8 +77,6 @@ func (p *Injector) Inject(w http.ResponseWriter, r *http.Request, sendData strin
saveFileRequest.Header = helper.HeaderClone(r.Header) saveFileRequest.Header = helper.HeaderClone(r.Header)
saveFileRequest.ContentLength = dependencyResponse.ContentLength saveFileRequest.ContentLength = dependencyResponse.ContentLength
w.Header().Del("Content-Length")
nrw := &nullResponseWriter{header: make(http.Header)} nrw := &nullResponseWriter{header: make(http.Header)}
p.uploadHandler.ServeHTTP(nrw, saveFileRequest) p.uploadHandler.ServeHTTP(nrw, saveFileRequest)
......
...@@ -33,7 +33,7 @@ func (f *fakeUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -33,7 +33,7 @@ func (f *fakeUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type errWriter struct{ writes int } type errWriter struct{ writes int }
func (w *errWriter) Header() http.Header { return nil } func (w *errWriter) Header() http.Header { return make(http.Header) }
func (w *errWriter) WriteHeader(h int) {} func (w *errWriter) WriteHeader(h int) {}
// First call of Write function succeeds while all the subsequent ones fail // First call of Write function succeeds while all the subsequent ones fail
...@@ -112,8 +112,9 @@ func TestInject(t *testing.T) { ...@@ -112,8 +112,9 @@ func TestInject(t *testing.T) {
func TestSuccessfullRequest(t *testing.T) { func TestSuccessfullRequest(t *testing.T) {
content := []byte("result") content := []byte("result")
contentLength := strconv.Itoa(len(content))
originResourceServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { originResourceServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", strconv.Itoa(len(content))) w.Header().Set("Content-Length", contentLength)
w.Write(content) w.Write(content)
})) }))
...@@ -135,6 +136,7 @@ func TestSuccessfullRequest(t *testing.T) { ...@@ -135,6 +136,7 @@ func TestSuccessfullRequest(t *testing.T) {
require.Equal(t, 200, response.Code) require.Equal(t, 200, response.Code)
require.Equal(t, string(content), response.Body.String()) require.Equal(t, string(content), response.Body.String())
require.Equal(t, contentLength, response.Header().Get("Content-Length"))
} }
func TestIncorrectSendData(t *testing.T) { func TestIncorrectSendData(t *testing.T) {
......
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