From bdf2bf9bb4c403632ba164bd7451cce9d00c8df6 Mon Sep 17 00:00:00 2001 From: thinkdb1 Date: Fri, 17 Nov 2023 14:48:42 +0800 Subject: [PATCH] edit body --- go.mod | 2 +- go.sum | 4 ++-- internal/http/request.go | 15 ++++++++++++--- internal/http/request_test.go | 12 ++++++++++++ pkg/http/http.go | 3 ++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 4f7915f..80186c7 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.15 require ( github.com/ReneKroon/ttlcache/v2 v2.4.0 - github.com/api7/ext-plugin-proto v0.6.0 + github.com/api7/ext-plugin-proto v0.6.1 github.com/google/flatbuffers v2.0.0+incompatible github.com/spf13/cobra v1.2.1 github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index ff5ba33..088e956 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,8 @@ github.com/ReneKroon/ttlcache/v2 v2.4.0 h1:KywGhjik+ZFTDXMNLiPECSzmdx2yNvAlDNKES github.com/ReneKroon/ttlcache/v2 v2.4.0/go.mod h1:zbo6Pv/28e21Z8CzzqgYRArQYGYtjONRxaAKGxzQvG4= github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/api7/ext-plugin-proto v0.6.0 h1:xmgcKwWRiM9EpBIs1wYJ7Ife/YnLl4IL2NEy4417g60= -github.com/api7/ext-plugin-proto v0.6.0/go.mod h1:8dbdAgCESeqwZ0IXirbjLbshEntmdrAX3uet+LW3jVU= +github.com/api7/ext-plugin-proto v0.6.1 h1:eQN0oHacL97ezVGWVmsRigt+ClcpgjipUq0rmW8BG4g= +github.com/api7/ext-plugin-proto v0.6.1/go.mod h1:8dbdAgCESeqwZ0IXirbjLbshEntmdrAX3uet+LW3jVU= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= diff --git a/internal/http/request.go b/internal/http/request.go index 5c00172..2dfdb6a 100644 --- a/internal/http/request.go +++ b/internal/http/request.go @@ -89,6 +89,10 @@ func (r *Request) SetPath(path []byte) { r.path = path } +func (r *Request) SetBody(b []byte) { + r.body = b +} + func (r *Request) Header() pkgHTTP.Header { if r.hdr == nil { hdr := newHeader() @@ -205,15 +209,17 @@ func (r *Request) Reset() { } func (r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder) bool { - if r.path == nil && r.hdr == nil && r.args == nil && r.respHdr == nil { + if r.path == nil && r.hdr == nil && r.args == nil && r.respHdr == nil && r.body == nil { return false } - var path flatbuffers.UOffsetT + var path, body flatbuffers.UOffsetT if r.path != nil { path = builder.CreateByteString(r.path) } - + if r.body != nil { + body = builder.CreateByteString(r.body) + } var hdrVec, respHdrVec flatbuffers.UOffsetT if r.hdr != nil { hdrs := []flatbuffers.UOffsetT{} @@ -314,6 +320,9 @@ func (r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder) bool { if path > 0 { hrc.RewriteAddPath(builder, path) } + if body > 0 { + hrc.RewriteAddBody(builder, body) + } if hdrVec > 0 { hrc.RewriteAddHeaders(builder, hdrVec) } diff --git a/internal/http/request_test.go b/internal/http/request_test.go index d6b58ed..0e5af6b 100644 --- a/internal/http/request_test.go +++ b/internal/http/request_test.go @@ -72,6 +72,7 @@ type reqOpt struct { headers []pair respHeader []pair args []pair + body string } func buildReq(opt reqOpt) []byte { @@ -505,3 +506,14 @@ func TestBody(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "Hello, Go Runner", string(v)) } +func TestSetBody(t *testing.T) { + out := buildReq(reqOpt{body: "apisix"}) + r := CreateRequest(out) + r.SetBody([]byte("apisix")) + assert.Equal(t, "apisix", string(r.body)) + r.SetBody([]byte("apisix_edit")) + builder := util.GetBuilder() + assert.True(t, r.FetchChanges(1, builder)) + rewrite := getRewriteAction(t, builder) + assert.Equal(t, "apisix_edit", string(rewrite.BodyBytes())) +} diff --git a/pkg/http/http.go b/pkg/http/http.go index 5b4e93c..2ec430b 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -66,7 +66,8 @@ type Request interface { // the runner will ask it from the APISIX. If the RPC call is failed, an error in // pkg/common.ErrConnClosed type is returned. Body() ([]byte, error) - + // SetBody edit body + SetBody([]byte) // Context returns the request's context. // // The returned context is always non-nil; it defaults to the