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

feat: support for modifying request body #127 #150

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
15 changes: 12 additions & 3 deletions internal/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type reqOpt struct {
headers []pair
respHeader []pair
args []pair
body string
}

func buildReq(opt reqOpt) []byte {
Expand Down Expand Up @@ -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()))
}
3 changes: 2 additions & 1 deletion pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading