-
Notifications
You must be signed in to change notification settings - Fork 546
/
handler.go
168 lines (150 loc) · 4.01 KB
/
handler.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto/tls"
"io"
"log"
"net"
"net/http"
"time"
)
type Net2 interface {
NetResolveIPAddr(network, addr string) (*net.IPAddr, error)
NetDialTimeout(network, address string, timeout time.Duration) (net.Conn, error)
TlsDialTimeout(network, address string, config *tls.Config, timeout time.Duration) (*tls.Conn, error)
HttpClientDo(req *http.Request) (*http.Response, error)
CopyResponseBody(w io.Writer, res *http.Response) (int64, error)
GetTimeout() time.Duration
SetTimeout()
GetAddressAlias(addr string) (alias string)
}
type RequestFilter interface {
Filter(req *http.Request) (pluginName string, pluginArgs *http.Header, err error)
}
type ResponseFilter interface {
Filter(req *http.Request, res *http.Response) (pluginName string, pluginArgs *http.Header, err error)
}
type PushListener interface {
net.Listener
Push(net.Conn, error)
}
type listenerAcceptTuple struct {
c net.Conn
e error
}
type listener struct {
net.Listener
ln net.Listener
ch chan listenerAcceptTuple
}
func Listen(network string, addr string) (net.Listener, error) {
ln, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
l := &listener{
ln: ln,
ch: make(chan listenerAcceptTuple, 200),
}
// http://golang.org/src/pkg/net/http/server.go
go func(ln net.Listener, ch chan listenerAcceptTuple) {
var tempDelay time.Duration
for {
c, e := ln.Accept()
ch <- listenerAcceptTuple{c, e}
if e != nil {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay)
time.Sleep(tempDelay)
continue
}
return
}
}
}(l.ln, l.ch)
return l, nil
}
func (l listener) Accept() (net.Conn, error) {
t := <-l.ch
return t.c, t.e
}
func (l listener) CLose() error {
return l.ln.Close()
}
func (l listener) Addr() net.Addr {
return l.ln.Addr()
}
func (l listener) Push(conn net.Conn, err error) {
l.ch <- listenerAcceptTuple{conn, err}
}
type Handler struct {
http.Handler
Listener net.Listener
Log *log.Logger
Net Net2
RequestPlugins map[string]RequestPlugin
ResponsePlugins map[string]ResponsePlugin
RequestFilters []RequestFilter
ResponseFilters []ResponseFilter
}
type PluginContext struct {
H *Handler
Args *http.Header
}
type RequestPlugin interface {
HandleRequest(*PluginContext, http.ResponseWriter, *http.Request) (*http.Response, error)
}
type ResponsePlugin interface {
HandleResponse(*PluginContext, http.ResponseWriter, *http.Request, *http.Response, error) error
}
type Plugin interface {
RequestPlugin
ResponsePlugin
}
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for _, reqfilter := range h.RequestFilters {
name, args, err := reqfilter.Filter(req)
if err != nil {
h.Log.Printf("RequestFilter error: %v", err)
}
if name == "" {
continue
}
if reqplugin, ok := h.RequestPlugins[name]; ok {
reqctx := &PluginContext{&h, args}
res, err := reqplugin.HandleRequest(reqctx, rw, req)
if err != nil {
h.Log.Printf("Plugin %s HandleResponse error: %v", name, err)
}
if res != nil {
for _, resfilter := range h.ResponseFilters {
name, args, err := resfilter.Filter(req, res)
if err != nil {
h.Log.Printf("ServeHTTP RequestFilter error: %v", err)
}
if name == "" {
continue
}
if resplugin, ok := h.ResponsePlugins[name]; ok {
resctx := &PluginContext{&h, args}
err := resplugin.HandleResponse(resctx, rw, req, res, err)
if err != nil {
h.Log.Printf("Plugin %s HandleResponse error: %v", name, err)
}
}
break
}
}
break
} else {
h.Log.Fatalf("plugin \"%s\" not registered", name)
}
}
}