mirror of
https://github.com/labstack/echo.git
synced 2025-06-15 00:14:57 +02:00
Handling 405 & pre-flight requests
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"golang.org/x/net/websocket"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -115,7 +115,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
|||||||
if c.echo.renderer == nil {
|
if c.echo.renderer == nil {
|
||||||
return RendererNotRegistered
|
return RendererNotRegistered
|
||||||
}
|
}
|
||||||
buf := new (bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err = c.echo.renderer.Render(buf, name, data); err != nil {
|
if err = c.echo.renderer.Render(buf, name, data); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
6
echo.go
6
echo.go
@ -175,8 +175,8 @@ var (
|
|||||||
return NewHTTPError(http.StatusNotFound)
|
return NewHTTPError(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
badRequestHandler = func(c *Context) error {
|
methodNotAllowedHandler = func(c *Context) error {
|
||||||
return NewHTTPError(http.StatusBadRequest)
|
return NewHTTPError(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -462,13 +462,11 @@ func (e *Echo) Routes() []Route {
|
|||||||
|
|
||||||
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
||||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
println(r.Method)
|
|
||||||
c := e.pool.Get().(*Context)
|
c := e.pool.Get().(*Context)
|
||||||
h, echo := e.router.Find(r.Method, r.URL.Path, c)
|
h, echo := e.router.Find(r.Method, r.URL.Path, c)
|
||||||
if echo != nil {
|
if echo != nil {
|
||||||
e = echo
|
e = echo
|
||||||
}
|
}
|
||||||
println(echo)
|
|
||||||
c.reset(r, w, e)
|
c.reset(r, w, e)
|
||||||
|
|
||||||
// Chain middleware with handler in the end
|
// Chain middleware with handler in the end
|
||||||
|
@ -382,12 +382,15 @@ func TestEchoNotFound(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoBadRequest(t *testing.T) {
|
func TestEchoMethodNotAllowed(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r, _ := http.NewRequest("INVALID", "/files", nil)
|
e.Get("/", func(c *Context) error {
|
||||||
|
return c.String(http.StatusOK, "Echo!")
|
||||||
|
})
|
||||||
|
r, _ := http.NewRequest(POST, "/", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoHTTPError(t *testing.T) {
|
func TestEchoHTTPError(t *testing.T) {
|
||||||
|
228
router.go
228
router.go
@ -4,30 +4,33 @@ import "net/http"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Router struct {
|
Router struct {
|
||||||
connectTree *node
|
tree *node
|
||||||
deleteTree *node
|
routes []Route
|
||||||
getTree *node
|
echo *Echo
|
||||||
headTree *node
|
|
||||||
optionsTree *node
|
|
||||||
patchTree *node
|
|
||||||
postTree *node
|
|
||||||
putTree *node
|
|
||||||
traceTree *node
|
|
||||||
routes []Route
|
|
||||||
echo *Echo
|
|
||||||
}
|
}
|
||||||
node struct {
|
node struct {
|
||||||
typ ntype
|
typ ntype
|
||||||
label byte
|
label byte
|
||||||
prefix string
|
prefix string
|
||||||
parent *node
|
parent *node
|
||||||
children children
|
children children
|
||||||
handler HandlerFunc
|
methodHandler *methodHandler
|
||||||
pnames []string
|
pnames []string
|
||||||
echo *Echo
|
echo *Echo
|
||||||
|
}
|
||||||
|
ntype uint8
|
||||||
|
children []*node
|
||||||
|
methodHandler struct {
|
||||||
|
connect HandlerFunc
|
||||||
|
delete HandlerFunc
|
||||||
|
get HandlerFunc
|
||||||
|
head HandlerFunc
|
||||||
|
options HandlerFunc
|
||||||
|
patch HandlerFunc
|
||||||
|
post HandlerFunc
|
||||||
|
put HandlerFunc
|
||||||
|
trace HandlerFunc
|
||||||
}
|
}
|
||||||
ntype uint8
|
|
||||||
children []*node
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -38,17 +41,11 @@ const (
|
|||||||
|
|
||||||
func NewRouter(e *Echo) *Router {
|
func NewRouter(e *Echo) *Router {
|
||||||
return &Router{
|
return &Router{
|
||||||
connectTree: new(node),
|
tree: &node{
|
||||||
deleteTree: new(node),
|
methodHandler: new(methodHandler),
|
||||||
getTree: new(node),
|
},
|
||||||
headTree: new(node),
|
routes: []Route{},
|
||||||
optionsTree: new(node),
|
echo: e,
|
||||||
patchTree: new(node),
|
|
||||||
postTree: new(node),
|
|
||||||
putTree: new(node),
|
|
||||||
traceTree: new(node),
|
|
||||||
routes: []Route{},
|
|
||||||
echo: e,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +87,7 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
|||||||
*e.maxParam = l
|
*e.maxParam = l
|
||||||
}
|
}
|
||||||
|
|
||||||
cn := r.findTree(method) // Current node as root
|
cn := r.tree // Current node as root
|
||||||
if cn == nil {
|
if cn == nil {
|
||||||
panic("echo => invalid method")
|
panic("echo => invalid method")
|
||||||
}
|
}
|
||||||
@ -115,20 +112,20 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
|||||||
cn.prefix = search
|
cn.prefix = search
|
||||||
if h != nil {
|
if h != nil {
|
||||||
cn.typ = t
|
cn.typ = t
|
||||||
cn.handler = h
|
cn.addHandler(method, h)
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
cn.echo = e
|
||||||
}
|
}
|
||||||
} else if l < pl {
|
} else if l < pl {
|
||||||
// Split node
|
// Split node
|
||||||
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
|
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.pnames, cn.echo)
|
||||||
|
|
||||||
// Reset parent node
|
// Reset parent node
|
||||||
cn.typ = stype
|
cn.typ = stype
|
||||||
cn.label = cn.prefix[0]
|
cn.label = cn.prefix[0]
|
||||||
cn.prefix = cn.prefix[:l]
|
cn.prefix = cn.prefix[:l]
|
||||||
cn.children = nil
|
cn.children = nil
|
||||||
cn.handler = nil
|
cn.methodHandler = new(methodHandler)
|
||||||
cn.pnames = nil
|
cn.pnames = nil
|
||||||
cn.echo = nil
|
cn.echo = nil
|
||||||
|
|
||||||
@ -137,12 +134,13 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
|||||||
if l == sl {
|
if l == sl {
|
||||||
// At parent node
|
// At parent node
|
||||||
cn.typ = t
|
cn.typ = t
|
||||||
cn.handler = h
|
cn.addHandler(method, h)
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
cn.echo = e
|
||||||
} else {
|
} else {
|
||||||
// Create child node
|
// Create child node
|
||||||
n = newNode(t, search[l:], cn, nil, h, pnames, e)
|
n = newNode(t, search[l:], cn, nil, new(methodHandler), pnames, e)
|
||||||
|
n.addHandler(method, h)
|
||||||
cn.addChild(n)
|
cn.addChild(n)
|
||||||
}
|
}
|
||||||
} else if l < sl {
|
} else if l < sl {
|
||||||
@ -154,12 +152,13 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Create child node
|
// Create child node
|
||||||
n := newNode(t, search, cn, nil, h, pnames, e)
|
n := newNode(t, search, cn, nil, new(methodHandler), pnames, e)
|
||||||
|
n.addHandler(method, h)
|
||||||
cn.addChild(n)
|
cn.addChild(n)
|
||||||
} else {
|
} else {
|
||||||
// Node already exists
|
// Node already exists
|
||||||
if h != nil {
|
if h != nil {
|
||||||
cn.handler = h
|
cn.addHandler(method, h)
|
||||||
cn.pnames = pnames
|
cn.pnames = pnames
|
||||||
cn.echo = e
|
cn.echo = e
|
||||||
}
|
}
|
||||||
@ -168,16 +167,16 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNode(t ntype, pre string, p *node, c children, h HandlerFunc, pnames []string, e *Echo) *node {
|
func newNode(t ntype, pre string, p *node, c children, mh *methodHandler, pnames []string, e *Echo) *node {
|
||||||
return &node{
|
return &node{
|
||||||
typ: t,
|
typ: t,
|
||||||
label: pre[0],
|
label: pre[0],
|
||||||
prefix: pre,
|
prefix: pre,
|
||||||
parent: p,
|
parent: p,
|
||||||
children: c,
|
children: c,
|
||||||
handler: h,
|
methodHandler: mh,
|
||||||
pnames: pnames,
|
pnames: pnames,
|
||||||
echo: e,
|
echo: e,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,76 +211,57 @@ func (n *node) findChildWithType(t ntype) *node {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) findTree(method string) (n *node) {
|
func (n *node) addHandler(method string, h HandlerFunc) {
|
||||||
switch method[0] {
|
switch method {
|
||||||
case 'G': // GET
|
case GET:
|
||||||
m := uint32(method[2])<<8 | uint32(method[1])<<16 | uint32(method[0])<<24
|
n.methodHandler.get = h
|
||||||
if m == 0x47455400 {
|
case POST:
|
||||||
n = r.getTree
|
n.methodHandler.post = h
|
||||||
}
|
case PUT:
|
||||||
case 'P': // POST, PUT or PATCH
|
n.methodHandler.put = h
|
||||||
switch method[1] {
|
case DELETE:
|
||||||
case 'O': // POST
|
n.methodHandler.delete = h
|
||||||
m := uint32(method[3]) | uint32(method[2])<<8 | uint32(method[1])<<16 |
|
case PATCH:
|
||||||
uint32(method[0])<<24
|
n.methodHandler.patch = h
|
||||||
if m == 0x504f5354 {
|
case OPTIONS:
|
||||||
n = r.postTree
|
n.methodHandler.delete = h
|
||||||
}
|
case HEAD:
|
||||||
case 'U': // PUT
|
n.methodHandler.head = h
|
||||||
m := uint32(method[2])<<8 | uint32(method[1])<<16 | uint32(method[0])<<24
|
case CONNECT:
|
||||||
if m == 0x50555400 {
|
n.methodHandler.connect = h
|
||||||
n = r.putTree
|
case TRACE:
|
||||||
}
|
n.methodHandler.trace = h
|
||||||
case 'A': // PATCH
|
}
|
||||||
m := uint64(method[4])<<24 | uint64(method[3])<<32 | uint64(method[2])<<40 |
|
}
|
||||||
uint64(method[1])<<48 | uint64(method[0])<<56
|
|
||||||
if m == 0x5041544348000000 {
|
func (n *node) findHandler(method string) HandlerFunc {
|
||||||
n = r.patchTree
|
switch method {
|
||||||
}
|
case GET:
|
||||||
}
|
return n.methodHandler.get
|
||||||
case 'D': // DELETE
|
case POST:
|
||||||
m := uint64(method[5])<<16 | uint64(method[4])<<24 | uint64(method[3])<<32 |
|
return n.methodHandler.post
|
||||||
uint64(method[2])<<40 | uint64(method[1])<<48 | uint64(method[0])<<56
|
case PUT:
|
||||||
if m == 0x44454c4554450000 {
|
return n.methodHandler.put
|
||||||
n = r.deleteTree
|
case DELETE:
|
||||||
}
|
return n.methodHandler.delete
|
||||||
case 'C': // CONNECT
|
case PATCH:
|
||||||
m := uint64(method[6])<<8 | uint64(method[5])<<16 | uint64(method[4])<<24 |
|
return n.methodHandler.patch
|
||||||
uint64(method[3])<<32 | uint64(method[2])<<40 | uint64(method[1])<<48 |
|
case OPTIONS:
|
||||||
uint64(method[0])<<56
|
return n.methodHandler.delete
|
||||||
if m == 0x434f4e4e45435400 {
|
case HEAD:
|
||||||
n = r.connectTree
|
return n.methodHandler.head
|
||||||
}
|
case CONNECT:
|
||||||
case 'H': // HEAD
|
return n.methodHandler.connect
|
||||||
m := uint32(method[3]) | uint32(method[2])<<8 | uint32(method[1])<<16 |
|
case TRACE:
|
||||||
uint32(method[0])<<24
|
return n.methodHandler.trace
|
||||||
if m == 0x48454144 {
|
default:
|
||||||
n = r.headTree
|
return nil
|
||||||
}
|
|
||||||
case 'O': // OPTIONS
|
|
||||||
m := uint64(method[6])<<8 | uint64(method[5])<<16 | uint64(method[4])<<24 |
|
|
||||||
uint64(method[3])<<32 | uint64(method[2])<<40 | uint64(method[1])<<48 |
|
|
||||||
uint64(method[0])<<56
|
|
||||||
if m == 0x4f5054494f4e5300 {
|
|
||||||
n = r.optionsTree
|
|
||||||
}
|
|
||||||
case 'T': // TRACE
|
|
||||||
m := uint64(method[4])<<24 | uint64(method[3])<<32 | uint64(method[2])<<40 |
|
|
||||||
uint64(method[1])<<48 | uint64(method[0])<<56
|
|
||||||
if m == 0x5452414345000000 {
|
|
||||||
n = r.traceTree
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
|
||||||
h = notFoundHandler
|
h = notFoundHandler
|
||||||
cn := r.findTree(method) // Current node as root
|
cn := r.tree // Current node as root
|
||||||
if cn == nil {
|
|
||||||
h = badRequestHandler
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip trailing slash
|
// Strip trailing slash
|
||||||
if r.echo.stripTrailingSlash {
|
if r.echo.stripTrailingSlash {
|
||||||
@ -339,14 +319,6 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if search == "" {
|
if search == "" {
|
||||||
if cn.handler == nil {
|
|
||||||
// Look up for match-any, might have an empty value for *, e.g.
|
|
||||||
// serving a directory. Issue #207
|
|
||||||
if cn = cn.findChildWithType(mtype); cn == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ctx.pvalues[len(cn.pnames)-1] = ""
|
|
||||||
}
|
|
||||||
goto Found
|
goto Found
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,7 +368,17 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
|||||||
|
|
||||||
Found:
|
Found:
|
||||||
ctx.pnames = cn.pnames
|
ctx.pnames = cn.pnames
|
||||||
h = cn.handler
|
h = cn.findHandler(method)
|
||||||
|
if h == nil {
|
||||||
|
h = methodNotAllowedHandler
|
||||||
|
// Look up for match-any, might have an empty value for *, e.g.
|
||||||
|
// serving a directory. Issue #207
|
||||||
|
if cn = cn.findChildWithType(mtype); cn == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h = cn.findHandler(method)
|
||||||
|
ctx.pvalues[len(cn.pnames)-1] = ""
|
||||||
|
}
|
||||||
e = cn.echo
|
e = cn.echo
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -567,16 +567,6 @@ func TestRouterAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouterAddInvalidMethod(t *testing.T) {
|
|
||||||
e := New()
|
|
||||||
r := e.router
|
|
||||||
assert.Panics(t, func() {
|
|
||||||
r.Add("INVALID", "/", func(*Context) error {
|
|
||||||
return nil
|
|
||||||
}, e)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRouterServeHTTP(t *testing.T) {
|
func TestRouterServeHTTP(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
r := e.router
|
r := e.router
|
||||||
@ -600,7 +590,7 @@ func TestRouterServeHTTP(t *testing.T) {
|
|||||||
|
|
||||||
func (n *node) printTree(pfx string, tail bool) {
|
func (n *node) printTree(pfx string, tail bool) {
|
||||||
p := prefix(tail, pfx, "└── ", "├── ")
|
p := prefix(tail, pfx, "└── ", "├── ")
|
||||||
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.typ, n.parent, n.handler)
|
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.typ, n.parent, n.methodHandler)
|
||||||
|
|
||||||
children := n.children
|
children := n.children
|
||||||
l := len(children)
|
l := len(children)
|
||||||
|
@ -34,13 +34,16 @@ a:link {
|
|||||||
.page-content header {
|
.page-content header {
|
||||||
padding-bottom: 16px;
|
padding-bottom: 16px;
|
||||||
}
|
}
|
||||||
|
.menu {
|
||||||
|
margin-right: 40px;
|
||||||
|
}
|
||||||
.menu a {
|
.menu a {
|
||||||
display: block;
|
display: block;
|
||||||
color: #757575;
|
color: #757575;
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
border-left:2px #F06292 solid;
|
border-left:4px #F06292 solid;
|
||||||
}
|
}
|
||||||
.menu a:hover {
|
.menu a:hover:not(.active) {
|
||||||
background-color: #E0E0E0;
|
background-color: #E0E0E0;
|
||||||
}
|
}
|
||||||
.menu .active {
|
.menu .active {
|
||||||
|
Reference in New Issue
Block a user