internal reverse proxy does not rewrite correct content-Length header after decompressing body with gzip
stanxing opened this issue · 4 comments
stanxing commented
Here is code link:
if r.StatusCode == http.StatusOK {
var buf []byte
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
gr, err := gzip.NewReader(r.Body)
if err != nil {
return err
}
defer gr.Close()
buf, err = ioutil.ReadAll(gr)
if err != nil {
return err
}
r.Header.Del("Content-Encoding")
} else {
buf, err = ioutil.ReadAll(r.Body)
if err != nil {
return err
}
}
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
if buf != nil {
file := filepath.Join(router.opts.DownloadRoot, r.Request.URL.Path)
os.MkdirAll(path.Dir(file), os.ModePerm)
err = renameio.WriteFile(file, buf, 0666)
if err != nil {
return err
}
}
}
r.Body = ioutil.NopCloser(bytes.NewReader(buf))
reload the decompressed data into r.Body
,causing the content-Length changed. I debugged and found that go server returned AbortHandlerError
and client reported EOF
error.
Maybe it is a solution to rewrite content-length header.
decompressedBodyLength := strconv.Itoa(len(buf))
r.Header.Set("Content-Length", decompressedBodyLength)
stanxing commented
Sure, I am glad to contribute it.