Skip to content
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
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, e

type disableLogger struct{}

// Printf implements the fasthttp Logger interface and discards log output.
func (*disableLogger) Printf(string, ...any) {
}

Expand Down
4 changes: 3 additions & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var bindPool = sync.Pool{
},
}

// Bind struct
// Bind provides helper methods for binding request data to Go values.
type Bind struct {
ctx Ctx
dontHandleErrs bool
Expand Down Expand Up @@ -331,6 +331,8 @@ func (b *Bind) Body(out any) error {
return ErrUnprocessableEntity
}

// All binds values from URI params, the request body, the query string,
// headers, and cookies into the provided struct in precedence order.
func (b *Bind) All(out any) error {
outVal := reflect.ValueOf(out)
if outVal.Kind() != reflect.Ptr || outVal.Elem().Kind() != reflect.Struct {
Expand Down
3 changes: 3 additions & 0 deletions binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var MsgPackBinderPool = sync.Pool{
},
}

// GetFromThePool retrieves a binder from the provided sync.Pool and panics if
// the stored value cannot be cast to the requested type.
func GetFromThePool[T any](pool *sync.Pool) T {
binder, ok := pool.Get().(T)
if !ok {
Expand All @@ -80,6 +82,7 @@ func GetFromThePool[T any](pool *sync.Pool) T {
return binder
}

// PutToThePool returns the binder to the provided sync.Pool.
func PutToThePool[T any](pool *sync.Pool, binder T) {
pool.Put(binder)
}
4 changes: 4 additions & 0 deletions binder/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ func (b *CBORBinding) Reset() {
b.CBORDecoder = nil
}

// UnimplementedCborMarshal panics to signal that a CBOR marshaler must be
// configured before CBOR support can be used.
func UnimplementedCborMarshal(_ any) ([]byte, error) {
panic("Must explicitly setup CBOR, please check docs: https://docs.gofiber.io/next/guide/advance-format#cbor")
}

// UnimplementedCborUnmarshal panics to signal that a CBOR unmarshaler must be
// configured before CBOR support can be used.
func UnimplementedCborUnmarshal(_ []byte, _ any) error {
panic("Must explicitly setup CBOR, please check docs: https://docs.gofiber.io/next/guide/advance-format#cbor")
}
2 changes: 1 addition & 1 deletion binder/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/valyala/fasthttp"
)

// v is the header binder for header request body.
// HeaderBinding is the binder implementation used to populate values from HTTP headers.
type HeaderBinding struct {
EnableSplitting bool
}
Expand Down
2 changes: 1 addition & 1 deletion binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func equalFieldType(out any, kind reflect.Kind, key, aliasTag string) bool {
return false
}

// Get content type from content type header
// FilterFlags returns the media type value by trimming any parameters from a Content-Type header.
func FilterFlags(content string) string {
for i, char := range content {
if char == ' ' || char == ';' {
Expand Down
4 changes: 4 additions & 0 deletions binder/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ func (b *MsgPackBinding) Reset() {
b.MsgPackDecoder = nil
}

// UnimplementedMsgpackMarshal panics to signal that a Msgpack marshaler must
// be configured before MsgPack support can be used.
func UnimplementedMsgpackMarshal(_ any) ([]byte, error) {
panic("Must explicit setup Msgpack, please check docs: https://docs.gofiber.io/next/guide/advance-format#msgpack")
}

// UnimplementedMsgpackUnmarshal panics to signal that a Msgpack unmarshaler
// must be configured before MsgPack support can be used.
func UnimplementedMsgpackUnmarshal(_ []byte, _ any) error {
panic("Must explicit setup Msgpack, please check docs: https://docs.gofiber.io/next/guide/advance-format#msgpack")
}
2 changes: 1 addition & 1 deletion binder/uri.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package binder

// uriBinding is the URI binder for URI parameters.
// URIBinding is the binder implementation for populating values from route parameters.
type URIBinding struct{}

// Name returns the binding name.
Expand Down
3 changes: 3 additions & 0 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,18 @@ type pair struct {
v []string
}

// Len implements sort.Interface and reports the number of tracked keys.
func (p *pair) Len() int {
return len(p.k)
}

// Swap implements sort.Interface and swaps the entries at the provided indices.
func (p *pair) Swap(i, j int) {
p.k[i], p.k[j] = p.k[j], p.k[i]
p.v[i], p.v[j] = p.v[j], p.v[i]
}

// Less implements sort.Interface and orders entries lexicographically by key.
func (p *pair) Less(i, j int) bool {
return p.k[i] < p.k[j]
}
Expand Down
9 changes: 3 additions & 6 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type DefaultCtx struct {
matched bool // Non use route matched
}

// TLSHandler object
// TLSHandler hosts the callback hooks Fiber invokes while negotiating TLS
// connections, including optional client certificate lookups.
type TLSHandler struct {
clientHelloInfo *tls.ClientHelloInfo
}
Expand Down Expand Up @@ -149,11 +150,7 @@ func (*DefaultCtx) Done() <-chan struct{} {
return nil
}

// If Done is not yet closed, Err returns nil.
// If Done is closed, Err returns a non-nil error explaining why:
// context.DeadlineExceeded if the context's deadline passed,
// or context.Canceled if the context was canceled for some other reason.
// After Err returns a non-nil error, successive calls to Err return the same error.
// Err mirrors context.Err, returning nil until cancellation and then the terminal error value.
//
// Due to current limitations in how fasthttp works, Err operates as a nop.
// See: https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945
Expand Down
4 changes: 4 additions & 0 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/valyala/fasthttp"
)

// CustomCtx extends Ctx with the additional methods required by Fiber's
// internals and middleware helpers.
type CustomCtx interface {
Ctx

Expand All @@ -30,6 +32,8 @@ type CustomCtx interface {
setRoute(route *Route)
}

// NewDefaultCtx constructs the default context implementation bound to the
// provided application.
func NewDefaultCtx(app *App) *DefaultCtx {
// return ctx
ctx := &DefaultCtx{
Expand Down
11 changes: 6 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,24 @@ type (

// encoding/json errors
type (
// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil pointer.)
InvalidUnmarshalError = json.InvalidUnmarshalError

// A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
// MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
MarshalerError = json.MarshalerError

// A SyntaxError is a description of a JSON syntax error.
// SyntaxError is a description of a JSON syntax error.
SyntaxError = json.SyntaxError

// An UnmarshalTypeError describes a JSON value that was
// UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
UnmarshalTypeError = json.UnmarshalTypeError

// An UnsupportedTypeError is returned by Marshal when attempting
// UnsupportedTypeError is returned by Marshal when attempting
// to encode an unsupported value type.
UnsupportedTypeError = json.UnsupportedTypeError

// UnsupportedValueError exposes json.UnsupportedValueError to describe unsupported values encountered during encoding.
UnsupportedValueError = json.UnsupportedValueError
)
3 changes: 2 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"reflect"
)

// Group struct
// Group represents a collection of routes that share middleware and a common
// path prefix.
type Group struct {
app *App
parentGroup *Group
Expand Down
26 changes: 22 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,15 @@ type testConn struct {
sync.Mutex
}

// Read implements net.Conn by reading from the buffered input.
func (c *testConn) Read(b []byte) (int, error) {
c.Lock()
defer c.Unlock()

return c.r.Read(b) //nolint:wrapcheck // This must not be wrapped
}

// Write implements net.Conn by appending to the buffered output.
func (c *testConn) Write(b []byte) (int, error) {
c.Lock()
defer c.Unlock()
Expand All @@ -767,6 +769,7 @@ func (c *testConn) Write(b []byte) (int, error) {
return c.w.Write(b) //nolint:wrapcheck // This must not be wrapped
}

// Close marks the connection as closed and prevents further writes.
func (c *testConn) Close() error {
c.Lock()
defer c.Unlock()
Expand All @@ -775,10 +778,19 @@ func (c *testConn) Close() error {
return nil
}

func (*testConn) LocalAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
func (*testConn) RemoteAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
func (*testConn) SetDeadline(_ time.Time) error { return nil }
func (*testConn) SetReadDeadline(_ time.Time) error { return nil }
// LocalAddr implements net.Conn and returns a placeholder address.
func (*testConn) LocalAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }

// RemoteAddr implements net.Conn and returns a placeholder address.
func (*testConn) RemoteAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }

// SetDeadline implements net.Conn but is a no-op for the in-memory connection.
func (*testConn) SetDeadline(_ time.Time) error { return nil }

// SetReadDeadline implements net.Conn but is a no-op for the in-memory connection.
func (*testConn) SetReadDeadline(_ time.Time) error { return nil }

// SetWriteDeadline implements net.Conn but is a no-op for the in-memory connection.
func (*testConn) SetWriteDeadline(_ time.Time) error { return nil }

func toStringImmutable(b []byte) string {
Expand Down Expand Up @@ -969,22 +981,28 @@ func genericParseType[V GenericType](str string) (V, error) {
}
}

// GenericType enumerates the values that can be parsed from strings by the
// generic helper functions.
type GenericType interface {
GenericTypeInteger | GenericTypeFloat | bool | string | []byte
}

// GenericTypeInteger is the union of all supported integer types.
type GenericTypeInteger interface {
GenericTypeIntegerSigned | GenericTypeIntegerUnsigned
}

// GenericTypeIntegerSigned is the union of supported signed integer types.
type GenericTypeIntegerSigned interface {
int | int8 | int16 | int32 | int64
}

// GenericTypeIntegerUnsigned is the union of supported unsigned integer types.
type GenericTypeIntegerUnsigned interface {
uint | uint8 | uint16 | uint32 | uint64
}

// GenericTypeFloat is the union of supported floating-point types.
type GenericTypeFloat interface {
float32 | float64
}
26 changes: 17 additions & 9 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import (
"github.com/gofiber/fiber/v3/log"
)

// OnRouteHandler Handlers define a function to create hooks for Fiber.
type (
OnRouteHandler = func(Route) error
OnNameHandler = OnRouteHandler
OnGroupHandler = func(Group) error
OnGroupNameHandler = OnGroupHandler
OnListenHandler = func(ListenData) error
OnPreShutdownHandler = func() error
// OnRouteHandler defines the hook signature invoked whenever a route is registered.
OnRouteHandler = func(Route) error
// OnNameHandler shares the OnRouteHandler signature for route naming callbacks.
OnNameHandler = OnRouteHandler
// OnGroupHandler defines the hook signature invoked whenever a group is registered.
OnGroupHandler = func(Group) error
// OnGroupNameHandler shares the OnGroupHandler signature for group naming callbacks.
OnGroupNameHandler = OnGroupHandler
// OnListenHandler runs when the application begins listening and receives the listener details.
OnListenHandler = func(ListenData) error
// OnPreShutdownHandler runs before the application shuts down.
OnPreShutdownHandler = func() error
// OnPostShutdownHandler runs after shutdown and receives the shutdown result.
OnPostShutdownHandler = func(error) error
OnForkHandler = func(int) error
OnMountHandler = func(*App) error
// OnForkHandler runs inside a forked worker process and receives the worker ID.
OnForkHandler = func(int) error
// OnMountHandler runs after a sub-application mounts to a parent and receives the parent app reference.
OnMountHandler = func(*App) error
)

// Hooks is a struct to use it with App.
Expand Down
12 changes: 8 additions & 4 deletions internal/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
utils "github.com/gofiber/utils/v2"
)

// Storage stores arbitrary values in memory for use in tests and benchmarks.
type Storage struct {
data map[string]item // data
sync.RWMutex
Expand All @@ -20,6 +21,7 @@ type item struct {
e uint32 // exp
}

// New constructs an in-memory Storage initialized with a background GC loop.
func New() *Storage {
store := &Storage{
data: make(map[string]item),
Expand All @@ -29,7 +31,8 @@ func New() *Storage {
return store
}

// Get value by key
// Get retrieves the value stored under key, returning nil when the entry does
// not exist or has expired.
func (s *Storage) Get(key string) any {
s.RLock()
v, ok := s.data[key]
Expand All @@ -40,7 +43,8 @@ func (s *Storage) Get(key string) any {
return v.v
}

// Set key with value
// Set stores val under key and applies the optional ttl before expiring the
// entry. A non-positive ttl keeps the item forever.
func (s *Storage) Set(key string, val any, ttl time.Duration) {
var exp uint32
if ttl > 0 {
Expand All @@ -52,14 +56,14 @@ func (s *Storage) Set(key string, val any, ttl time.Duration) {
s.Unlock()
}

// Delete key by key
// Delete removes key and its associated value from the storage.
func (s *Storage) Delete(key string) {
s.Lock()
delete(s.data, key)
s.Unlock()
}

// Reset all keys
// Reset clears the storage by dropping every stored key.
func (s *Storage) Reset() {
nd := make(map[string]item)
s.Lock()
Expand Down
Loading
Loading