]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/net/http/server.go
net/http: add GODEBUG setting for old ServeMux behavior
[gostls13.git] / src / net / http / server.go
index ee02d776ac5fc5e5e011bfcd16cf46e052ee5c9d..f456e43cce5b61e9c7d4ddcc88eab38a391439a4 100644 (file)
@@ -33,6 +33,8 @@ import (
        "golang.org/x/net/http/httpguts"
 )
 
+// TODO(jba): test
+
 // Errors used by the HTTP server.
 var (
        // ErrBodyNotAllowed is returned by ResponseWriter.Write calls
@@ -2348,7 +2350,8 @@ type ServeMux struct {
        mu       sync.RWMutex
        tree     routingNode
        index    routingIndex
-       patterns []*pattern // TODO(jba): remove if possible
+       patterns []*pattern  // TODO(jba): remove if possible
+       mux121   serveMux121 // used only when GODEBUG=httpmuxgo121=1
 }
 
 // NewServeMux allocates and returns a new ServeMux.
@@ -2412,6 +2415,9 @@ func stripHostPort(h string) string {
 // If there is no registered handler that applies to the request,
 // Handler returns a “page not found” handler and an empty pattern.
 func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
+       if use121 {
+               return mux.mux121.findHandler(r)
+       }
        h, p, _, _ := mux.findHandler(r)
        return h, p
 }
@@ -2585,9 +2591,12 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
                w.WriteHeader(StatusBadRequest)
                return
        }
-       h, _, pat, matches := mux.findHandler(r)
-       r.pat = pat
-       r.matches = matches
+       var h Handler
+       if use121 {
+               h, _ = mux.mux121.findHandler(r)
+       } else {
+               h, _, r.pat, r.matches = mux.findHandler(r)
+       }
        h.ServeHTTP(w, r)
 }
 
@@ -2597,23 +2606,35 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
 // Handle registers the handler for the given pattern.
 // If a handler already exists for pattern, Handle panics.
 func (mux *ServeMux) Handle(pattern string, handler Handler) {
+       if use121 {
+               mux.mux121.handle(pattern, handler)
+       }
        mux.register(pattern, handler)
 }
 
 // HandleFunc registers the handler function for the given pattern.
 func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
+       if use121 {
+               mux.mux121.handleFunc(pattern, handler)
+       }
        mux.register(pattern, HandlerFunc(handler))
 }
 
 // Handle registers the handler for the given pattern in [DefaultServeMux].
 // The documentation for [ServeMux] explains how patterns are matched.
 func Handle(pattern string, handler Handler) {
+       if use121 {
+               DefaultServeMux.mux121.handle(pattern, handler)
+       }
        DefaultServeMux.register(pattern, handler)
 }
 
 // HandleFunc registers the handler function for the given pattern in [DefaultServeMux].
 // The documentation for [ServeMux] explains how patterns are matched.
 func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
+       if use121 {
+               DefaultServeMux.mux121.handleFunc(pattern, handler)
+       }
        DefaultServeMux.register(pattern, HandlerFunc(handler))
 }