-
Notifications
You must be signed in to change notification settings - Fork 5
/
mux.go
240 lines (224 loc) · 6.49 KB
/
mux.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"fmt"
"sync"
)
// Mux is an ldap request multiplexer. It matches the inbound request against a
// list of registered route handlers. Routes are matched in the order they're
// added and only one route is called per request.
type Mux struct {
mu sync.Mutex
routes []route
defaultRoute route
unbindRoute route
}
// NewMux creates a new multiplexer.
func NewMux(opt ...Option) (*Mux, error) {
return &Mux{
routes: []route{},
}, nil
}
// Bind will register a handler for bind requests.
// Options supported: WithLabel
func (m *Mux) Bind(bindFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Bind"
if bindFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &simpleBindRoute{
baseRoute: &baseRoute{
h: bindFn,
routeOp: bindRouteOperation,
label: opts.withLabel,
},
authChoice: SimpleAuthChoice,
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// Unbind will register a handler for unbind requests and override the default
// unbind handler. Registering an unbind handler is optional and regardless of
// whether or not an unbind route is defined the server will stop serving
// requests for a connection after an unbind request is received. Options
// supported: WithLabel
func (m *Mux) Unbind(bindFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Unbind"
if bindFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &unbindRoute{
baseRoute: &baseRoute{
h: bindFn,
routeOp: bindRouteOperation,
label: opts.withLabel,
},
}
m.mu.Lock()
defer m.mu.Unlock()
m.unbindRoute = r
return nil
}
// Search will register a handler for search requests.
// Options supported: WithLabel, WithBaseDN, WithScope
func (m *Mux) Search(searchFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Search"
if searchFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &searchRoute{
baseRoute: &baseRoute{
h: searchFn,
routeOp: searchRouteOperation,
label: opts.withLabel,
},
basedn: opts.withBaseDN,
filter: opts.withFilter,
scope: opts.withScope,
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// ExtendedOperation will register a handler for extended operation requests.
// Options supported: WithLabel
func (m *Mux) ExtendedOperation(operationFn HandlerFunc, exName ExtendedOperationName, opt ...Option) error {
const op = "gldap.(Mux).Search"
if operationFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &extendedRoute{
baseRoute: &baseRoute{
h: operationFn,
routeOp: extendedRouteOperation,
label: opts.withLabel,
},
extendedName: exName,
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// Modify will register a handler for modify operation requests.
// Options supported: WithLabel
func (m *Mux) Modify(modifyFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Modify"
if modifyFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &modifyRoute{
baseRoute: &baseRoute{
h: modifyFn,
routeOp: modifyRouteOperation,
label: opts.withLabel,
},
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// Add will register a handler for add operation requests.
// Options supported: WithLabel
func (m *Mux) Add(addFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Add"
if addFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &addRoute{
baseRoute: &baseRoute{
h: addFn,
routeOp: addRouteOperation,
label: opts.withLabel,
},
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// Delete will register a handler for delete operation requests.
// Options supported: WithLabel
func (m *Mux) Delete(modifyFn HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Delete"
if modifyFn == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
opts := getRouteOpts(opt...)
r := &deleteRoute{
baseRoute: &baseRoute{
h: modifyFn,
routeOp: deleteRouteOperation,
label: opts.withLabel,
},
}
m.mu.Lock()
defer m.mu.Unlock()
m.routes = append(m.routes, r)
return nil
}
// DefaultRoute will register a default handler requests which have no other
// registered handler.
func (m *Mux) DefaultRoute(noRouteFN HandlerFunc, opt ...Option) error {
const op = "gldap.(Mux).Bind"
if noRouteFN == nil {
return fmt.Errorf("%s: missing HandlerFunc: %w", op, ErrInvalidParameter)
}
r := &baseRoute{
h: noRouteFN,
routeOp: bindRouteOperation,
}
m.mu.Lock()
defer m.mu.Unlock()
m.defaultRoute = r
return nil
}
// serveRequests will find a matching route to serve the request
func (m *Mux) serve(w *ResponseWriter, req *Request) {
const op = "gldap.(Mux).serve"
defer func() {
w.logger.Debug("finished serving request", "op", op, "connID", w.connID, "requestID", w.requestID)
}()
if w == nil {
// this should be unreachable, and if it is then we'll just panic
panic(fmt.Errorf("%s: %d/%d missing response writer: %w", op, w.connID, w.requestID, ErrInternal).Error())
}
if req == nil {
w.logger.Error("missing request", "op", op, "connID", w.connID, "requestID", w.requestID)
return
}
// find the first matching route to dispatch the request to and then return
for _, r := range m.routes {
if !r.match(req) {
continue
}
h := r.handler()
if h == nil {
w.logger.Error("route is missing handler", "op", op, "connID", w.connID, "requestID", w.requestID, "route", r.op)
return
}
// the handler intentionally doesn't return errors, since we want the
// handler to response to the connection's client with errors.
h(w, req)
return
}
if m.defaultRoute != nil {
h := m.defaultRoute.handler()
h(w, req)
return
}
w.logger.Error("no matching handler found for request and returning internal error", "op", op, "connID", w.connID, "requestID", w.requestID, "routeOp", req.routeOp)
resp := req.NewResponse(WithResponseCode(ResultUnwillingToPerform), WithDiagnosticMessage("No matching handler found"))
_ = w.Write(resp)
}