Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️Refactor: remove redundant field method in DefaultCtx #3372

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,29 @@ const (
DefaultWriteBufferSize = 4096
)

const (
methodGet = iota
methodHead
methodPost
methodPut
methodDelete
methodConnect
methodOptions
methodTrace
methodPatch
)

// HTTP methods enabled by default
var DefaultMethods = []string{
MethodGet,
MethodHead,
MethodPost,
MethodPut,
MethodDelete,
MethodConnect,
MethodOptions,
MethodTrace,
MethodPatch,
methodGet: MethodGet,
methodHead: MethodHead,
methodPost: MethodPost,
methodPut: MethodPut,
methodDelete: MethodDelete,
methodConnect: MethodConnect,
methodOptions: MethodOptions,
methodTrace: MethodTrace,
methodPatch: MethodPatch,
}

// DefaultErrorHandler that process return errors from handlers
Expand Down
24 changes: 10 additions & 14 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type DefaultCtx struct {
res *DefaultRes // Default response api reference
values [maxParams]string // Route parameter values
viewBindMap sync.Map // Default view map to bind template engine
method string // HTTP method
baseURI string // HTTP base uri
pathOriginal string // Original HTTP path
flashMessages redirectionMsgs // Flash messages
Expand All @@ -70,7 +69,7 @@ type DefaultCtx struct {
treePathHash int // Hash of the path for the search in the tree
indexRoute int // Index of the current route
indexHandler int // Index of the current handler
methodINT int // HTTP method INT equivalent
methodInt int // HTTP method INT equivalent
matched bool // Non use route matched
}

Expand Down Expand Up @@ -1006,19 +1005,17 @@ func (c *DefaultCtx) Location(path string) {
func (c *DefaultCtx) Method(override ...string) string {
if len(override) == 0 {
// Nothing to override, just return current method from context
return c.method
return c.app.method(c.methodInt)
}

method := utils.ToUpper(override[0])
mINT := c.app.methodInt(method)
if mINT == -1 {
methodInt := c.app.methodInt(method)
if methodInt == -1 {
// Provided override does not valid HTTP method, no override, return current method
return c.method
return c.app.method(c.methodInt)
}

c.method = method
c.methodINT = mINT
return c.method
c.methodInt = methodInt
return method
}

// MultipartForm parse form entries from binary.
Expand Down Expand Up @@ -1486,7 +1483,7 @@ func (c *DefaultCtx) Route() *Route {
return &Route{
path: c.pathOriginal,
Path: c.pathOriginal,
Method: c.method,
Method: c.Method(),
Handlers: make([]Handler, 0),
Params: make([]string, 0),
}
Expand Down Expand Up @@ -1919,8 +1916,7 @@ func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {
// Set paths
c.pathOriginal = c.app.getString(fctx.URI().PathOriginal())
// Set method
c.method = c.app.getString(fctx.Request.Header.Method())
c.methodINT = c.app.methodInt(c.method)
c.methodInt = c.app.methodInt(utils.UnsafeString(fctx.Request.Header.Method()))
// Attach *fasthttp.RequestCtx to ctx
c.fasthttp = fctx
// reset base uri
Expand Down Expand Up @@ -1952,7 +1948,7 @@ func (c *DefaultCtx) getBody() []byte {

// Methods to use with next stack.
func (c *DefaultCtx) getMethodINT() int {
return c.methodINT
return c.methodInt
}

func (c *DefaultCtx) getIndexRoute() int {
Expand Down
31 changes: 14 additions & 17 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -652,39 +653,35 @@ func getBytesImmutable(s string) []byte {
func (app *App) methodInt(s string) int {
// For better performance
if len(app.configured.RequestMethods) == 0 {
// TODO: Use iota instead
switch s {
case MethodGet:
return 0
return methodGet
case MethodHead:
return 1
return methodHead
case MethodPost:
return 2
return methodPost
case MethodPut:
return 3
return methodPut
case MethodDelete:
return 4
return methodDelete
case MethodConnect:
return 5
return methodConnect
case MethodOptions:
return 6
return methodOptions
case MethodTrace:
return 7
return methodTrace
case MethodPatch:
return 8
return methodPatch
default:
return -1
}
}

// For method customization
for i, v := range app.config.RequestMethods {
if s == v {
return i
}
}
return slices.Index(app.config.RequestMethods, s)
}

return -1
func (app *App) method(methodInt int) string {
return app.config.RequestMethods[methodInt]
}

// IsMethodSafe reports whether the HTTP method is considered safe.
Expand Down
8 changes: 4 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func (app *App) nextCustom(c CustomCtx) (bool, error) { //nolint:unparam // bool

func (app *App) next(c *DefaultCtx) (bool, error) {
// Get stack length
tree, ok := app.treeStack[c.methodINT][c.treePathHash]
tree, ok := app.treeStack[c.methodInt][c.treePathHash]
if !ok {
tree = app.treeStack[c.methodINT][0]
tree = app.treeStack[c.methodInt][0]
}
lenTree := len(tree) - 1

Expand Down Expand Up @@ -202,7 +202,7 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
}

// If c.Next() does not match, return 404
err := NewError(StatusNotFound, "Cannot "+c.method+" "+html.EscapeString(c.pathOriginal))
err := NewError(StatusNotFound, "Cannot "+c.Method()+" "+html.EscapeString(c.pathOriginal))
if !c.matched && app.methodExist(c) {
// If no match, scan stack again if other methods match the request
// Moved from app.handler because middleware may break the route chain
Expand All @@ -221,7 +221,7 @@ func (app *App) defaultRequestHandler(rctx *fasthttp.RequestCtx) {
defer app.ReleaseCtx(ctx)

// Check if the HTTP method is valid
if ctx.methodINT == -1 {
if ctx.methodInt == -1 {
_ = ctx.SendStatus(StatusNotImplemented) //nolint:errcheck // Always return nil
return
}
Expand Down
Loading