Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
wip: http filter framework
Browse files Browse the repository at this point in the history
  • Loading branch information
mathetake committed Sep 16, 2020
1 parent 1dc9e27 commit d69007a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The target Envoy version is `release/v1.15`

## setup

- __git clone__ this repository to `${GOPATH}/github.com/tetratelabs/proxy-wasm-sdk-go`
- __git clone__ this repository to `${GOPATH}/github.com/tetratelabs/proxy-wasm-go-sdk`
- `go get` may fail because some functions do not have the function body.
- For IDE and editors, please set `-tags=proxytest` build tag for correct completion and task runners.

Expand Down
1 change: 1 addition & 0 deletions examples/metrics/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
Expand Down
117 changes: 117 additions & 0 deletions proxytest/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2020 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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 proxytest

import (
"log"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/rawhostcall"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)

type HttpFilterHost struct {
*baseHost

newContext func(contextID uint32) proxywasm.HttpContext
contexts map[uint32]*contextState
currentContextID uint32
}

type contextState struct {
context proxywasm.HttpContext
requestHeaders, responseHeaders,
requestTrailers, responseTrailers [][2]string
requestBody, responseBody []byte

action types.Action
}

func NewHttpFilterHost(f func(contextID uint32) proxywasm.HttpContext) (*HttpFilterHost, func()) {
host := &HttpFilterHost{
baseHost: newBaseHost(),
newContext: f,
contexts: map[uint32]*contextState{},
}

hostMux.Lock()
rawhostcall.RegisterMockWASMHost(host)
return host, func() {
hostMux.Unlock()
}
}

func (h *HttpFilterHost) InitContext() uint32 {
contextID := uint32(len(h.contexts)) + 1
ctx := h.newContext(contextID)

h.contexts[contextID] = &contextState{
context: ctx,
action: types.ActionContinue,
}
return contextID
}

func (h *HttpFilterHost) GetAction(contextID uint32) types.Action {
cs, ok := h.contexts[contextID]
if !ok {
log.Fatalf("invalid context id: %d", contextID)
}
return cs.action
}

func (h *HttpFilterHost) PutRequestHeaders(contextID uint32, headers [][2]string) {
cs, ok := h.contexts[contextID]
if !ok {
log.Fatalf("invalid context id: %d", contextID)
}

cs.requestHeaders = headers
h.currentContextID = contextID
cs.context.OnHttpRequestHeaders(len(headers), false) // TODO: allow for specifying end_of_stream
}

func (h *HttpFilterHost) PutResponseHeaders(contextID uint32, headers [][2]string) {
cs, ok := h.contexts[contextID]
if !ok {
log.Fatalf("invalid context id: %d", contextID)
}

cs.responseHeaders = headers
h.currentContextID = contextID
cs.context.OnHttpResponseHeaders(len(headers), false) // TODO: allow for specifying end_of_stream
}

func (h *HttpFilterHost) PutRequestTrailers(contextID uint32, headers [][2]string) {
cs, ok := h.contexts[contextID]
if !ok {
log.Fatalf("invalid context id: %d", contextID)
}

cs.requestTrailers = headers
h.currentContextID = contextID
cs.context.OnHttpRequestTrailers(len(headers))
}

func (h *HttpFilterHost) PutResponseTrailers(contextID uint32, headers [][2]string) {
cs, ok := h.contexts[contextID]
if !ok {
log.Fatalf("invalid context id: %d", contextID)
}

cs.responseTrailers = headers
h.currentContextID = contextID
cs.context.OnHttpResponseTrailers(len(headers))
}
17 changes: 17 additions & 0 deletions proxytest/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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 proxytest

// TODO:

0 comments on commit d69007a

Please sign in to comment.