Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Sep 11, 2023
1 parent e8a88b9 commit 0f733dc
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pkg/utils/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,18 @@ func (p *customReverseProxies) ServeHTTP(w http.ResponseWriter, r *http.Request)
reader = resp.Body
}

// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data.
// So we need to call WriteHeader first.
// We need to copy the response headers before we write the header.
// Otherwise, we cannot set the header.
// And we need to write the header before we copy the response body.
// Otherwise, we cannot set the status code.
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)

var contentLength, written int64
for {
if written, err = io.CopyN(w, reader, chunkSize); err != nil {
if _, err = io.CopyN(w, reader, chunkSize); err != nil {
if err == io.EOF {
err = nil
}
contentLength += written
break
}
}
Expand All @@ -454,17 +455,18 @@ func (p *customReverseProxies) ServeHTTP(w http.ResponseWriter, r *http.Request)
// try next url.
continue
}

// We need to set the Content-Length header manually to avoid meeting unexpected EOF error.
w.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10))
copyHeader(w.Header(), resp.Header)
return
}
http.Error(w, ErrRedirectFailed, http.StatusInternalServerError)
}

func copyHeader(dst, src http.Header) {
for k, vv := range src {
// skip Content-Encoding and Content-Length header
// because they need to be set by http.ResponseWriter when gzip is enabled
if k == "Content-Encoding" || k == "Content-Length" {
continue
}
values := dst[k]
for _, v := range vv {
if !slice.Contains(values, v) {
Expand Down

0 comments on commit 0f733dc

Please sign in to comment.