~samwhited/mux

ref: 6201e8e0dc2c08e45929475b425ab75c989fdce9 mux/pathmux.go -rw-r--r-- 11.5 KiB
6201e8e0Sam Whited all: prepare to publish 1 year, 8 months ago
                                                                                
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Package mux is a fast and safe HTTP multiplexer.
//
// URL Parameters
//
// Routes registered on the multiplexer may contain variable path parameters
// that comprise an optional name, followed by a type.
//
// Valid types include:
//
//     int    eg. -1, 1 (int64 in Go)
//     uint   eg. 0, 1 (uint64 in Go)
//     float  eg. 1, 1.123, -1.123 (float64 in Go)
//     string eg. anything ({string} is the same as {})
//     path   eg. files/123.png (must be the last path component)
//
// All numeric types are 64 bits wide.
// A non-path typed variable parameter may appear anywhere in the path and match
// a single path component:
//
//     /user/{id int}/edit
//
// Parameters of type "path" match the remainder of the input path and therefore
// may only appear as the final component of a route:
//
//     /file/{p path}
//
// Two paths with different typed variable parameters (including static routes)
// in the same position are not allowed.
// Attempting to register any two of the following routes will panic:
//
//     /user/{a int}/new
//     /user/{b int}/edit
//     /user/{b string}/edit
//     /user/me
package mux // import "code.soquee.net/mux"

import (
	"context"
	"fmt"
	"net"
	"net/http"
	"path"
	"strconv"
	"strings"
)

const (
	emptyPanic  = "invalid empty pattern"
	panicNoRoot = "all handlers must start with /"
	typStatic   = "static"
	typWild     = "path"
	typString   = "string"
	typUint     = "uint"
	typInt      = "int"
	typFloat    = "float"
)

// ServeMux is an HTTP request multiplexer.
// It matches the URL of each incoming request against a list of registered
// patterns and calls the handler for the pattern that most closely matches the
// URL.
type ServeMux struct {
	node
	notFound http.Handler
}

// ServeHTTP dispatches the request to the handler whose pattern most closely
// matches the request URL.
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	h, newReq := mux.handler(r)
	h.ServeHTTP(w, newReq)
}

// stripHostPort returns h without any trailing ":<port>".
func stripHostPort(h string) string {
	// If no port on host, return unchanged
	if strings.IndexByte(h, ':') == -1 {
		return h
	}
	host, _, err := net.SplitHostPort(h)
	if err != nil {
		return h // on error, return unchanged
	}
	return host
}

// 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
}

// Handler returns the handler to use for the given request, consulting
// r.URL.Path.
// It always returns a non-nil handler.
//
// The path and host are used unchanged for CONNECT requests.
//
// If there is no registered handler that applies to the request, Handler
// returns a ``page not found'' handler and an empty pattern.
// The new request uses a context that contains any route parameters that were
// matched against the request path.
func (mux *ServeMux) Handler(r *http.Request) (http.Handler, *http.Request) {
	return mux.handler(r)
}

// handler returns the handler to use for the given request and a new request
// with parameters set on the context.
func (mux *ServeMux) handler(r *http.Request) (http.Handler, *http.Request) {
	// TODO: Add /tree to /tree/ redirect option and apply here.
	// TODO: use host
	host := r.Host
	_ = host
	path := r.URL.Path

	// CONNECT requests are not canonicalized
	if r.Method != "CONNECT" {
		// All other requests have any port stripped and path cleaned
		// before passing to mux.handler.
		host = stripHostPort(r.Host)
		path = cleanPath(r.URL.Path)
		if path != r.URL.Path {
			url := *r.URL
			url.Path = path
			return http.RedirectHandler(url.String(), http.StatusPermanentRedirect), r
		}
	}

	// TODO: add host based matching and check it here.
	node := &mux.node
	path = strings.TrimPrefix(path, "/")
	if path == "" {
		if node.handler == nil {
			return mux.notFound, r
		}
		return node.handler, r
	}

nodeloop:
	for node != nil {
		// If this is a variable route,
		if len(node.child) == 1 && node.child[0].typ != typStatic {
			var part, remain string
			part, remain, r = node.child[0].match(path, r)

			// If the type doesn't match, we're done.
			if part == "" {
				return mux.notFound, r
			}

			// The variable route matched and it's the last thing in the path, so we
			// have our route:
			if remain == "" {
				if node.child[0].handler == nil {
					return mux.notFound, r
				}
				return node.child[0].handler, r
			}
			node = &node.child[0]
			path = remain
			continue
		}

		// If this is a static route
		for _, child := range node.child {
			var part, remain string
			part, remain, r = child.match(path, r)
			// The child did not match, so check the next.
			if part == "" {
				path = remain
				continue
			}

			// The child matched and was the last thing in the path, so we have our
			// route:
			if remain == "" {
				if child.handler == nil {
					return mux.notFound, r
				}
				return child.handler, r
			}

			// The child matched but was not the last one, move on to the next match.
			node = &child
			path = remain
			continue nodeloop
		}

		// No child matched.
		return mux.notFound, r
	}

	return mux.notFound, r
}

type node struct {
	name    string
	typ     string
	handler http.Handler

	child []node
}

// ctxParam is a type used for context keys that contain route parameters.
type ctxParam string

func addValue(r *http.Request, name string, val interface{}) *http.Request {
	if name != "" {
		return r.WithContext(context.WithValue(r.Context(), ctxParam(name), val))
	}
	return r
}

// Param returns the named route parameter from the requests context.
func Param(r *http.Request, name string) interface{} {
	return r.Context().Value(ctxParam(name))
}

// TODO: take a context and put parameters into it.
func (n *node) match(path string, r *http.Request) (part string, remain string, req *http.Request) {
	// Nil nodes never match.
	if n == nil {
		return "", "", r
	}

	// wildcards are a special case that always match the entire remainder of the
	// path.
	if n.typ == typWild {
		r = addValue(r, n.name, path)
		return path, "", r
	}

	part, remain = nextPart(path)
	switch n.typ {
	case typStatic:
		if n.name == part {
			return part, remain, r
		}
		return "", path, r
	case typString:
		r = addValue(r, n.name, part)
		return part, remain, r
	case typUint:
		v, err := strconv.ParseUint(part, 10, 64)
		if err != nil {
			return "", path, r
		}
		r = addValue(r, n.name, v)
		return part, remain, r
	case typInt:
		v, err := strconv.ParseInt(part, 10, 64)
		if err != nil {
			return "", path, r
		}
		r = addValue(r, n.name, v)
		return part, remain, r
	case typFloat:
		v, err := strconv.ParseFloat(part, 64)
		if err != nil {
			return "", path, r
		}
		r = addValue(r, n.name, v)
		return part, remain, r
	}
	panic("unknown type")
}

// New allocates and returns a new ServeMux.
func New(opts ...Option) *ServeMux {
	mux := &ServeMux{
		node: node{
			name:    "/",
			typ:     typStatic,
			handler: nil,
		},
		notFound: http.HandlerFunc(http.NotFound),
	}
	for _, o := range opts {
		o(mux)
	}
	return mux
}

// Option is used to configure a ServeMux.
type Option func(*ServeMux)

// NotFound sets the handler to use when a request does not have a registered
// route.
//
// The handler should set the status code to 404 (Page Not Found).
func NotFound(h http.Handler) Option {
	return func(mux *ServeMux) {
		mux.notFound = h
	}
}

// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func HandleFunc(r string, h http.HandlerFunc) Option {
	return Handle(r, h)
}

// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func Handle(r string, h http.Handler) Option {
	if !strings.HasPrefix(r, "/") {
		panic(panicNoRoot)
	}
	r = r[1:]

	const (
		alreadyRegistered = "route already registered for /%s"
	)

	return func(mux *ServeMux) {
		pointer := &mux.node

		// If we're registering a root handler
		if r == "" {
			// If it exists already
			if pointer.handler != nil {
				panic(fmt.Sprintf(alreadyRegistered, r))
			}
			pointer.handler = h
			return
		}

	pathloop:
		for part, remain := nextPart(r); remain != "" || part != ""; part, remain = nextPart(remain) {
			name, typ := parseParam(part)

			if typ == typWild && remain != "" {
				panic(fmt.Sprintf("wildcards must be the last component in a route: /%s", r))
			}

			// If there are already children, check that this one is compatible with
			// them.
			if len(pointer.child) > 0 {
				child := pointer.child[0]
				switch {
				// All non static routes must have the same type and name.
				case typ != typStatic && child.typ != typ:
					panic(fmt.Sprintf("conflicting type found, {%s %s} in route %q conflicts with existing registration of {%s %s}", name, typ, r, pointer.child[0].name, pointer.child[0].typ))
				case typ != typStatic && child.name != name:
					panic(fmt.Sprintf("conflicting variable name found, {%s %s} in route %q conflicts with existing registration of {%s %s}", name, typ, r, pointer.child[0].name, pointer.child[0].typ))
				// All static routes must have the same type.
				case typ == typStatic && child.typ != typ:
					panic(fmt.Sprintf("conflicting type found, {%s %s} in route %q conflicts with existing registration of {%s %s}", name, typ, r, pointer.child[0].name, pointer.child[0].typ))
				}
			}

			// Check if a node already exists in the tree with this name.
			for i, child := range pointer.child {
				if child.name == name {
					if remain == "" {
						// If this is the path we want to register and no handler has been
						// registered for it, add one:
						if child.handler == nil {
							pointer.child[i].handler = h
							continue pathloop
						} else {
							// If one already exists and this is the path we were trying to
							// register, panic.
							panic(fmt.Sprintf(alreadyRegistered, r))
						}
					}

					pointer = &pointer.child[i]
					continue pathloop
				}
			}

			// Not found at his level. Append new node.
			n := node{
				name: name,
				typ:  typ,
			}
			if remain == "" {
				n.handler = h
			}

			pointer.child = append(pointer.child, n)
			pointer = &pointer.child[len(pointer.child)-1]
		}
	}
}

type route struct{}

// parseParam returns a node with an empty handler from a path component.
func parseParam(pattern string) (name string, typ string) {
	// README:
	// The various checks in this function are a tad brittle and *order matters*
	// in subtle ways.
	// Be careful when refactoring this function.
	// that something is missing re-ordering these checks may result in panics.
	// Eventually we should build a proper tokenizer for this.

	// We should never be passed an empty pattern.
	// If we get one, it's a bug.
	if pattern == "" {
		panic(emptyPanic)
	}

	// Static route components aren't patterns and must match exactly.
	if pattern[0] != '{' || pattern[len(pattern)-1] != '}' {
		return pattern, typStatic
	}

	// {} is an unnamed variable (it matches any single path component)
	if len(pattern) == 2 {
		return "", typString
	}

	// Variable matches ("{name type}" or "{type}")
	idx := strings.IndexByte(pattern, ' ')
	if idx == -1 {
		idx = 0
	}
	typ = pattern[idx+1 : len(pattern)-1]
	if idx == 0 {
		idx = 1
	}

	switch typ {
	case typInt, typUint, typFloat, typString, typWild:
		return pattern[1:idx], typ
	}
	panic(fmt.Sprintf("invalid type: %q", typ))
}
func nextPart(path string) (string, string) {
	idx := strings.IndexByte(path, '/')
	if idx == -1 {
		return path, ""
	}
	return path[:idx], path[idx+1:]
}