Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ func (engine *Engine) Init() error {
engine.options.TLS.NextProtos = append(engine.options.TLS.NextProtos, suite.HTTP1)
}

if len(engine.noRoute) <= 0 {
engine.noRoute = []app.HandlerFunc{hertzRouteNotFound}
engine.rebuild404Handlers()
}

if len(engine.noMethod) <= 0 {
engine.noMethod = []app.HandlerFunc{hertzNotAllowedMethod}
engine.rebuild405Handlers()
}

if !atomic.CompareAndSwapUint32(&engine.status, 0, statusInitialized) {
return errInitFailed
}
Expand All @@ -417,6 +427,14 @@ func (engine *Engine) listenAndServe() error {
return engine.transport.ListenAndServe(engine.onData)
}

// hertzRouteNotFound is default NoRoute handler when users do not configure any handler with NoRoute.
// it is not an anonymous function, so utils.NameOfFunction could obtain a specific function name to assist troubleshooting.
func hertzRouteNotFound(c context.Context, ctx *app.RequestContext) {}

// hertzNotAllowedMethod is default NoMethod handler when users do not configure any handler with NoMethod.
// it is not an anonymous function, so utils.NameOfFunction could obtain a specific function name to assist troubleshooting.
func hertzNotAllowedMethod(c context.Context, ctx *app.RequestContext) {}

func (c *hijackConn) Close() error {
if !c.e.KeepHijackedConns {
// when we do not keep hijacked connections,
Expand Down
18 changes: 18 additions & 0 deletions pkg/route/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
Expand All @@ -62,6 +63,7 @@ import (
errs "github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"github.com/cloudwego/hertz/pkg/common/test/mock"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/network/standard"
"github.com/cloudwego/hertz/pkg/protocol"
Expand Down Expand Up @@ -517,6 +519,22 @@ func TestSetEngineRun(t *testing.T) {
assert.True(t, e.IsRunning())
}

func TestEngine_InitNoRouteAndNoMethod(t *testing.T) {
e := NewEngine(config.NewOptions(nil))
err := e.Init()
assert.Assert(t, err == nil, err)
assert.Assert(t, len(e.allNoRoute) == 1, e.allNoRoute)
hdlName := utils.NameOfFunction(e.allNoRoute[0])
pos := strings.LastIndexByte(hdlName, '.')
assert.Assert(t, pos >= 0, hdlName)
assert.Assert(t, hdlName[pos+1:] == "hertzRouteNotFound", hdlName)
assert.Assert(t, len(e.allNoMethod) == 1, e.allNoMethod)
hdlName = utils.NameOfFunction(e.allNoMethod[0])
pos = strings.LastIndexByte(hdlName, '.')
assert.Assert(t, pos >= 0, hdlName)
assert.Assert(t, hdlName[pos+1:] == "hertzNotAllowedMethod", hdlName)
}

type mockConn struct{}

func (m *mockConn) SetWriteTimeout(t time.Duration) error {
Expand Down