-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathroute.go
More file actions
190 lines (162 loc) · 3.86 KB
/
route.go
File metadata and controls
190 lines (162 loc) · 3.86 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ldapserver
import "reflect"
// Constant to LDAP Request protocol Type names
const (
SEARCH = "SearchRequest"
BIND = "BindRequest"
COMPARE = "CompareRequest"
ADD = "AddRequest"
MODIFY = "ModifyRequest"
DELETE = "DeleteRequest"
EXTENDED = "ExtendedRequest"
ABANDON = "AbandonRequest"
)
// HandlerFunc type is an adapter to allow the use of
// ordinary functions as LDAP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler object that calls f.
type HandlerFunc func(ResponseWriter, *Message)
// RouteMux manages all routes
type RouteMux struct {
routes []*route
notFoundRoute *route
}
type route struct {
operation string
handler HandlerFunc
exoName LDAPOID
sBasedn string
sFilter string
}
// Match return true when the *Message matches the route
// conditions
func (r *route) Match(m *Message) bool {
if reflect.TypeOf(m.protocolOp).Name() != r.operation {
return false
}
switch v := m.protocolOp.(type) {
case ExtendedRequest:
if "" != r.exoName {
if v.GetResponseName() == r.exoName {
return true
}
return false
}
case SearchRequest:
if "" != r.sBasedn {
if string(v.GetBaseObject()) == r.sBasedn {
return true
}
return false
}
}
return true
}
func (r *route) BaseDn(dn string) *route {
r.sBasedn = dn
return r
}
func (r *route) Filter(pattern string) *route {
r.sFilter = pattern
return r
}
func (r *route) RequestName(name LDAPOID) *route {
r.exoName = name
return r
}
// NewRouteMux returns a new *RouteMux
// RouteMux implements ldapserver.Handler
func NewRouteMux() *RouteMux {
return &RouteMux{}
}
// Handler interface used to serve a LDAP Request message
type Handler interface {
ServeLDAP(w ResponseWriter, r *Message)
}
// ServeLDAP dispatches the request to the handler whose
// pattern most closely matches the request request Message.
func (h *RouteMux) ServeLDAP(w ResponseWriter, r *Message) {
//find a matching Route
for _, route := range h.routes {
//if the route don't match, skip it
if route.Match(r) == false {
continue
}
route.handler(w, r)
return
}
if h.notFoundRoute != nil {
h.notFoundRoute.handler(w, r)
} else {
res := NewResponse(LDAPResultUnwillingToPerform)
res.DiagnosticMessage = "Operation not implemented by server"
w.Write(res)
}
}
// Adds a new Route to the Handler
func (h *RouteMux) addRoute(r *route) {
//and finally append to the list of Routes
//create the Route
h.routes = append(h.routes, r)
}
func (h *RouteMux) NotFound(handler HandlerFunc) {
route := &route{}
route.handler = handler
h.notFoundRoute = route
}
func (h *RouteMux) Bind(handler HandlerFunc) *route {
route := &route{}
route.operation = BIND
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Search(handler HandlerFunc) *route {
route := &route{}
route.operation = SEARCH
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Add(handler HandlerFunc) *route {
route := &route{}
route.operation = ADD
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Delete(handler HandlerFunc) *route {
route := &route{}
route.operation = DELETE
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Modify(handler HandlerFunc) *route {
route := &route{}
route.operation = MODIFY
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Compare(handler HandlerFunc) *route {
route := &route{}
route.operation = COMPARE
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Extended(handler HandlerFunc) *route {
route := &route{}
route.operation = EXTENDED
route.handler = handler
h.addRoute(route)
return route
}
func (h *RouteMux) Abandon(handler HandlerFunc) *route {
route := &route{}
route.operation = ABANDON
route.handler = handler
h.addRoute(route)
return route
}