-
Notifications
You must be signed in to change notification settings - Fork 22
/
plugin.go
99 lines (86 loc) · 2.75 KB
/
plugin.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
package intercept
/*
// Plugin is the instance of the resulting composition of all the plugins. This is likely
// to be changed in future versions so please do not use/modify this.
var Plugin PlugHandler
// PlugHandler is the resulting composition of all the plugins. This is likely
// to be changed in future versions so please do not use/modify this.
type PlugHandler struct {
sync.Mutex
used bool
//Called if request is intercepted and before the buffer is sento to the UI for editing
preModifyRequest RequestModifier
//called on every request before preModifyRequest
alwaysModifyRequest RequestModifier
//Called if request is intercepted and after the buffer has been modified
postModifyRequest RequestModifier
//Called if response is intercepted and before the buffer is sento to the UI for editing
preModifyResponse ResponseModifier
//called on every response before preModifyRequest
alwaysModifyRespone ResponseModifier
//Called if response is intercepted and after the buffer has been modified
postModifyResponse ResponseModifier
}
type RequestModifier func(*http.Request) (*http.Request, error)
func (p *PlugHandler) PreProcessRequest(rm RequestModifier, pre bool) {
p.Lock()
defer p.Unlock()
addRequestModifier(&p.preModifyRequest, rm, pre)
}
func (p *PlugHandler) PostProcessRequest(rm RequestModifier, pre bool) {
p.Lock()
defer p.Unlock()
addRequestModifier(&p.postModifyRequest, rm, pre)
}
func addRequestModifier(field *RequestModifier, rm RequestModifier, pre bool) {
if *field == nil {
*field = rm
} else {
if pre {
*field = composeRequestModifier(*field, rm)
} else {
*field = composeRequestModifier(rm, *field)
}
}
}
func composeRequestModifier(a RequestModifier, b RequestModifier) RequestModifier {
return func(r *http.Request) (*http.Request, error) {
out, err := b(r)
if err != nil {
return nil, err
}
return a(out)
}
}
type ResponseModifier func(*http.Request, *http.Response) (*http.Response, error)
func (p *PlugHandler) PreProcessResponse(rm ResponseModifier, pre bool) {
p.Lock()
defer p.Unlock()
addResponseModifier(&p.preModifyResponse, rm, pre)
}
func (p *PlugHandler) PostProcessResponse(rm ResponseModifier, pre bool) {
p.Lock()
defer p.Unlock()
addResponseModifier(&p.postModifyResponse, rm, pre)
}
func addResponseModifier(field *ResponseModifier, rm ResponseModifier, pre bool) {
if *field == nil {
*field = rm
} else {
if pre {
*field = composeResponseModifier(*field, rm)
} else {
*field = composeResponseModifier(rm, *field)
}
}
}
func composeResponseModifier(a ResponseModifier, b ResponseModifier) ResponseModifier {
return func(req *http.Request, in *http.Response) (*http.Response, error) {
out, err := b(req, in)
if err != nil {
return nil, err
}
return a(req, out)
}
}
*/