trout

Paddy 2016-01-21 Parent:3df515f0cec5

4:07a9b501d6b5 Go to Latest

trout/route.go

Add Router prefix. Make the Router use a prefix that will be stripped before matching URLs against Endpoints, so we can ignore the prefix that http.Handle is matching against, instead of duplicating it, which can lead to confusing 404s.

History
paddy@0 1 package trout
paddy@0 2
paddy@0 3 import (
paddy@0 4 "net/http"
paddy@0 5 "strconv"
paddy@0 6 "strings"
paddy@0 7 "time"
paddy@0 8 )
paddy@0 9
paddy@0 10 var (
paddy@0 11 default404Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paddy@0 12 w.WriteHeader(http.StatusNotFound)
paddy@0 13 w.Write([]byte("404 Not Found"))
paddy@0 14 return
paddy@0 15 }))
paddy@0 16 default405Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paddy@3 17 w.Header().Set("Allow", strings.Join(r.Header[http.CanonicalHeaderKey("Trout-Methods")], ", "))
paddy@0 18 w.WriteHeader(http.StatusMethodNotAllowed)
paddy@0 19 w.Write([]byte("405 Method Not Allowed"))
paddy@0 20 return
paddy@0 21 }))
paddy@0 22 )
paddy@0 23
paddy@0 24 // RequestVars returns easy-to-access mappings of parameters to values for URL templates. Any {parameter} in
paddy@0 25 // your URL template will be available in the returned Header as a slice of strings, one for each instance of
paddy@0 26 // the {parameter}. In the case of a parameter name being used more than once in the same URL template, the
paddy@0 27 // values will be in the slice in the order they appeared in the template.
paddy@0 28 //
paddy@0 29 // Values can easily be accessed by using the .Get() method of the returned Header, though to access multiple
paddy@0 30 // values, they must be accessed through the map. All parameters use http.CanonicalHeaderKey for their formatting.
paddy@0 31 // When using .Get(), the parameter name will be transformed automatically. When utilising the Header as a map,
paddy@0 32 // the parameter name needs to have http.CanonicalHeaderKey applied manually.
paddy@0 33 func RequestVars(r *http.Request) http.Header {
paddy@0 34 res := http.Header{}
paddy@0 35 for h, v := range r.Header {
paddy@0 36 stripped := strings.TrimPrefix(h, http.CanonicalHeaderKey("Trout-Param-"))
paddy@0 37 if stripped != h {
paddy@0 38 res[stripped] = v
paddy@0 39 }
paddy@0 40 }
paddy@0 41 return res
paddy@0 42 }
paddy@0 43
paddy@0 44 // Router defines a set of Endpoints that map requests to the http.Handlers. The http.Handler assigned to
paddy@0 45 // Handle404, if set, will be called when no Endpoint matches the current request. The http.Handler assigned
paddy@0 46 // to Handle405, if set, will be called when an Endpoint matches the current request, but has no http.Handler
paddy@0 47 // set for the HTTP method that the request used. Should either of these properties be unset, a default
paddy@0 48 // http.Handler will be used.
paddy@0 49 //
paddy@0 50 // The Router type is safe for use with empty values, but makes no attempt at concurrency-safety in adding
paddy@0 51 // Endpoints or in setting properties. It should also be noted that the adding Endpoints while simultaneously
paddy@0 52 // routing requests will lead to undefined and (almost certainly) undesirable behaviour. Routers are intended
paddy@0 53 // to be initialised with a set of Endpoints, and then start serving requests. Using them outside of this use
paddy@0 54 // case is unsupported.
paddy@0 55 type Router struct {
paddy@0 56 t *trie
paddy@0 57 Handle404 http.Handler
paddy@0 58 Handle405 http.Handler
paddy@4 59 prefix string
paddy@0 60 }
paddy@0 61
paddy@1 62 func (router *Router) serve404(w http.ResponseWriter, r *http.Request, t time.Time) {
paddy@0 63 h := default404Handler
paddy@0 64 if router.Handle404 != nil {
paddy@0 65 h = router.Handle404
paddy@0 66 }
paddy@0 67 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(t).Nanoseconds(), 10))
paddy@0 68 h.ServeHTTP(w, r)
paddy@0 69 }
paddy@0 70
paddy@1 71 func (router *Router) serve405(w http.ResponseWriter, r *http.Request, t time.Time) {
paddy@0 72 h := default405Handler
paddy@0 73 if router.Handle405 != nil {
paddy@0 74 h = router.Handle405
paddy@0 75 }
paddy@0 76 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(t).Nanoseconds(), 10))
paddy@0 77 h.ServeHTTP(w, r)
paddy@0 78 }
paddy@0 79
paddy@0 80 func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
paddy@0 81 start := time.Now()
paddy@0 82 if router.t == nil {
paddy@0 83 router.serve404(w, r, start)
paddy@1 84 return
paddy@0 85 }
paddy@4 86 u := strings.TrimPrefix(r.URL.Path, router.prefix)
paddy@4 87 pieces := strings.Split(strings.ToLower(strings.Trim(u, "/")), "/")
paddy@0 88 router.t.RLock()
paddy@0 89 defer router.t.RUnlock()
paddy@0 90 branches := make([]*branch, len(pieces))
paddy@0 91 path, ok := router.t.match(pieces)
paddy@0 92 if !ok {
paddy@0 93 router.serve404(w, r, start)
paddy@1 94 return
paddy@0 95 }
paddy@0 96 b := router.t.branch
paddy@0 97 for i, pos := range path {
paddy@0 98 b = b.children[pos]
paddy@0 99 branches[i] = b
paddy@0 100 }
paddy@0 101 v := vars(branches, pieces)
paddy@0 102 for key, vals := range v {
paddy@0 103 r.Header[http.CanonicalHeaderKey("Trout-Param-"+key)] = vals
paddy@0 104 }
paddy@0 105 ms := make([]string, len(b.methods))
paddy@0 106 i := 0
paddy@0 107 for m := range b.methods {
paddy@0 108 ms[i] = m
paddy@0 109 i = i + 1
paddy@0 110 }
paddy@0 111 r.Header[http.CanonicalHeaderKey("Trout-Methods")] = ms
paddy@0 112 h := b.methods[r.Method]
paddy@0 113 if h == nil {
paddy@0 114 router.serve405(w, r, start)
paddy@1 115 return
paddy@0 116 }
paddy@0 117 r.Header.Set("Trout-Timer", strconv.FormatInt(time.Now().Sub(start).Nanoseconds(), 10))
paddy@0 118 h.ServeHTTP(w, r)
paddy@0 119 }
paddy@0 120
paddy@4 121 // SetPrefix sets a string prefix for the Router that won't be taken into account when matching Endpoints.
paddy@4 122 // This is usually set to whatever path is associated with the http.Handler serving the Router.
paddy@4 123 func (router *Router) SetPrefix(prefix string) {
paddy@4 124 router.prefix = prefix
paddy@4 125 }
paddy@4 126
paddy@0 127 // Endpoint defines a new Endpoint on the Router. The Endpoint should be a URL template, using curly braces
paddy@0 128 // to denote parameters that should be filled at runtime. For example, `{id}` denotes a parameter named `id`
paddy@0 129 // that should be filled with whatever the request has in that space.
paddy@0 130 //
paddy@0 131 // Parameters are always `/`-separated strings. There is no support for regular expressions or other limitations
paddy@0 132 // on what may be in those strings. A parameter is simply defined as "whatever is between these two / characters".
paddy@1 133 func (router *Router) Endpoint(e string) *Endpoint {
paddy@0 134 e = strings.Trim(e, "/")
paddy@0 135 e = strings.ToLower(e)
paddy@0 136 pieces := strings.Split(e, "/")
paddy@1 137 if router.t == nil {
paddy@1 138 router.t = &trie{}
paddy@1 139 }
paddy@0 140 router.t.Lock()
paddy@0 141 defer router.t.Unlock()
paddy@0 142 if router.t.branch == nil {
paddy@0 143 router.t.branch = &branch{
paddy@0 144 parent: nil,
paddy@0 145 children: []*branch{},
paddy@0 146 key: "",
paddy@0 147 isParam: false,
paddy@0 148 methods: map[string]http.Handler{},
paddy@0 149 }
paddy@0 150 }
paddy@0 151 closest := findClosestLeaf(pieces, router.t.branch)
paddy@0 152 b := router.t.branch
paddy@0 153 for _, pos := range closest {
paddy@0 154 b = b.children[pos]
paddy@0 155 }
paddy@0 156 if len(closest) == len(pieces) {
paddy@0 157 return (*Endpoint)(b)
paddy@0 158 }
paddy@0 159 offset := len(closest)
paddy@0 160 for i := offset; i < len(pieces); i++ {
paddy@0 161 piece := pieces[i]
paddy@0 162 var isParam bool
paddy@1 163 if len(piece) > 0 && piece[0:1] == "{" && piece[len(piece)-1:] == "}" {
paddy@0 164 isParam = true
paddy@0 165 piece = piece[1 : len(piece)-1]
paddy@0 166 }
paddy@0 167 b = b.addChild(piece, isParam)
paddy@0 168 }
paddy@0 169 return (*Endpoint)(b)
paddy@0 170 }
paddy@0 171
paddy@0 172 func vars(path []*branch, pieces []string) map[string][]string {
paddy@0 173 v := map[string][]string{}
paddy@0 174 for pos, p := range path {
paddy@0 175 if !p.isParam {
paddy@0 176 continue
paddy@0 177 }
paddy@0 178 _, ok := v[p.key]
paddy@0 179 if !ok {
paddy@0 180 v[p.key] = []string{pieces[pos]}
paddy@0 181 continue
paddy@0 182 }
paddy@0 183 v[p.key] = append(v[p.key], pieces[pos])
paddy@0 184 }
paddy@0 185 return v
paddy@0 186 }
paddy@0 187
paddy@0 188 func findClosestLeaf(pieces []string, b *branch) []int {
paddy@0 189 offset := 0
paddy@0 190 path := []int{}
paddy@0 191 longest := []int{}
paddy@0 192 num := len(pieces)
paddy@0 193 for i := 0; i < num; i++ {
paddy@0 194 piece := pieces[i]
paddy@0 195 var isParam bool
paddy@1 196 if len(piece) > 0 && piece[0:1] == "{" && piece[len(piece)-1:] == "}" {
paddy@0 197 isParam = true
paddy@0 198 piece = piece[1 : len(piece)-1]
paddy@0 199 }
paddy@0 200 offset = pickNextRoute(b, offset, piece, isParam)
paddy@0 201 if offset == -1 {
paddy@0 202 if len(path) == 0 {
paddy@0 203 // exhausted our options, bail
paddy@0 204 break
paddy@0 205 }
paddy@0 206 // no match, maybe save this and backup
paddy@0 207 if len(path) > len(longest) {
paddy@0 208 longest = append([]int{}, path...) // copy them over so they don't get modified
paddy@0 209 }
paddy@0 210 path, offset = backup(path)
paddy@0 211 offset = offset + 1
paddy@0 212 b = b.parent
paddy@0 213 i = i - 2
paddy@0 214 } else {
paddy@0 215 path = append(path, offset)
paddy@0 216 b = b.children[offset]
paddy@0 217 offset = 0
paddy@0 218 }
paddy@0 219 }
paddy@0 220 if len(longest) < len(path) {
paddy@0 221 longest = append([]int{}, path...)
paddy@0 222 }
paddy@0 223 return longest
paddy@0 224 }
paddy@0 225
paddy@0 226 func pickNextRoute(b *branch, offset int, input string, variable bool) int {
paddy@0 227 count := len(b.children)
paddy@0 228 for i := offset; i < count; i++ {
paddy@3 229 if b.children[i].key == input && b.children[i].isParam == variable {
paddy@0 230 return i
paddy@0 231 }
paddy@0 232 }
paddy@0 233 return -1
paddy@0 234 }
paddy@0 235
paddy@0 236 // Endpoint defines a single URL template that requests can be matched against. It uses
paddy@0 237 // URL parameters to accept variables in the URL structure and make them available to
paddy@0 238 // the Handlers associated with the Endpoint.
paddy@0 239 type Endpoint branch
paddy@0 240
paddy@0 241 // Handler associates the passed http.Handler with the Endpoint. This http.Handler will be
paddy@0 242 // used for all requests, regardless of the HTTP method they are using, unless overridden by
paddy@0 243 // the Methods method. Endpoints without a http.Handler associated with them will not be
paddy@0 244 // considered matches for requests, unless the request was made using an HTTP method that the
paddy@0 245 // Endpoint has an http.Handler mapped to.
paddy@0 246 func (e *Endpoint) Handler(h http.Handler) {
paddy@0 247 (*branch)(e).setHandler("", h)
paddy@0 248 }
paddy@0 249
paddy@0 250 // Methods simple returns a Methods object that will enable the mapping of the passed HTTP
paddy@0 251 // request methods to a Methods object. On its own, this function does not modify anything. It
paddy@0 252 // should, instead, be used as a friendly shorthand to get to the Methods.Handler method.
paddy@0 253 func (e *Endpoint) Methods(m ...string) Methods {
paddy@0 254 return Methods{
paddy@0 255 e: e,
paddy@0 256 m: m,
paddy@0 257 }
paddy@0 258 }
paddy@0 259
paddy@0 260 // Methods defines a pairing of an Endpoint to the HTTP request methods that should be mapped to
paddy@0 261 // specific http.Handlers. Its sole purpose is to enable the Methods.Handler method.
paddy@0 262 type Methods struct {
paddy@0 263 e *Endpoint
paddy@0 264 m []string
paddy@0 265 }
paddy@0 266
paddy@0 267 // Handler maps a Methods object to a specific http.Handler. This overrides the http.Handler
paddy@0 268 // associated with the Endpoint to only handle specific HTTP method(s).
paddy@0 269 func (m Methods) Handler(h http.Handler) {
paddy@0 270 b := (*branch)(m.e)
paddy@0 271 for _, method := range m.m {
paddy@0 272 b.setHandler(method, h)
paddy@0 273 }
paddy@0 274 }