github.com/searKing/golang/go@v1.2.117/net/http/server.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"net/http"
     9  	"net/url"
    10  )
    11  
    12  // StripPrefix returns a handler that serves HTTP requests
    13  // by removing the given prefix from the request URL's Path
    14  // and invoking the handler h. StripPrefix handles a
    15  // request for a path that doesn't begin with prefix by
    16  // replying with an HTTP 404 not found error.
    17  func ReplacePath(path string, h http.Handler) http.Handler {
    18  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		r2 := new(http.Request)
    20  		*r2 = *r
    21  		r2.URL = new(url.URL)
    22  		*r2.URL = *r.URL
    23  		r2.URL.Path = path
    24  		h.ServeHTTP(w, r2)
    25  	})
    26  }