This is an automated email from the ASF dual-hosted git repository.

shuaijinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git


The following commit(s) were added to refs/heads/master by this push:
     new ade2e90  feat(request): fetch request body support (#70)
ade2e90 is described below

commit ade2e90be694adedda2bbeed48708e727b1cb11d
Author: 帅进超 <[email protected]>
AuthorDate: Mon Mar 7 10:02:40 2022 +0800

    feat(request): fetch request body support (#70)
---
 internal/http/request.go      | 19 +++++++++++++++
 internal/http/request_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++
 pkg/http/http.go              |  7 ++++++
 3 files changed, 83 insertions(+)

diff --git a/internal/http/request.go b/internal/http/request.go
index ce7de9f..c37e64b 100644
--- a/internal/http/request.go
+++ b/internal/http/request.go
@@ -54,6 +54,7 @@ type Request struct {
        rawArgs url.Values
 
        vars map[string][]byte
+       body []byte
 
        ctx    context.Context
        cancel context.CancelFunc
@@ -162,6 +163,23 @@ func (r *Request) Var(name string) ([]byte, error) {
        return v, nil
 }
 
+func (r *Request) Body() ([]byte, error) {
+       if len(r.body) > 0 {
+               return r.body, nil
+       }
+
+       builder := util.GetBuilder()
+       ei.ReqBodyStart(builder)
+       bodyInfo := ei.ReqBodyEnd(builder)
+       v, err := r.askExtraInfo(builder, ei.InfoReqBody, bodyInfo)
+       if err != nil {
+               return nil, err
+       }
+
+       r.body = v
+       return v, nil
+}
+
 func (r *Request) Reset() {
        defer r.cancel()
        r.path = nil
@@ -169,6 +187,7 @@ func (r *Request) Reset() {
        r.args = nil
 
        r.vars = nil
+       r.body = nil
        r.conn = nil
        r.ctx = nil
 
diff --git a/internal/http/request_test.go b/internal/http/request_test.go
index d276b5b..37e5294 100644
--- a/internal/http/request_test.go
+++ b/internal/http/request_test.go
@@ -398,3 +398,60 @@ func TestContext(t *testing.T) {
        ReuseRequest(r)
        assert.Equal(t, r.ctx, nil)
 }
+
+func TestBody(t *testing.T) {
+       out := buildReq(reqOpt{})
+       r := CreateRequest(out)
+
+       cc, sc := net.Pipe()
+       r.BindConn(cc)
+
+       go func() {
+               header := make([]byte, util.HeaderLen)
+               n, err := sc.Read(header)
+               if util.ReadErr(n, err, util.HeaderLen) {
+                       return
+               }
+
+               ty := header[0]
+               assert.Equal(t, byte(util.RPCExtraInfo), ty)
+               header[0] = 0
+               length := binary.BigEndian.Uint32(header)
+
+               buf := make([]byte, length)
+               n, err = sc.Read(buf)
+               if util.ReadErr(n, err, int(length)) {
+                       return
+               }
+
+               req := ei.GetRootAsReq(buf, 0)
+               assert.Equal(t, ei.InfoReqBody, req.InfoType())
+
+               builder := util.GetBuilder()
+               res := builder.CreateByteVector([]byte("Hello, Go Runner"))
+               ei.RespStart(builder)
+               ei.RespAddResult(builder, res)
+               eiRes := ei.RespEnd(builder)
+               builder.Finish(eiRes)
+               out := builder.FinishedBytes()
+               size := len(out)
+               binary.BigEndian.PutUint32(header, uint32(size))
+               header[0] = util.RPCExtraInfo
+
+               n, err = sc.Write(header)
+               if err != nil {
+                       util.WriteErr(n, err)
+                       return
+               }
+
+               n, err = sc.Write(out)
+               if err != nil {
+                       util.WriteErr(n, err)
+                       return
+               }
+       }()
+
+       v, err := r.Body()
+       assert.Nil(t, err)
+       assert.Equal(t, "Hello, Go Runner", string(v))
+}
diff --git a/pkg/http/http.go b/pkg/http/http.go
index 4fed2f6..90916e3 100644
--- a/pkg/http/http.go
+++ b/pkg/http/http.go
@@ -60,6 +60,13 @@ type Request interface {
        // pkg/common.ErrConnClosed type is returned.
        Var(name string) ([]byte, error)
 
+       // Body returns HTTP request body
+       //
+       // To fetch the value, the runner will look up the request's cache 
first. If not found,
+       // 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)
+
        // Context returns the request's context.
        //
        // The returned context is always non-nil; it defaults to the

Reply via email to