Skip to content
Merged
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
21 changes: 21 additions & 0 deletions src/main/java/io/roastedroot/proxywasm/impl/Imports.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.roastedroot.proxywasm.v1.Handler;
import io.roastedroot.proxywasm.v1.LogLevel;
import io.roastedroot.proxywasm.v1.MapType;
import io.roastedroot.proxywasm.v1.StreamType;
import io.roastedroot.proxywasm.v1.WasmException;
import io.roastedroot.proxywasm.v1.WasmResult;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -779,4 +780,24 @@ int proxyGetCurrentTimeNanoseconds(int returnTime) {
return e.result().getValue();
}
}

@WasmExport
int proxyContinueStream(int arg) {
var streamType = StreamType.fromInt(arg);
if (streamType == null) {
return WasmResult.BAD_ARGUMENT.getValue();
}
switch (streamType) {
case REQUEST:
return handler.continueRequest().getValue();
case RESPONSE:
return handler.continueResponse().getValue();
case DOWNSTREAM:
return handler.continueDownstream().getValue();
case UPSTREAM:
return handler.continueUpstream().getValue();
}
// should never reach here
return WasmResult.INTERNAL_FAILURE.getValue();
}
}
20 changes: 20 additions & 0 deletions src/main/java/io/roastedroot/proxywasm/v1/ChainedHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,24 @@ public WasmResult setHttpResponseBody(byte[] body) {
public WasmResult setHttpRequestBody(byte[] body) {
return next().setHttpRequestBody(body);
}

@Override
public WasmResult continueRequest() {
return next().continueRequest();
}

@Override
public WasmResult continueResponse() {
return next().continueResponse();
}

@Override
public WasmResult continueDownstream() {
return next().continueDownstream();
}

@Override
public WasmResult continueUpstream() {
return next().continueUpstream();
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/roastedroot/proxywasm/v1/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,20 @@ default WasmResult setGrpcReceiveInitialMetaData(Map<String, String> metadata) {
default WasmResult setGrpcReceiveTrailerMetaData(Map<String, String> metadata) {
return WasmResult.UNIMPLEMENTED;
}

default WasmResult continueRequest() {
return WasmResult.UNIMPLEMENTED;
}

default WasmResult continueResponse() {
return WasmResult.UNIMPLEMENTED;
}

default WasmResult continueDownstream() {
return WasmResult.UNIMPLEMENTED;
}

default WasmResult continueUpstream() {
return WasmResult.UNIMPLEMENTED;
}
}
47 changes: 47 additions & 0 deletions src/main/java/io/roastedroot/proxywasm/v1/StreamType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.roastedroot.proxywasm.v1;

/**
* Represents the type of map in proxy WASM.
* Converted from Go's MapType type.
*/
public enum StreamType {
REQUEST(0),
RESPONSE(1),
DOWNSTREAM(2),
UPSTREAM(3);

private final int value;

/**
* Constructor for MapType enum.
*
* @param value The integer value of the map type
*/
StreamType(int value) {
this.value = value;
}

/**
* Get the integer value of this map type.
*
* @return The integer value
*/
public int getValue() {
return value;
}

/**
* Convert an integer value to a MapType.
*
* @param value The integer value to convert
* @return The corresponding MapType or null if the value doesn't match any MapType
*/
public static StreamType fromInt(int value) {
for (StreamType type : values()) {
if (type.value == value) {
return type;
}
}
return null;
}
}
Binary file modified src/test/go-examples/helloworld/main.wasm
Binary file not shown.
Binary file modified src/test/go-examples/http_body/main.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions src/test/go-examples/http_body_chunk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Attribution

This example originally came from:
https://github.com/proxy-wasm/proxy-wasm-go-sdk/tree/main/examples/http_body_chunk
5 changes: 5 additions & 0 deletions src/test/go-examples/http_body_chunk/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/proxy-wasm/proxy-wasm-go-sdk/examples/http_body

go 1.24

require github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924
10 changes: 10 additions & 0 deletions src/test/go-examples/http_body_chunk/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924 h1:wTcK6gcyTKJMeDka69AMjZYvisdI8CBXzTEfZ+2pOxI=
github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924/go.mod h1:9mBRvh8I6Td6sg3CwEY+zGFE4DKaIoieCaca1kQnDBE=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
112 changes: 112 additions & 0 deletions src/test/go-examples/http_body_chunk/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2020-2024 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 main

import (
"fmt"
"strings"

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

func main() {}
func init() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
}

// NewHttpContext implements types.PluginContext.
func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
return &setBodyContext{}
}

// OnPluginStart implements types.PluginContext.
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
return types.OnPluginStartStatusOK
}

// setBodyContext implements types.HttpContext.
type setBodyContext struct {
// Embed the default root http context here,
// so that we don't need to reimplement all the methods.
types.DefaultHttpContext
totalRequestBodyReadSize int
receivedChunks int
}

// OnHttpRequestBody implements types.HttpContext.
func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
proxywasm.LogInfof("OnHttpRequestBody called. BodySize: %d, totalRequestBodyReadSize: %d, endOfStream: %v", bodySize, ctx.totalRequestBodyReadSize, endOfStream)

// If some data has been received, we read it.
// Reading the body chunk by chunk, bodySize is the size of the current chunk, not the total size of the body.
chunkSize := bodySize - ctx.totalRequestBodyReadSize
if chunkSize > 0 {
ctx.receivedChunks++
chunk, err := proxywasm.GetHttpRequestBody(ctx.totalRequestBodyReadSize, chunkSize)
if err != nil {
proxywasm.LogCriticalf("failed to get request body: %v", err)
return types.ActionContinue
}
proxywasm.LogInfof("read chunk size: %d", len(chunk))
if len(chunk) != chunkSize {
proxywasm.LogErrorf("read data does not match the expected size: %d != %d", len(chunk), chunkSize)
}
ctx.totalRequestBodyReadSize += len(chunk)
if strings.Contains(string(chunk), "pattern") {
patternFound := fmt.Sprintf("pattern found in chunk: %d", ctx.receivedChunks)
proxywasm.LogInfo(patternFound)
if err := proxywasm.SendHttpResponse(403, [][2]string{
{"powered-by", "proxy-wasm-go-sdk"},
}, []byte(patternFound), -1); err != nil {
proxywasm.LogCriticalf("failed to send local response: %v", err)
_ = proxywasm.ResumeHttpRequest()
} else {
proxywasm.LogInfo("local 403 response sent")
}
return types.ActionPause
}
}

if !endOfStream {
// Wait until we see the entire body before sending the request upstream.
return types.ActionPause
}
// When endOfStream is true, we have received the entire body. We expect the total size is equal to the sum of the sizes of the chunks.
if ctx.totalRequestBodyReadSize != bodySize {
proxywasm.LogErrorf("read data does not match the expected total size: %d != %d", ctx.totalRequestBodyReadSize, bodySize)
}
proxywasm.LogInfof("pattern not found")
return types.ActionContinue
}
Binary file added src/test/go-examples/http_body_chunk/main.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/test/go-examples/properties/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Attribution

This example originally came from:
https://github.com/proxy-wasm/proxy-wasm-go-sdk/tree/main/examples/proxy-wasm-go-examples/properties
https://github.com/proxy-wasm/proxy-wasm-go-sdk/tree/main/examples/properties
Binary file modified src/test/go-examples/vm_plugin_configuration/main.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void echoRequest() throws StartException {
// Must be paused.
assertEquals(Action.PAUSE, action);

var response = handler.getSenthttpResponse();
var response = handler.getSentHttpResponse();
assertNotNull(response);
assertEquals(200, response.statusCode);
assertEquals("frame1...frame2...frame3...", string(response.body));
Expand Down
Loading