mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #7404 from nikhiljindal/handleIndex
Updating handleIndex to return 404 for invalid server URL
This commit is contained in:
commit
45f85dbf06
@ -27,6 +27,12 @@ import (
|
|||||||
|
|
||||||
func IndexHandler(container *restful.Container, muxHelper *MuxHelper) func(http.ResponseWriter, *http.Request) {
|
func IndexHandler(container *restful.Container, muxHelper *MuxHelper) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
status := http.StatusOK
|
||||||
|
if r.URL.Path != "/" && r.URL.Path != "/index.html" {
|
||||||
|
// Since "/" matches all paths, handleIndex is called for all paths for which there is no handler registered.
|
||||||
|
// We want to to return a 404 status with a list of all valid paths, incase of an invalid URL request.
|
||||||
|
status = http.StatusNotFound
|
||||||
|
}
|
||||||
var handledPaths []string
|
var handledPaths []string
|
||||||
// Extract the paths handled using restful.WebService
|
// Extract the paths handled using restful.WebService
|
||||||
for _, ws := range container.RegisteredWebServices() {
|
for _, ws := range container.RegisteredWebServices() {
|
||||||
@ -35,6 +41,6 @@ func IndexHandler(container *restful.Container, muxHelper *MuxHelper) func(http.
|
|||||||
// Extract the paths handled using mux handler.
|
// Extract the paths handled using mux handler.
|
||||||
handledPaths = append(handledPaths, muxHelper.RegisteredPaths...)
|
handledPaths = append(handledPaths, muxHelper.RegisteredPaths...)
|
||||||
sort.Strings(handledPaths)
|
sort.Strings(handledPaths)
|
||||||
writeRawJSON(http.StatusOK, api.RootPaths{Paths: handledPaths}, w)
|
writeRawJSON(status, api.RootPaths{Paths: handledPaths}, w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -363,6 +363,7 @@ func getTestRequests() []struct {
|
|||||||
{"GET", "/api", "", code200},
|
{"GET", "/api", "", code200},
|
||||||
{"GET", "/healthz", "", code200},
|
{"GET", "/healthz", "", code200},
|
||||||
{"GET", "/version", "", code200},
|
{"GET", "/version", "", code200},
|
||||||
|
{"GET", "/invalidURL", "", code404},
|
||||||
}
|
}
|
||||||
return requests
|
return requests
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user