blob: 8d933a4de76bd45e7da63564309f6878565c1042 [file]
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package middleware
import (
"net/http"
"slices"
)
const maxURILength = 1000
// AcceptRequests serves 405 (Method Not Allowed) for any method not on the
// given list and 414 (Method Request URI Too Long) for any URI that exceeds
// the maxURILength.
func AcceptRequests(methods ...string) Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(r.URL.String()) >= maxURILength {
http.Error(w, http.StatusText(http.StatusRequestURITooLong), http.StatusRequestURITooLong)
return
}
if slices.Contains(methods, r.Method) {
h.ServeHTTP(w, r)
return
}
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
})
}
}