-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpassthrough.go
More file actions
137 lines (126 loc) · 4.45 KB
/
Copy pathpassthrough.go
File metadata and controls
137 lines (126 loc) · 4.45 KB
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
package main
import (
"fmt"
"github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go/shared"
)
type (
// passthroughFilterConfigFactory implements [shared.HttpFilterConfigFactory].
passthroughFilterConfigFactory struct {
shared.EmptyHttpFilterConfigFactory
}
// passthroughFilterFactory implements [shared.HttpFilterFactory].
passthroughFilterFactory struct{}
// passthroughFilter implements [shared.HttpFilter].
passthroughFilter struct {
handle shared.HttpFilterHandle
shared.EmptyHttpFilter
}
)
// Create implements [shared.HttpFilterConfigFactory].
func (p *passthroughFilterConfigFactory) Create(handle shared.HttpFilterConfigHandle, unparsedConfig []byte) (shared.HttpFilterFactory, error) {
return &passthroughFilterFactory{}, nil
}
// Create implements [shared.HttpFilterFactory].
func (p *passthroughFilterFactory) Create(handle shared.HttpFilterHandle) shared.HttpFilter {
return &passthroughFilter{handle: handle}
}
// OnRequestHeaders implements [shared.HttpFilter].
func (p *passthroughFilter) OnRequestHeaders(headers shared.HeaderMap, endOfStream bool) shared.HeadersStatus {
fooValue := headers.GetOne("foo")
fmt.Printf("gosdk: RequestHeaders, foo: %v\n", fooValue)
fmt.Printf("gosdk: RequestHeaders, endOfStream: %v\n", endOfStream)
for _, header := range headers.GetAll() {
fmt.Printf("gosdk: RequestHeaders, header: %s: %s\n", header[0], header[1])
}
sourceAddr, _ := p.handle.GetAttributeString(shared.AttributeIDSourceAddress)
destAddr, _ := p.handle.GetAttributeString(shared.AttributeIDDestinationAddress)
protocol, _ := p.handle.GetAttributeString(shared.AttributeIDRequestProtocol)
fmt.Printf("gosdk: RequestHeaders, source address: %s\n", sourceAddr)
fmt.Printf("gosdk: RequestHeaders, destination address: %s\n", destAddr)
fmt.Printf("gosdk: RequestHeaders, request protocol: %s\n", protocol)
return shared.HeadersStatusContinue
}
// OnRequestBody implements [shared.HttpFilter].
func (p *passthroughFilter) OnRequestBody(body shared.BodyBuffer, endOfStream bool) shared.BodyStatus {
if !endOfStream {
// Wait for the end of stream.
return shared.BodyStatusStopAndBuffer
}
fmt.Println("gosdk: RequestBody")
chunks := body.GetChunks()
var original []byte
for _, chunk := range chunks {
original = append(original, chunk...)
}
fmt.Printf("gosdk: RequestBody, body: %s\n", original)
body.Drain(uint64(len(original)))
body.Append([]byte("hello world"))
chunks = body.GetChunks()
var modified []byte
for _, chunk := range chunks {
modified = append(modified, chunk...)
}
if string(modified) != "hello world" {
panic("request body should be modified")
}
// Write it back.
body.Drain(uint64(len(modified)))
body.Append(original)
chunks = body.GetChunks()
modified = nil
for _, chunk := range chunks {
modified = append(modified, chunk...)
}
if string(modified) != string(original) {
panic("request body should be modified")
}
return shared.BodyStatusContinue
}
// OnResponseHeaders implements [shared.HttpFilter].
func (p *passthroughFilter) OnResponseHeaders(headers shared.HeaderMap, endOfStream bool) shared.HeadersStatus {
status := headers.GetOne(":status")
if status == "" {
panic("x-status header should be set")
}
fmt.Printf("gosdk: ResponseHeaders, status: %v\n", status)
headers.Set("x-passthrough-response-header", "true")
for _, header := range headers.GetAll() {
fmt.Printf("gosdk: ResponseHeaders, header: %s: %s\n", header[0], header[1])
}
return shared.HeadersStatusContinue
}
// OnResponseBody implements [shared.HttpFilter].
func (p *passthroughFilter) OnResponseBody(body shared.BodyBuffer, endOfStream bool) shared.BodyStatus {
if !endOfStream {
// Wait for the end of stream.
return shared.BodyStatusStopAndBuffer
}
chunks := body.GetChunks()
var original []byte
for _, chunk := range chunks {
original = append(original, chunk...)
}
fmt.Printf("gosdk: ResponseBody, body: %s\n", original)
body.Drain(uint64(len(original)))
body.Append([]byte("hello world"))
chunks = body.GetChunks()
var modified []byte
for _, chunk := range chunks {
modified = append(modified, chunk...)
}
if string(modified) != "hello world" {
panic("response body should be modified")
}
// Write it back.
body.Drain(uint64(len(modified)))
body.Append(original)
chunks = body.GetChunks()
modified = nil
for _, chunk := range chunks {
modified = append(modified, chunk...)
}
if string(modified) != string(original) {
panic("response body should be modified")
}
return shared.BodyStatusContinue
}