|
| 1 | +// Copyright 2020-2024 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/binary" |
| 19 | + "errors" |
| 20 | + |
| 21 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm" |
| 22 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + sharedDataKey = "shared_data_key" |
| 27 | +) |
| 28 | + |
| 29 | +func main() {} |
| 30 | +func init() { |
| 31 | + proxywasm.SetVMContext(&vmContext{}) |
| 32 | +} |
| 33 | + |
| 34 | +type ( |
| 35 | + // vmContext implements types.VMContext. |
| 36 | + vmContext struct{} |
| 37 | + // pluginContext implements types.PluginContext. |
| 38 | + pluginContext struct { |
| 39 | + // Embed the default plugin context here, |
| 40 | + // so that we don't need to reimplement all the methods. |
| 41 | + types.DefaultPluginContext |
| 42 | + } |
| 43 | + // httpContext implements types.HttpContext. |
| 44 | + httpContext struct { |
| 45 | + // Embed the default http context here, |
| 46 | + // so that we don't need to reimplement all the methods. |
| 47 | + types.DefaultHttpContext |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +// OnVMStart implements types.VMContext. |
| 52 | +func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus { |
| 53 | + initialValueBuf := make([]byte, 0) // Empty data to indicate that the data is not initialized. |
| 54 | + if err := proxywasm.SetSharedData(sharedDataKey, initialValueBuf, 0); err != nil { |
| 55 | + proxywasm.LogWarnf("error setting shared data on OnVMStart: %v", err) |
| 56 | + } |
| 57 | + return types.OnVMStartStatusOK |
| 58 | +} |
| 59 | + |
| 60 | +// NewPluginContext implements types.VMContext. |
| 61 | +func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { |
| 62 | + return &pluginContext{} |
| 63 | +} |
| 64 | + |
| 65 | +// NewHttpContext implements types.PluginContext. |
| 66 | +func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext { |
| 67 | + return &httpContext{} |
| 68 | +} |
| 69 | + |
| 70 | +// OnHttpRequestHeaders implements types.HttpContext. |
| 71 | +func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { |
| 72 | + for { |
| 73 | + value, err := ctx.incrementData() |
| 74 | + if err == nil { |
| 75 | + proxywasm.LogInfof("shared value: %d", value) |
| 76 | + } else if errors.Is(err, types.ErrorStatusCasMismatch) { |
| 77 | + continue |
| 78 | + } |
| 79 | + break |
| 80 | + } |
| 81 | + return types.ActionContinue |
| 82 | +} |
| 83 | + |
| 84 | +// incrementData increments the shared data value by 1. |
| 85 | +func (ctx *httpContext) incrementData() (uint64, error) { |
| 86 | + data, cas, err := proxywasm.GetSharedData(sharedDataKey) |
| 87 | + if err != nil { |
| 88 | + proxywasm.LogWarnf("error getting shared data on OnHttpRequestHeaders: %v", err) |
| 89 | + return 0, err |
| 90 | + } |
| 91 | + |
| 92 | + var nextValue uint64 |
| 93 | + if len(data) > 0 { |
| 94 | + nextValue = binary.LittleEndian.Uint64(data) + 1 |
| 95 | + } else { |
| 96 | + nextValue = 1 |
| 97 | + } |
| 98 | + |
| 99 | + buf := make([]byte, 8) |
| 100 | + binary.LittleEndian.PutUint64(buf, nextValue) |
| 101 | + if err := proxywasm.SetSharedData(sharedDataKey, buf, cas); err != nil { |
| 102 | + proxywasm.LogWarnf("error setting shared data on OnHttpRequestHeaders: %v", err) |
| 103 | + return 0, err |
| 104 | + } |
| 105 | + return nextValue, err |
| 106 | +} |
0 commit comments