You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/05/31 02:07:19 UTC
[apisix-go-plugin-runner] 12/22: feat: add basic structure for
HTTPReqCall
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git
commit c0d186030e6757c328589d17f523572194e45a09
Author: spacewander <sp...@gmail.com>
AuthorDate: Mon May 24 08:50:52 2021 +0800
feat: add basic structure for HTTPReqCall
---
internal/http/{http.go => request.go} | 23 +++++++++------
internal/http/{http.go => response.go} | 29 +++++++++++--------
internal/plugin/conf.go | 4 +++
internal/{http/http.go => plugin/plugin.go} | 36 ++++++++++++++++++++----
internal/{http/http.go => plugin/plugin_test.go} | 31 ++++++++++++++------
internal/server/server.go | 3 +-
{internal => pkg}/http/http.go | 23 +++++++--------
7 files changed, 100 insertions(+), 49 deletions(-)
diff --git a/internal/http/http.go b/internal/http/request.go
similarity index 74%
copy from internal/http/http.go
copy to internal/http/request.go
index d946102..4c96aa2 100644
--- a/internal/http/http.go
+++ b/internal/http/request.go
@@ -15,16 +15,21 @@
package http
import (
- flatbuffers "github.com/google/flatbuffers/go"
-
- "github.com/apache/apisix-go-plugin-runner/internal/util"
hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
)
-func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
- builder := util.GetBuilder()
- hrc.RespStart(builder)
- resp := hrc.RespEnd(builder)
- builder.Finish(resp)
- return builder, nil
+type Request struct {
+ // the root of the flatbuffers HTTPReqCall Request msg
+ r *hrc.Req
+}
+
+func (r Request) ConfToken() uint32 {
+ return r.r.ConfToken()
+}
+
+func CreateRequest(buf []byte) *Request {
+ req := &Request{
+ r: hrc.GetRootAsReq(buf, 0),
+ }
+ return req
}
diff --git a/internal/http/http.go b/internal/http/response.go
similarity index 69%
copy from internal/http/http.go
copy to internal/http/response.go
index d946102..46fbeeb 100644
--- a/internal/http/http.go
+++ b/internal/http/response.go
@@ -14,17 +14,24 @@
// limitations under the License.
package http
-import (
- flatbuffers "github.com/google/flatbuffers/go"
+import "net/http"
- "github.com/apache/apisix-go-plugin-runner/internal/util"
- hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
-)
+type Response struct {
+ code int
+}
+
+func (r Response) Header() http.Header {
+ return nil
+}
+
+func (r Response) Write([]byte) (int, error) {
+ return 0, nil
+}
+
+func (r Response) WriteHeader(statusCode int) {
+ r.code = statusCode
+}
-func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
- builder := util.GetBuilder()
- hrc.RespStart(builder)
- resp := hrc.RespEnd(builder)
- builder.Finish(resp)
- return builder, nil
+func CreateResponse() *Response {
+ return &Response{}
}
diff --git a/internal/plugin/conf.go b/internal/plugin/conf.go
index 8e18ecc..3b3a8bb 100644
--- a/internal/plugin/conf.go
+++ b/internal/plugin/conf.go
@@ -81,3 +81,7 @@ func GetRuleConf(token uint32) (RuleConf, error) {
}
return res.(RuleConf), err
}
+
+func SetRuleConf(token uint32, conf RuleConf) error {
+ return cache.Set(strconv.FormatInt(int64(token), 10), conf)
+}
diff --git a/internal/http/http.go b/internal/plugin/plugin.go
similarity index 62%
copy from internal/http/http.go
copy to internal/plugin/plugin.go
index d946102..ba07c8a 100644
--- a/internal/http/http.go
+++ b/internal/plugin/plugin.go
@@ -12,19 +12,45 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-package http
+package plugin
import (
+ "net/http"
+
+ hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
flatbuffers "github.com/google/flatbuffers/go"
+ inHTTP "github.com/apache/apisix-go-plugin-runner/internal/http"
"github.com/apache/apisix-go-plugin-runner/internal/util"
- hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
+ pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
)
-func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
+func handle(conf RuleConf, w http.ResponseWriter, r pkgHTTP.Request) error {
+ return nil
+}
+
+func reportAction(req *inHTTP.Request, resp *inHTTP.Response) *flatbuffers.Builder {
builder := util.GetBuilder()
hrc.RespStart(builder)
- resp := hrc.RespEnd(builder)
- builder.Finish(resp)
+ res := hrc.RespEnd(builder)
+ builder.Finish(res)
+ return builder
+}
+
+func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
+ req := inHTTP.CreateRequest(buf)
+ resp := inHTTP.CreateResponse()
+
+ token := req.ConfToken()
+ conf, err := GetRuleConf(token)
+ if err != nil {
+ return nil, err
+ }
+ err = handle(conf, resp, req)
+ if err != nil {
+ return nil, err
+ }
+
+ builder := reportAction(req, resp)
return builder, nil
}
diff --git a/internal/http/http.go b/internal/plugin/plugin_test.go
similarity index 64%
copy from internal/http/http.go
copy to internal/plugin/plugin_test.go
index d946102..5433cca 100644
--- a/internal/http/http.go
+++ b/internal/plugin/plugin_test.go
@@ -12,19 +12,32 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-package http
+package plugin
import (
- flatbuffers "github.com/google/flatbuffers/go"
+ "testing"
+ "time"
- "github.com/apache/apisix-go-plugin-runner/internal/util"
hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
+ flatbuffers "github.com/google/flatbuffers/go"
+ "github.com/stretchr/testify/assert"
)
-func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
- builder := util.GetBuilder()
- hrc.RespStart(builder)
- resp := hrc.RespEnd(builder)
- builder.Finish(resp)
- return builder, nil
+func TestHTTPReqCall(t *testing.T) {
+ InitConfCache(10 * time.Millisecond)
+ SetRuleConf(1, RuleConf{})
+
+ builder := flatbuffers.NewBuilder(1024)
+ hrc.ReqStart(builder)
+ hrc.ReqAddConfToken(builder, 1)
+ r := hrc.ReqEnd(builder)
+ builder.Finish(r)
+ out := builder.FinishedBytes()
+
+ b, err := HTTPReqCall(out)
+ assert.Nil(t, err)
+
+ out = b.FinishedBytes()
+ resp := hrc.GetRootAsResp(out, 0)
+ assert.Equal(t, hrc.ActionNONE, resp.ActionType())
}
diff --git a/internal/server/server.go b/internal/server/server.go
index a856066..7299252 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -27,7 +27,6 @@ import (
"syscall"
"time"
- "github.com/apache/apisix-go-plugin-runner/internal/http"
"github.com/apache/apisix-go-plugin-runner/internal/log"
"github.com/apache/apisix-go-plugin-runner/internal/plugin"
"github.com/apache/apisix-go-plugin-runner/internal/util"
@@ -101,7 +100,7 @@ func handleConn(c net.Conn) {
case RPCPrepareConf:
bd, err = plugin.PrepareConf(buf)
case RPCHTTPReqCall:
- bd, err = http.HTTPReqCall(buf)
+ bd, err = plugin.HTTPReqCall(buf)
default:
err = UnknownType{ty}
}
diff --git a/internal/http/http.go b/pkg/http/http.go
similarity index 57%
rename from internal/http/http.go
rename to pkg/http/http.go
index d946102..54edde1 100644
--- a/internal/http/http.go
+++ b/pkg/http/http.go
@@ -14,17 +14,14 @@
// limitations under the License.
package http
-import (
- flatbuffers "github.com/google/flatbuffers/go"
-
- "github.com/apache/apisix-go-plugin-runner/internal/util"
- hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
-)
-
-func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
- builder := util.GetBuilder()
- hrc.RespStart(builder)
- resp := hrc.RespEnd(builder)
- builder.Finish(resp)
- return builder, nil
+// Request represents the HTTP request received by APISIX.
+// We don't use net/http's Request because it doesn't suit our purpose.
+// Take `Request.Header` as an example:
+//
+// 1. We need to record any change to the request headers. As the Request.Header
+// is not an interface, there is not way to inject our special tracker.
+// 2. As the author of fasthttp pointed out, "headers are stored in a map[string][]string.
+// So the server must parse all the headers, ...". The official API is suboptimal, which
+// is even worse in our case as it is not a real HTTP server.
+type Request interface {
}