Skip to content

Commit

Permalink
feat: wasm support body-transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Feb 22, 2024
1 parent f537a1c commit 23614dd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/wasm-go/extensions/body-transformer/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
19 changes: 19 additions & 0 deletions plugins/wasm-go/extensions/body-transformer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/alibaba/higress/plugins/wasm-go/extensions/body-transformer

go 1.19

require (
github.com/alibaba/higress/plugins/wasm-go v0.0.0-00010101000000-000000000000
github.com/tidwall/gjson v1.14.3
)

require (
github.com/google/uuid v1.3.0 // indirect
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 // indirect
github.com/magefile/mage v1.14.0 // indirect
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

replace github.com/alibaba/higress/plugins/wasm-go => ../..
18 changes: 18 additions & 0 deletions plugins/wasm-go/extensions/body-transformer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c h1:OCUFXVGixHLfNjg6/QYEhv+jHJ5mRGhpEUVFv9eWPJE=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c/go.mod h1:5t/pWFNJ9eMyu/K/Z+OeGhDJ9sN9eCo8fc2pyM/Qjg4=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
72 changes: 72 additions & 0 deletions plugins/wasm-go/extensions/body-transformer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"net/http"

"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
)

func main() {
wrapper.SetCtx(
"body-transformer",
wrapper.ParseConfigBy(parseConfig),
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
wrapper.ProcessRequestBodyBy(onHttpRequestBody),
wrapper.ProcessResponseBodyBy(onHttpResponseBody),
)
}

type BodyTransformerConfig struct {
reqTemplate string
rspTemplate string
}

func parseConfig(json gjson.Result, config *BodyTransformerConfig, log wrapper.Log) error {
return nil
}

func onHttpResponseBody(ctx wrapper.HttpContext, config BodyTransformerConfig, body []byte, log wrapper.Log) types.Action {
decoders(formatParse(ctx))
return types.ActionContinue
}

func onHttpRequestHeaders(ctx wrapper.HttpContext, config BodyTransformerConfig, log wrapper.Log) types.Action {
decoders(formatParse(ctx))
return types.ActionContinue
}

func onHttpRequestBody(ctx wrapper.HttpContext, config BodyTransformerConfig, body []byte, log wrapper.Log) types.Action {
decoders(formatParse(ctx))
return types.ActionContinue
}

func formatParse(ctx wrapper.HttpContext) string {
var f string
if ctx.Method() == http.MethodGet {
f = "args"
}

cType, _ := proxywasm.GetHttpRequestHeader("Content-Type")
if len(f) == 0 && len(cType) != 0 {
if cType == "text/xml" {
f = "xml"
} else if cType == "application/json" {
f = "json"
} else if cType == "application/x-www-form-urlencoded" {
f = "encoded"
}
}
return f
}

func decoders(format string) {
switch format {
case "json":
case "xml":
case "encoded":
case "args":
}
}

0 comments on commit 23614dd

Please sign in to comment.