Skip to content

Commit 72e8380

Browse files
committed
fix(proxy): invalid Read on closed Body
1 parent ab9f018 commit 72e8380

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

handler/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,24 @@ func getRequestParam(r *http.Request, key string, delete bool) string {
2424
return result
2525
}
2626

27+
func (w *fakeCloseReadCloser) Close() error {
28+
return nil
29+
}
30+
31+
func (w *fakeCloseReadCloser) RealClose() error {
32+
if w.ReadCloser == nil {
33+
return nil
34+
}
35+
return w.ReadCloser.Close()
36+
}
37+
2738
func Handler(w http.ResponseWriter, r *http.Request) {
39+
if r.Body != nil {
40+
r.Body = &fakeCloseReadCloser{r.Body}
41+
defer func() {
42+
_ = r.Body.(*fakeCloseReadCloser).RealClose()
43+
}()
44+
}
2845
proxy.ServeHTTP(w, r)
2946
}
3047

handler/structs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
package handler
22

3+
import (
4+
"io"
5+
)
6+
37
type ctxKeyType struct{}
8+
9+
type fakeCloseReadCloser struct {
10+
io.ReadCloser
11+
}

0 commit comments

Comments
 (0)