-
Notifications
You must be signed in to change notification settings - Fork 0
/
body-rewrite.go
79 lines (60 loc) · 1.91 KB
/
body-rewrite.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
package main
import (
ep "github.com/wrossmorrow/envoy-extproc-sdk-go"
"log"
)
type bodyRewriteRequestProcessor struct {
opts *ep.ProcessingOptions
bodyRewrite string
}
func (s *bodyRewriteRequestProcessor) GetName() string {
return "body-rewrite"
}
func (s *bodyRewriteRequestProcessor) GetOptions() *ep.ProcessingOptions {
return s.opts
}
func (s *bodyRewriteRequestProcessor) ProcessRequestHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error {
return ctx.ContinueRequest()
}
func (s *bodyRewriteRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error {
ctx.ReplaceBodyChunk([]byte(s.bodyRewrite))
return ctx.ContinueRequest()
}
func (s *bodyRewriteRequestProcessor) ProcessRequestTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error {
return ctx.ContinueRequest()
}
func (s *bodyRewriteRequestProcessor) ProcessResponseHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error {
return ctx.ContinueRequest()
}
func (s *bodyRewriteRequestProcessor) ProcessResponseBody(ctx *ep.RequestContext, body []byte) error {
return ctx.ContinueRequest()
}
func (s *bodyRewriteRequestProcessor) ProcessResponseTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error {
return ctx.ContinueRequest()
}
const kbodyRewrite = "body-rewrite"
func (s *bodyRewriteRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error {
s.opts = opts
s.bodyRewrite = "this is a replacement body."
var (
i int
bodyRewrite string
)
nArgs := len(nonFlagArgs)
for ; i < nArgs-1; i++ {
if nonFlagArgs[i] == kbodyRewrite {
break
}
}
if i == nArgs {
log.Printf("the argument: 'body-rewrite' is missing, use the default.\n")
return nil
}
bodyRewrite = nonFlagArgs[i+1]
if len(bodyRewrite) > 0 {
s.bodyRewrite = bodyRewrite
log.Printf("the body rewrite is: %s\n", s.bodyRewrite)
}
return nil
}
func (s *bodyRewriteRequestProcessor) Finish() {}