-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor.go
57 lines (43 loc) · 973 Bytes
/
interceptor.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
package router
import (
"net/http"
)
const HeaderFlagDoNotIntercept = "do_not_intercept"
type excludeHeaderWriter struct {
http.ResponseWriter
excludedHeaders []string
}
func (w *excludeHeaderWriter) WriteHeader(statusCode int) {
for _, header := range w.excludedHeaders {
w.Header().Del(header)
}
w.ResponseWriter.WriteHeader(statusCode)
}
type routingStatusInterceptWriter struct {
http.ResponseWriter
interceptMap map[int]func() bool
statusCode int
intercepted bool
}
func (w *routingStatusInterceptWriter) WriteHeader(statusCode int) {
if w.intercepted {
return
}
w.statusCode = statusCode
for code, fn := range w.interceptMap {
if w.intercepted {
return
}
if code == statusCode && fn() {
w.intercepted = true
return
}
}
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *routingStatusInterceptWriter) Write(data []byte) (int, error) {
if w.intercepted {
return 0, nil
}
return w.ResponseWriter.Write(data)
}