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 ( import (
"net/http" "net/http"
"path" "path"
"regexp"
"strings" "strings"
) )
var ( // anyMethods for RouterGroup Any method
// regEnLetter matches english letters for http method name var anyMethods = []string{
regEnLetter = regexp.MustCompile("^[A-Z]+$") http.MethodGet,
http.MethodPost,
// anyMethods for RouterGroup Any method http.MethodPut,
anyMethods = []string{ http.MethodPatch,
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodHead,
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect, http.MethodOptions,
http.MethodTrace, http.MethodDelete,
} http.MethodConnect,
) http.MethodTrace,
}
// IRouter defines all router handle interface includes single and group router. // IRouter defines all router handle interface includes single and group router.
type IRouter interface { 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 // frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy). // communication with a proxy).
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes { 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") panic("http method " + httpMethod + " is not valid")
} }
return group.handle(httpMethod, relativePath, handlers) 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). // POST is a shortcut for router.Handle("POST", path, handlers).
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes { func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle(http.MethodPost, relativePath, handlers) return group.handle(http.MethodPost, relativePath, handlers)