| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | // Copyright 2014 The Macaron Authors
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License"): you may
 | 
					
						
							|  |  |  | // not use this file except in compliance with the License. You may obtain
 | 
					
						
							|  |  |  | // a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 | 
					
						
							|  |  |  | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 | 
					
						
							|  |  |  | // License for the specific language governing permissions and limitations
 | 
					
						
							|  |  |  | // under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 22:56:14 +08:00
										 |  |  | package web | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	// Known HTTP methods.
 | 
					
						
							|  |  |  | 	_HTTP_METHODS = map[string]bool{ | 
					
						
							|  |  |  | 		"GET":     true, | 
					
						
							|  |  |  | 		"POST":    true, | 
					
						
							|  |  |  | 		"PUT":     true, | 
					
						
							|  |  |  | 		"DELETE":  true, | 
					
						
							|  |  |  | 		"PATCH":   true, | 
					
						
							|  |  |  | 		"OPTIONS": true, | 
					
						
							|  |  |  | 		"HEAD":    true, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // routeMap represents a thread-safe map for route tree.
 | 
					
						
							|  |  |  | type routeMap struct { | 
					
						
							|  |  |  | 	lock   sync.RWMutex | 
					
						
							|  |  |  | 	routes map[string]map[string]*Leaf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewRouteMap initializes and returns a new routeMap.
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func newRouteMap() *routeMap { | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 	rm := &routeMap{ | 
					
						
							|  |  |  | 		routes: make(map[string]map[string]*Leaf), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for m := range _HTTP_METHODS { | 
					
						
							|  |  |  | 		rm.routes[m] = make(map[string]*Leaf) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return rm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // getLeaf returns Leaf object if a route has been registered.
 | 
					
						
							|  |  |  | func (rm *routeMap) getLeaf(method, pattern string) *Leaf { | 
					
						
							|  |  |  | 	rm.lock.RLock() | 
					
						
							|  |  |  | 	defer rm.lock.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return rm.routes[method][pattern] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // add adds new route to route tree map.
 | 
					
						
							|  |  |  | func (rm *routeMap) add(method, pattern string, leaf *Leaf) { | 
					
						
							|  |  |  | 	rm.lock.Lock() | 
					
						
							|  |  |  | 	defer rm.lock.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rm.routes[method][pattern] = leaf | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type group struct { | 
					
						
							|  |  |  | 	pattern  string | 
					
						
							|  |  |  | 	handlers []Handler | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Router represents a Macaron router layer.
 | 
					
						
							|  |  |  | type Router struct { | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | 	m       *Macaron | 
					
						
							|  |  |  | 	routers map[string]*Tree | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 	*routeMap | 
					
						
							|  |  |  | 	namedRoutes map[string]*Leaf | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | 	groups   []group | 
					
						
							|  |  |  | 	notFound http.HandlerFunc | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewRouter() *Router { | 
					
						
							|  |  |  | 	return &Router{ | 
					
						
							|  |  |  | 		routers:     make(map[string]*Tree), | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | 		routeMap:    newRouteMap(), | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 		namedRoutes: make(map[string]*Leaf), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Handle is a function that can be registered to a route to handle HTTP requests.
 | 
					
						
							|  |  |  | // Like http.HandlerFunc, but has a third parameter for the values of wildcards (variables).
 | 
					
						
							| 
									
										
										
										
											2021-09-15 00:34:56 +08:00
										 |  |  | type Handle func(http.ResponseWriter, *http.Request, map[string]string) | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // handle adds new route to the router tree.
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) handle(method, pattern string, handle Handle) { | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 	method = strings.ToUpper(method) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var leaf *Leaf | 
					
						
							|  |  |  | 	// Prevent duplicate routes.
 | 
					
						
							|  |  |  | 	if leaf = r.getLeaf(method, pattern); leaf != nil { | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate HTTP methods.
 | 
					
						
							|  |  |  | 	if !_HTTP_METHODS[method] && method != "*" { | 
					
						
							|  |  |  | 		panic("unknown HTTP method: " + method) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Generate methods need register.
 | 
					
						
							|  |  |  | 	methods := make(map[string]bool) | 
					
						
							|  |  |  | 	if method == "*" { | 
					
						
							|  |  |  | 		for m := range _HTTP_METHODS { | 
					
						
							|  |  |  | 			methods[m] = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		methods[method] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Add to router tree.
 | 
					
						
							|  |  |  | 	for m := range methods { | 
					
						
							|  |  |  | 		if t, ok := r.routers[m]; ok { | 
					
						
							|  |  |  | 			leaf = t.Add(pattern, handle) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			t := NewTree() | 
					
						
							|  |  |  | 			leaf = t.Add(pattern, handle) | 
					
						
							|  |  |  | 			r.routers[m] = t | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		r.add(m, pattern, leaf) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Handle registers a new request handle with the given pattern, method and handlers.
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Handle(method string, pattern string, handlers []Handler) { | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 	if len(r.groups) > 0 { | 
					
						
							|  |  |  | 		groupPattern := "" | 
					
						
							|  |  |  | 		h := make([]Handler, 0) | 
					
						
							|  |  |  | 		for _, g := range r.groups { | 
					
						
							|  |  |  | 			groupPattern += g.pattern | 
					
						
							|  |  |  | 			h = append(h, g.handlers...) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pattern = groupPattern + pattern | 
					
						
							|  |  |  | 		h = append(h, handlers...) | 
					
						
							|  |  |  | 		handlers = h | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 00:34:56 +08:00
										 |  |  | 	r.handle(method, pattern, func(resp http.ResponseWriter, req *http.Request, params map[string]string) { | 
					
						
							|  |  |  | 		c := r.m.createContext(resp, SetURLParams(req, params)) | 
					
						
							| 
									
										
										
										
											2022-08-09 20:58:50 +08:00
										 |  |  | 		for _, h := range handlers { | 
					
						
							|  |  |  | 			c.mws = append(c.mws, mwFromHandler(h)) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 		c.run() | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r *Router) Group(pattern string, fn func(), h ...Handler) { | 
					
						
							|  |  |  | 	r.groups = append(r.groups, group{pattern, h}) | 
					
						
							|  |  |  | 	fn() | 
					
						
							|  |  |  | 	r.groups = r.groups[:len(r.groups)-1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get is a shortcut for r.Handle("GET", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Get(pattern string, h ...Handler) { | 
					
						
							|  |  |  | 	r.Handle("GET", pattern, h) | 
					
						
							|  |  |  | 	r.Head(pattern, h...) | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Patch is a shortcut for r.Handle("PATCH", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Patch(pattern string, h ...Handler) { r.Handle("PATCH", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Post is a shortcut for r.Handle("POST", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Post(pattern string, h ...Handler) { r.Handle("POST", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Put is a shortcut for r.Handle("PUT", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Put(pattern string, h ...Handler) { r.Handle("PUT", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Delete is a shortcut for r.Handle("DELETE", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Delete(pattern string, h ...Handler) { r.Handle("DELETE", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Options is a shortcut for r.Handle("OPTIONS", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Options(pattern string, h ...Handler) { r.Handle("OPTIONS", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Head is a shortcut for r.Handle("HEAD", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Head(pattern string, h ...Handler) { r.Handle("HEAD", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Any is a shortcut for r.Handle("*", pattern, handlers)
 | 
					
						
							| 
									
										
										
										
											2021-08-30 17:48:34 +08:00
										 |  |  | func (r *Router) Any(pattern string, h ...Handler) { r.Handle("*", pattern, h) } | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // NotFound configurates http.HandlerFunc which is called when no matching route is
 | 
					
						
							|  |  |  | // found. If it is not set, http.NotFound is used.
 | 
					
						
							|  |  |  | // Be sure to set 404 response code in your handler.
 | 
					
						
							|  |  |  | func (r *Router) NotFound(handlers ...Handler) { | 
					
						
							|  |  |  | 	r.notFound = func(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 		c := r.m.createContext(rw, req) | 
					
						
							| 
									
										
										
										
											2022-08-09 20:58:50 +08:00
										 |  |  | 		for _, h := range handlers { | 
					
						
							|  |  |  | 			c.mws = append(c.mws, mwFromHandler(h)) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 		c.run() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 	if t, ok := r.routers[req.Method]; ok { | 
					
						
							|  |  |  | 		// Fast match for static routes
 | 
					
						
							| 
									
										
										
										
											2021-10-06 01:38:09 +08:00
										 |  |  | 		if !strings.ContainsAny(req.URL.Path, ":*") { | 
					
						
							|  |  |  | 			leaf := r.getLeaf(req.Method, req.URL.Path) | 
					
						
							|  |  |  | 			if leaf != nil { | 
					
						
							|  |  |  | 				leaf.handle(rw, req, nil) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-07-08 20:19:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		h, p, ok := t.Match(req.URL.EscapedPath()) | 
					
						
							|  |  |  | 		if ok { | 
					
						
							|  |  |  | 			if splat, ok := p["*0"]; ok { | 
					
						
							|  |  |  | 				p["*"] = splat // Easy name.
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			h(rw, req, p) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r.notFound(rw, req) | 
					
						
							|  |  |  | } |