// mux stores a pathHandler and is used to handle the actual serving. // Turns out, we want to accept trailing slashes, BUT we don't care about handling // everything under them. This does exactly matches only unless its explicitly requested to // do something different mux atomic.Value
// exposedPaths is the list of paths that should be shown at / exposedPaths []string
// pathStacks holds the stacks of all registered paths. This allows us to show a more helpful message // before the "http: multiple registrations for %s" panic. pathStacks map[string]string }
// pathHandler is an http.Handler that will satisfy requests first by exact match, then by prefix, // then by notFoundHandler type pathHandler struct { // muxName is used for logging so you can trace requests through muxName string
// pathToHandler is a map of exactly matching request to its handler pathToHandler map[string]http.Handler
// this has to be sorted by most slashes then by length prefixHandlers []prefixHandler
// notFoundHandler is the handler to use for satisfying requests with no other match notFoundHandler http.Handler }
// prefixHandler holds the prefix it should match and the handler to use type prefixHandler struct { // prefix is the prefix to test for a request match prefix string // handler is used to satisfy matching requests handler http.Handler }
// ServeHTTP makes it an http.Handler func(m *PathRecorderMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { m.mux.Load().(*pathHandler).ServeHTTP(w, r) }
// ServeHTTP makes it an http.Handler func(h *pathHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if exactHandler, ok := h.pathToHandler[r.URL.Path]; ok { klog.V(5).Infof("%v: %q satisfied by exact match", h.muxName, r.URL.Path) exactHandler.ServeHTTP(w, r) return }
for _, prefixHandler := range h.prefixHandlers { if strings.HasPrefix(r.URL.Path, prefixHandler.prefix) { klog.V(5).Infof("%v: %q satisfied by prefix %v", h.muxName, r.URL.Path, prefixHandler.prefix) prefixHandler.handler.ServeHTTP(w, r) return } }
klog.V(5).Infof("%v: %q satisfied by NotFoundHandler", h.muxName, r.URL.Path) h.notFoundHandler.ServeHTTP(w, r) }