Merge 3e6e8845319ceed5db39234a5eda782cac067eb6 into 2a794cd0b0faa7d829291375b27a3467ea972b0d

This commit is contained in:
Name 2025-12-04 11:31:57 +08:00 committed by GitHub
commit efb72c95cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,21 +7,21 @@ package gin
import (
"net/http"
"path"
"regexp"
"strings"
)
var (
// regEnLetter matches english letters for http method name
regEnLetter = regexp.MustCompile("^[A-Z]+$")
// anyMethods for RouterGroup Any method
anyMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
var anyMethods = []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodHead,
http.MethodOptions,
http.MethodDelete,
http.MethodConnect,
http.MethodTrace,
}
)
// IRouter defines all router handle interface includes single and group router.
type IRouter interface {
@ -101,12 +101,22 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {
if matched := regEnLetter.MatchString(httpMethod); !matched {
if !isHTTPMethod(httpMethod) {
panic("http method " + httpMethod + " is not valid")
}
return group.handle(httpMethod, relativePath, handlers)
}
// isHTTPMethod checks if the method is a valid HTTP method.
func isHTTPMethod(method string) bool {
for _, m := range anyMethods {
if m == method {
return true
}
}
return false
}
// POST is a shortcut for router.Handle("POST", path, handlers).
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle(http.MethodPost, relativePath, handlers)