github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/migration.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package safehttp
    16  
    17  import (
    18  	"net/http"
    19  )
    20  
    21  // RegisteredHandler returns the combined (all request methods) handler
    22  // registered for a given pattern. Returns nil if the exact pattern wasn't used
    23  // to register any handlers.
    24  //
    25  // This method is helpful for migrating services incrementally, endpoint by
    26  // endpoint. The handler runs all the installed interceptors and the dispatcher.
    27  //
    28  // # Important
    29  //
    30  // This function does not attempt to do any kind of path matching. If the
    31  // handler was registered using the ServeMuxConfig for a pattern "/foo/", this
    32  // method will return the handler only when given "/foo/" as an argument, not
    33  // "/foo" nor "/foo/x".
    34  func RegisteredHandler(mux *ServeMux, pattern string) http.Handler {
    35  	if h, ok := mux.handlers[pattern]; ok {
    36  		return h
    37  	}
    38  	// Keep this. Otherwise mux.handlerMap[pattern] returns a
    39  	// (*registeredHandler)(nil), which is not equal to an untyped nil.
    40  	return nil
    41  }