Skip to content

Commit

Permalink
fix(proxy): invalid Read on closed Body
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Feb 15, 2022
1 parent ab9f018 commit 72e8380
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ func getRequestParam(r *http.Request, key string, delete bool) string {
return result
}

func (w *fakeCloseReadCloser) Close() error {
return nil
}

func (w *fakeCloseReadCloser) RealClose() error {
if w.ReadCloser == nil {
return nil
}
return w.ReadCloser.Close()
}

func Handler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
r.Body = &fakeCloseReadCloser{r.Body}
defer func() {
_ = r.Body.(*fakeCloseReadCloser).RealClose()
}()
}
proxy.ServeHTTP(w, r)
}

Expand Down
8 changes: 8 additions & 0 deletions handler/structs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package handler

import (
"io"
)

type ctxKeyType struct{}

type fakeCloseReadCloser struct {
io.ReadCloser
}

0 comments on commit 72e8380

Please sign in to comment.