Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add close option for HTTPContext.Request().SetBody method #502

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/context/contexttest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type MockedHTTPRequest struct {
MockedCookies func() []*http.Cookie
MockedAddCookie func(cookie *http.Cookie)
MockedBody func() io.Reader
MockedSetBody func(io.Reader)
MockedSetBody func(io.Reader, bool)
MockedStd func() *http.Request
MockedSize func() uint64
}
Expand Down Expand Up @@ -189,9 +189,9 @@ func (r *MockedHTTPRequest) Body() io.Reader {
}

// SetBody mocks the SetBody function of HTTPRequest
func (r *MockedHTTPRequest) SetBody(body io.Reader) {
func (r *MockedHTTPRequest) SetBody(body io.Reader, closePreviousReader bool) {
if r.MockedSetBody != nil {
r.MockedSetBody(body)
r.MockedSetBody(body, closePreviousReader)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/context/httpcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type (
AddCookie(cookie *http.Cookie)

Body() io.Reader
SetBody(io.Reader)
SetBody(io.Reader, bool)

Std() *http.Request

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/httprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func (r *httpRequest) Body() io.Reader {
return r.body
}

func (r *httpRequest) SetBody(reader io.Reader) {
r.body = callbackreader.New(reader)
func (r *httpRequest) SetBody(reader io.Reader, closePreviousReader bool) {
r.body.SetReader(reader, closePreviousReader)
}

func (r *httpRequest) Size() uint64 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/httptemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func saveReqBody(e *HTTPTemplate, filterName string, ctx HTTPContext) error {
}
e.Engine.SetDict(fmt.Sprintf(filterReqBody, filterName), string(bodyBuff.Bytes()))
// Reset back into Request's Body
ctx.Request().SetBody(bodyBuff)
ctx.Request().SetBody(bodyBuff, true)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/headertojson/headertojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ func (h *HeaderToJSON) handle(ctx context.HTTPContext) string {
if err != nil {
return resultJSONEncodeDecodeErr
}
ctx.Request().SetBody(bytes.NewReader(bodyBytes))
ctx.Request().SetBody(bytes.NewReader(bodyBytes), true)
return ""
}
2 changes: 1 addition & 1 deletion pkg/filter/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (b *Proxy) Handle(ctx context.HTTPContext) (result string) {
func (b *Proxy) handle(ctx context.HTTPContext) (result string) {
if b.mirrorPool != nil && b.mirrorPool.filter.Filter(ctx) {
primaryBody, secondaryBody := newPrimarySecondaryReader(ctx.Request().Body())
ctx.Request().SetBody(primaryBody)
ctx.Request().SetBody(primaryBody, false)

wg := &sync.WaitGroup{}
wg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/remotefilter/remotefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (rf *RemoteFilter) unmarshalHTTPContext(buff []byte, ctx context.HTTPContex
r.SetPath(re.Path)
r.SetQuery(re.Query)
r.Header().Reset(re.Header)
r.SetBody(bytes.NewReader(re.Body))
r.SetBody(bytes.NewReader(re.Body), true)

if we == nil {
return
Expand Down
8 changes: 4 additions & 4 deletions pkg/filter/requestadaptor/requestadaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func (ra *RequestAdaptor) handle(ctx context.HTTPContext) string {
logger.Errorf("BUG request render body failed, template %s, err %v",
ra.spec.Body, err)
} else {
ctx.Request().SetBody(bytes.NewReader([]byte(body)))
ctx.Request().SetBody(bytes.NewReader([]byte(body)), true)
}
} else {
ctx.Request().SetBody(bytes.NewReader([]byte(ra.spec.Body)))
ctx.Request().SetBody(bytes.NewReader([]byte(ra.spec.Body)), true)
}
ctx.Request().Header().Del("Content-Encoding")
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func (ra *RequestAdaptor) processCompress(ctx context.HTTPContext) string {
}
gw.Close()

ctx.Request().SetBody(&buf)
ctx.Request().SetBody(&buf, true)
ctx.Request().Header().Set("Content-Encoding", "gzip")
return ""
}
Expand All @@ -220,7 +220,7 @@ func (ra *RequestAdaptor) processDecompress(ctx context.HTTPContext) string {
if err != nil {
return resultDecompressFail
}
ctx.Request().SetBody(bytes.NewReader(data))
ctx.Request().SetBody(bytes.NewReader(data), true)
ctx.Request().Header().Del("Content-Encoding")
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/retryer/retryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (r *Retryer) handle(ctx context.HTTPContext, u *URLRule) string {
data, _ := io.ReadAll(ctx.Request().Body())
for {
attempt++
ctx.Request().SetBody(bytes.NewReader(data))
ctx.Request().SetBody(bytes.NewReader(data), true)

result := ctx.CallNextHandler("")

Expand Down
12 changes: 12 additions & 0 deletions pkg/util/callbackreader/callbackreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ func (cr *CallbackReader) Close() error {

return nil
}

// SetReader replace previous reader with new reader. If closePreviousReader set to true, it will close
// previous reader.
func (cr *CallbackReader) SetReader(reader io.Reader, closePreviousReader bool) {
if closePreviousReader {
closer, ok := cr.reader.(io.Closer)
if ok {
closer.Close()
}
}
cr.reader = reader
}