mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 06:42:10 +08:00
Fix Allow header according to RFC 7231
This commit is contained in:
parent
b94d23d1b4
commit
326ed438d4
14
gin.go
14
gin.go
@ -11,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/internal/bytesconv"
|
"github.com/gin-gonic/gin/internal/bytesconv"
|
||||||
@ -434,16 +435,23 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if engine.HandleMethodNotAllowed {
|
if engine.HandleMethodNotAllowed {
|
||||||
|
// According to RFC 7231 section 6.5.5, MUST generate an Allow header field in response
|
||||||
|
// containing a list of the target resource's currently supported methods.
|
||||||
|
allowed := make([]string, 0, len(t)-1)
|
||||||
for _, tree := range engine.trees {
|
for _, tree := range engine.trees {
|
||||||
if tree.method == httpMethod {
|
if tree.method == httpMethod {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if value := tree.root.getValue(rPath, nil, unescape); value.handlers != nil {
|
if value := tree.root.getValue(rPath, nil, unescape); value.handlers != nil {
|
||||||
c.handlers = engine.allNoMethod
|
allowed = append(allowed, tree.method)
|
||||||
serveError(c, http.StatusMethodNotAllowed, default405Body)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(allowed) > 0 {
|
||||||
|
c.handlers = engine.allNoMethod
|
||||||
|
c.writermem.Header()["Allow"] = []string{strings.Join(allowed, ", ")}
|
||||||
|
serveError(c, http.StatusMethodNotAllowed, default405Body)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.handlers = engine.allNoRoute
|
c.handlers = engine.allNoRoute
|
||||||
serveError(c, http.StatusNotFound, default404Body)
|
serveError(c, http.StatusNotFound, default404Body)
|
||||||
|
@ -393,6 +393,18 @@ func TestRouteNotAllowedEnabled2(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRouteNotAllowedEnabled3(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
router.HandleMethodNotAllowed = true
|
||||||
|
router.GET("/path", func(c *Context) {})
|
||||||
|
router.POST("/path", func(c *Context) {})
|
||||||
|
w := performRequest(router, http.MethodPut, "/path")
|
||||||
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
|
allowed := w.Header().Get("Allow")
|
||||||
|
assert.Contains(t, allowed, "GET")
|
||||||
|
assert.Contains(t, allowed, "POST")
|
||||||
|
}
|
||||||
|
|
||||||
func TestRouteNotAllowedDisabled(t *testing.T) {
|
func TestRouteNotAllowedDisabled(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
router.HandleMethodNotAllowed = false
|
router.HandleMethodNotAllowed = false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user