forked from tetratelabs/proxy-wasm-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add entrypoints for Plugin, Http, Tcp contexts
Fixes #5 Signed-off-by: Matt Leon <[email protected]>
- Loading branch information
Showing
11 changed files
with
287 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// 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 internal | ||
|
||
import "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" | ||
|
||
// elidedVmContext is registered when the plugin author uses a | ||
// SetPluginContext, SetHttpContext, or SetTcpContext entrypoint. It indicates | ||
// the author did not register a VmContext. | ||
// | ||
// elidedVmContext's primary responsibility is calling the author-provided (or | ||
// elided) thunk to create a new PluginContext. | ||
type elidedVmContext struct { | ||
types.DefaultVMContext | ||
newPluginContext types.PluginContextFactory | ||
} | ||
|
||
func (ctx *elidedVmContext) NewPluginContext(contextID uint32) types.PluginContext { | ||
return ctx.newPluginContext(contextID) | ||
} | ||
|
||
// elidedPluginContext is registered when the plugin author uses the | ||
// SetHttpContext or SetTcpContext entrypoints. It indicates the author did not | ||
// register a VmContext or PluginContext. | ||
// | ||
// elidedVmContext's primary responsibility is calling the author-provided (or | ||
// elided) thunk to create a new HttpContext or TcpContext. | ||
type elidedPluginContext struct { | ||
types.DefaultPluginContext | ||
newHttpContext types.HttpContextFactory | ||
newTcpContext types.TcpContextFactory | ||
} | ||
|
||
func (ctx *elidedPluginContext) NewHttpContext(contextID uint32) types.HttpContext { | ||
return ctx.newHttpContext(contextID) | ||
} | ||
|
||
func (ctx *elidedPluginContext) NewTcpContext(contextID uint32) types.TcpContext { | ||
return ctx.newTcpContext(contextID) | ||
} | ||
|
||
func SetPluginContext(newPluginContext types.PluginContextFactory) { | ||
SetVMContext(&elidedVmContext{newPluginContext: newPluginContext}) | ||
} | ||
|
||
func SetHttpContext(newHttpContext types.HttpContextFactory) { | ||
SetVMContext(&elidedVmContext{ | ||
newPluginContext: func(uint32) types.PluginContext { | ||
return &elidedPluginContext{newHttpContext: newHttpContext} | ||
}, | ||
}) | ||
} | ||
|
||
func SetTcpContext(newTcpContext types.TcpContextFactory) { | ||
SetVMContext(&elidedVmContext{ | ||
newPluginContext: func(uint32) types.PluginContext { | ||
return &elidedPluginContext{newTcpContext: newTcpContext} | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// 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 internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testPluginContext struct { | ||
types.DefaultPluginContext | ||
contextID uint32 | ||
} | ||
|
||
func TestSetPluginContext_ReturnsPluginContext(t *testing.T) { | ||
SetPluginContext(func(contextID uint32) types.PluginContext { | ||
return &testPluginContext{contextID: contextID} | ||
}) | ||
|
||
pluginContext := currentState.vmContext.NewPluginContext(4321) | ||
|
||
require.IsType(t, pluginContext, &testPluginContext{}) | ||
require.Equal(t, pluginContext.(*testPluginContext).contextID, uint32(4321)) | ||
} | ||
|
||
type testPluginContextB struct { | ||
types.DefaultPluginContext | ||
} | ||
|
||
func TestSetPluginContext_Reentrant(t *testing.T) { | ||
SetPluginContext(func(uint32) types.PluginContext { | ||
return &testPluginContext{} | ||
}) | ||
SetPluginContext(func(uint32) types.PluginContext { | ||
return &testPluginContextB{} | ||
}) | ||
|
||
require.IsType(t, currentState.vmContext.NewPluginContext(1), &testPluginContextB{}) | ||
} | ||
|
||
type ( | ||
testHttpContext struct { | ||
types.DefaultHttpContext | ||
contextID uint32 | ||
} | ||
testTcpContext struct{ types.DefaultTcpContext } | ||
) | ||
|
||
func TestSetHttpContext(t *testing.T) { | ||
SetHttpContext(func(contextID uint32) types.HttpContext { | ||
return &testHttpContext{contextID: contextID} | ||
}) | ||
|
||
pluginContext := currentState.vmContext.NewPluginContext(4321).NewHttpContext(1234) | ||
|
||
require.IsType(t, pluginContext, &testHttpContext{}) | ||
require.Equal(t, pluginContext.(*testHttpContext).contextID, uint32(1234)) | ||
} | ||
|
||
func TestSetTcpContext(t *testing.T) { | ||
SetTcpContext(func(uint32) types.TcpContext { | ||
return &testTcpContext{} | ||
}) | ||
|
||
pluginContext := currentState.vmContext.NewPluginContext(4321).NewTcpContext(1234) | ||
|
||
require.IsType(t, pluginContext, &testTcpContext{}) | ||
} | ||
|
||
func TestSetTcpContext_ClearsSetHttpContext(t *testing.T) { | ||
SetHttpContext(func(contextID uint32) types.HttpContext { | ||
return &testHttpContext{contextID: contextID} | ||
}) | ||
SetTcpContext(func(uint32) types.TcpContext { | ||
return &testTcpContext{} | ||
}) | ||
|
||
pluginContext := currentState.vmContext.NewPluginContext(4321).NewTcpContext(1234) | ||
|
||
require.IsType(t, pluginContext, &testTcpContext{}) | ||
} |
Oops, something went wrong.