/
router.go
186 lines (165 loc) · 4.45 KB
/
router.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
package cheshire
import (
"log"
"path"
"strings"
"sync"
)
// The Cheshire Router, translates between a uri + method to a controller
type RouteMatcher interface {
// A controller matches the given method, path
Match(string, string) Controller
// Registers a controller for the specified methods
Register([]string, Controller)
}
// This is a default implementation of a cheshire Router.
// it is based on the HTTP request multiplexer from golang sources:
// http://golang.org/src/pkg/net/http/server.go?s=25470:25535#L841
//
// Patterns name fixed, rooted paths, like "/favicon.ico",
// or rooted subtrees, like "/images/" (note the trailing slash).
// Longer patterns take precedence over shorter ones, so that
// if there are handlers registered for both "/images/"
// and "/images/thumbnails/", the latter handler will be
// called for paths beginning "/images/thumbnails/" and the
// former will receive requests for any other paths in the
// "/images/" subtree.
//
// Should also takes care of sanitizing the URL request path,
// redirecting any request containing . or .. elements to an
// equivalent .- and ..-free URL.
type Router struct {
mu sync.RWMutex
gets map[string]muxEntry
posts map[string]muxEntry
deletes map[string]muxEntry
puts map[string]muxEntry
NotFoundHandler Controller
}
type muxEntry struct {
explicit bool
h Controller
pattern string
}
type DefaultNotFoundHandler struct {
}
func (h *DefaultNotFoundHandler) Config() *ControllerConfig {
return nil
}
func (h *DefaultNotFoundHandler) HandleRequest(txn *Txn) {
response := NewResponse(txn)
response.SetStatusCode(404)
response.SetStatusMessage("Not Found")
txn.Write(response)
}
// NewServeMux allocates and returns a new CheshireMux.
func NewDefaultRouter() *Router {
router := &Router{
gets: make(map[string]muxEntry),
posts: make(map[string]muxEntry),
deletes: make(map[string]muxEntry),
puts: make(map[string]muxEntry),
}
router.NotFoundHandler = new(DefaultNotFoundHandler)
return router
}
// Does path match pattern?
func pathMatch(pattern, path string) bool {
if len(pattern) == 0 {
// should not happen
return false
}
n := len(pattern)
if pattern[n-1] != '/' {
return pattern == path
}
return len(path) >= n && path[0:n] == pattern
}
// Return the canonical path for p, eliminating . and .. elements.
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
np += "/"
}
return np
}
// Find a handler on a handler map given a path string
// Most-specific (longest) pattern wins
func (this *Router) match(method string, path string) Controller {
var h Controller
var n = 0
m, ok := this.getMethodMap(method)
if !ok {
return nil
}
for k, v := range m {
if !pathMatch(k, path) {
continue
}
if h == nil || len(k) > n {
n = len(k)
h = v.h
}
}
return h
}
// Match returns the registered Controller that matches the
// request or, if no match the registered not found handler is returned
func (mux *Router) Match(method string, path string) (h Controller) {
mux.mu.RLock()
defer mux.mu.RUnlock()
h = mux.match(method, path)
if h == nil {
log.Print("Not Found. TODO: do something!")
h = mux.NotFoundHandler
}
return
}
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (this *Router) Register(methods []string, handler Controller) {
this.mu.Lock()
defer this.mu.Unlock()
for _, m := range methods {
this.reg(m, handler)
}
}
func (this *Router) getMethodMap(method string) (map[string]muxEntry, bool) {
m := strings.ToUpper(method)
switch m {
case "GET":
return this.gets, true
case "POST":
return this.posts, true
case "PUT":
return this.puts, true
case "DELETE":
return this.deletes, true
}
return nil, false
}
func (this *Router) reg(method string, handler Controller) {
var pattern = handler.Config().Route
m, ok := this.getMethodMap(method)
if !ok {
panic("cheshire: " + method + " is not a valid method!")
}
if pattern == "" {
panic("cheshire: invalid pattern " + pattern)
}
if handler == nil {
panic("cheshire: nil handler")
}
if m[pattern].explicit {
panic("cheshire: multiple registrations for " + pattern)
}
m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern}
}