/
filter.go
139 lines (117 loc) · 4.36 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"strconv"
"github.com/envoyproxy/envoy/contrib/golang/common/go/api"
)
var UpdateUpstreamBody = "upstream response body updated by the simple plugin"
// The callbacks in the filter, like `DecodeHeaders`, can be implemented on demand.
// Because api.PassThroughStreamFilter provides a default implementation.
type filter struct {
api.PassThroughStreamFilter
callbacks api.FilterCallbackHandler
path string
config *config
}
func (f *filter) sendLocalReplyInternal() api.StatusType {
body := fmt.Sprintf("%s, path: %s\r\n", f.config.echoBody, f.path)
f.callbacks.SendLocalReply(200, body, nil, 0, "")
// Remember to return LocalReply when the request is replied locally
return api.LocalReply
}
// Callbacks which are called in request path
// The endStream is true if the request doesn't have body
func (f *filter) DecodeHeaders(header api.RequestHeaderMap, endStream bool) api.StatusType {
f.path, _ = header.Get(":path")
api.LogDebugf("get path %s", f.path)
if f.path == "/localreply_by_config" {
return f.sendLocalReplyInternal()
}
return api.Continue
/*
// If the code is time-consuming, to avoid blocking the Envoy,
// we need to run the code in a background goroutine
// and suspend & resume the filter
go func() {
defer f.callbacks.RecoverPanic()
// do time-consuming jobs
// resume the filter
f.callbacks.Continue(status)
}()
// suspend the filter
return api.Running
*/
}
// DecodeData might be called multiple times during handling the request body.
// The endStream is true when handling the last piece of the body.
func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
// support suspending & resuming the filter in a background goroutine
return api.Continue
}
func (f *filter) DecodeTrailers(trailers api.RequestTrailerMap) api.StatusType {
// support suspending & resuming the filter in a background goroutine
return api.Continue
}
// Callbacks which are called in response path
// The endStream is true if the response doesn't have body
func (f *filter) EncodeHeaders(header api.ResponseHeaderMap, endStream bool) api.StatusType {
if f.path == "/update_upstream_response" {
header.Set("Content-Length", strconv.Itoa(len(UpdateUpstreamBody)))
}
header.Set("Rsp-Header-From-Go", "bar-test")
// support suspending & resuming the filter in a background goroutine
return api.Continue
}
// EncodeData might be called multiple times during handling the response body.
// The endStream is true when handling the last piece of the body.
func (f *filter) EncodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
if f.path == "/update_upstream_response" {
if endStream {
buffer.SetString(UpdateUpstreamBody)
} else {
buffer.Reset()
}
}
// support suspending & resuming the filter in a background goroutine
return api.Continue
}
func (f *filter) EncodeTrailers(trailers api.ResponseTrailerMap) api.StatusType {
return api.Continue
}
// OnLog is called when the HTTP stream is ended on HTTP Connection Manager filter.
func (f *filter) OnLog() {
code, _ := f.callbacks.StreamInfo().ResponseCode()
respCode := strconv.Itoa(int(code))
api.LogDebug(respCode)
/*
// It's possible to kick off a goroutine here.
// But it's unsafe to access the f.callbacks because the FilterCallbackHandler
// may be already released when the goroutine is scheduled.
go func() {
defer func() {
if p := recover(); p != nil {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
fmt.Printf("http: panic serving: %v\n%s", p, buf)
}
}()
// do time-consuming jobs
}()
*/
}
// OnLogDownstreamStart is called when HTTP Connection Manager filter receives a new HTTP request
// (required the corresponding access log type is enabled)
func (f *filter) OnLogDownstreamStart() {
// also support kicking off a goroutine here, like OnLog.
}
// OnLogDownstreamPeriodic is called on any HTTP Connection Manager periodic log record
// (required the corresponding access log type is enabled)
func (f *filter) OnLogDownstreamPeriodic() {
// also support kicking off a goroutine here, like OnLog.
}
func (f *filter) OnDestroy(reason api.DestroyReason) {
// One should not access f.callbacks here because the FilterCallbackHandler
// is released. But we can still access other Go fields in the filter f.
// goroutine can be used everywhere.
}