|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" |
| 8 | + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" |
| 9 | + "github.com/tidwall/gjson" |
| 10 | +) |
| 11 | + |
| 12 | +var _ types.PluginContext = (*pluginContext)(nil) |
| 13 | + |
| 14 | +type pluginContext struct { |
| 15 | + types.DefaultPluginContext |
| 16 | + |
| 17 | + configuration *pluginConfiguration |
| 18 | +} |
| 19 | + |
| 20 | +func (c *pluginContext) NewHttpContext(_ uint32) types.HttpContext { |
| 21 | + return &httpContext{ |
| 22 | + configuration: c.configuration, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (c *pluginContext) OnPluginStart(_ int) types.OnPluginStartStatus { |
| 27 | + config, err := getPluginConfiguration() |
| 28 | + if err != nil { |
| 29 | + proxywasm.LogErrorf("failed to get the plugin configuration: %s", err) |
| 30 | + return types.OnPluginStartStatusFailed |
| 31 | + } |
| 32 | + |
| 33 | + c.configuration = config |
| 34 | + |
| 35 | + return types.OnPluginStartStatusOK |
| 36 | +} |
| 37 | + |
| 38 | +func getPluginConfiguration() (*pluginConfiguration, error) { |
| 39 | + config, err := proxywasm.GetPluginConfiguration() |
| 40 | + if err != nil { |
| 41 | + if err == types.ErrorStatusNotFound { |
| 42 | + return nil, errors.New("the plugin configuration is not found") |
| 43 | + } |
| 44 | + |
| 45 | + return nil, fmt.Errorf("failed to get the plugin configuration: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + if len(config) == 0 { |
| 49 | + return nil, errors.New("the plugin configuration is empty") |
| 50 | + } |
| 51 | + |
| 52 | + if !gjson.ValidBytes(config) { |
| 53 | + return nil, errors.New("the plugin configuration is not valid JSON") |
| 54 | + } |
| 55 | + |
| 56 | + jsonConfig := gjson.ParseBytes(config) |
| 57 | + |
| 58 | + requestHeadersToRename := jsonConfig.Get(configKeyRequestHeadersToRename).Array() |
| 59 | + if len(requestHeadersToRename) == 0 { |
| 60 | + return nil, errors.New("the request headers to rename are not found") |
| 61 | + } |
| 62 | + |
| 63 | + headersToRename := make([]requestHeaderToRename, len(requestHeadersToRename)) |
| 64 | + |
| 65 | + for i, header := range requestHeadersToRename { |
| 66 | + h := header.Get(configKeyHeader) |
| 67 | + |
| 68 | + key := h.Get(configKeyKey).String() |
| 69 | + if key == "" { |
| 70 | + return nil, errors.New("the header key for renaming is empty") |
| 71 | + } |
| 72 | + |
| 73 | + value := h.Get(configKeyValue).String() |
| 74 | + if value == "" { |
| 75 | + return nil, errors.New("the header value for renaming is empty") |
| 76 | + } |
| 77 | + |
| 78 | + headersToRename[i] = requestHeaderToRename{ |
| 79 | + header: headerValue{ |
| 80 | + key: key, |
| 81 | + value: value, |
| 82 | + }, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return &pluginConfiguration{ |
| 87 | + requestHeadersToRename: headersToRename, |
| 88 | + }, nil |
| 89 | +} |
0 commit comments