1
- // Copyright 2020-2021 Tetrate
1
+ // Copyright 2020-2024 Tetrate
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -29,33 +29,35 @@ func main() {
29
29
proxywasm .SetVMContext (& vmContext {})
30
30
}
31
31
32
+ // vmContext implements types.VMContext.
32
33
type vmContext struct {
33
34
// Embed the default VM context here,
34
35
// so that we don't need to reimplement all the methods.
35
36
types.DefaultVMContext
36
37
}
37
38
38
- // Override types.DefaultVMContext .
39
+ // NewPluginContext implements types.VMContext .
39
40
func (* vmContext ) NewPluginContext (contextID uint32 ) types.PluginContext {
40
41
return & pluginContext {}
41
42
}
42
43
44
+ // pluginContext implements types.PluginContext.
43
45
type pluginContext struct {
44
46
// Embed the default plugin context here,
45
47
// so that we don't need to reimplement all the methods.
46
48
types.DefaultPluginContext
47
49
shouldEchoBody bool
48
50
}
49
51
50
- // Override types.DefaultPluginContext .
52
+ // NewHttpContext implements types.PluginContext .
51
53
func (ctx * pluginContext ) NewHttpContext (contextID uint32 ) types.HttpContext {
52
54
if ctx .shouldEchoBody {
53
55
return & echoBodyContext {}
54
56
}
55
57
return & setBodyContext {}
56
58
}
57
59
58
- // Override types.DefaultPluginContext .
60
+ // OnPluginStart implements types.PluginContext .
59
61
func (ctx * pluginContext ) OnPluginStart (pluginConfigurationSize int ) types.OnPluginStartStatus {
60
62
data , err := proxywasm .GetPluginConfiguration ()
61
63
if err != nil {
@@ -65,6 +67,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
65
67
return types .OnPluginStartStatusOK
66
68
}
67
69
70
+ // setBodyContext implements types.HttpContext.
68
71
type setBodyContext struct {
69
72
// Embed the default root http context here,
70
73
// so that we don't need to reimplement all the methods.
@@ -73,7 +76,7 @@ type setBodyContext struct {
73
76
bufferOperation string
74
77
}
75
78
76
- // Override types.DefaultHttpContext .
79
+ // OnHttpRequestHeaders implements types.HttpContext .
77
80
func (ctx * setBodyContext ) OnHttpRequestHeaders (numHeaders int , endOfStream bool ) types.Action {
78
81
mode , err := proxywasm .GetHttpRequestHeader ("buffer-replace-at" )
79
82
if err == nil && mode == "response" {
@@ -104,7 +107,7 @@ func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool
104
107
return types .ActionContinue
105
108
}
106
109
107
- // Override types.DefaultHttpContext .
110
+ // OnHttpRequestBody implements types.HttpContext .
108
111
func (ctx * setBodyContext ) OnHttpRequestBody (bodySize int , endOfStream bool ) types.Action {
109
112
if ctx .modifyResponse {
110
113
return types .ActionContinue
@@ -137,7 +140,7 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ
137
140
return types .ActionContinue
138
141
}
139
142
140
- // Override types.DefaultHttpContext .
143
+ // OnHttpResponseHeaders implements types.HttpContext .
141
144
func (ctx * setBodyContext ) OnHttpResponseHeaders (numHeaders int , endOfStream bool ) types.Action {
142
145
if ! ctx .modifyResponse {
143
146
return types .ActionContinue
@@ -151,7 +154,7 @@ func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream boo
151
154
return types .ActionContinue
152
155
}
153
156
154
- // Override types.DefaultHttpContext .
157
+ // OnHttpResponseBody implements types.HttpContext .
155
158
func (ctx * setBodyContext ) OnHttpResponseBody (bodySize int , endOfStream bool ) types.Action {
156
159
if ! ctx .modifyResponse {
157
160
return types .ActionContinue
@@ -184,14 +187,15 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty
184
187
return types .ActionContinue
185
188
}
186
189
190
+ // echoBodyContext implements types.HttpContext.
187
191
type echoBodyContext struct {
188
192
// Embed the default plugin context
189
193
// so that you don't need to reimplement all the methods by yourself.
190
194
types.DefaultHttpContext
191
195
totalRequestBodySize int
192
196
}
193
197
194
- // Override types.DefaultHttpContext .
198
+ // OnHttpRequestBody implements types.HttpContext .
195
199
func (ctx * echoBodyContext ) OnHttpRequestBody (bodySize int , endOfStream bool ) types.Action {
196
200
ctx .totalRequestBodySize = bodySize
197
201
if ! endOfStream {
0 commit comments