diff --git a/app.go b/app.go index 6fc214a35e..28d232edb2 100644 --- a/app.go +++ b/app.go @@ -31,7 +31,7 @@ import ( ) // Version of current fiber package -const Version = "2.2.3" +const Version = "2.2.4" // Handler defines a function to serve HTTP requests. type Handler = func(*Ctx) error diff --git a/internal/gotiny/decEngine.go b/internal/gotiny/decEngine.go index 07617a7e9a..f08e7b8544 100644 --- a/internal/gotiny/decEngine.go +++ b/internal/gotiny/decEngine.go @@ -1,6 +1,7 @@ package gotiny import ( + "fmt" "reflect" "sync" "time" @@ -178,7 +179,8 @@ func buildDecEngine(rt reflect.Type, engPtr *decEng) { decString(d, unsafe.Pointer(&name)) et, has := name2type[name] if !has { - panic("unknown typ:" + name) + //panic("unknown typ:" + name) + fmt.Println("[session] Register this type first with the `RegisterType` method.") } v := reflect.NewAt(rt, p).Elem() var ev reflect.Value @@ -194,7 +196,7 @@ func buildDecEngine(rt reflect.Type, engPtr *decEng) { } } case reflect.Chan, reflect.Func: - panic("not support " + rt.String() + " type") + //panic("not support " + rt.String() + " type") default: engine = baseDecEngines[kind] } diff --git a/internal/gotiny/encEngine.go b/internal/gotiny/encEngine.go index 9964ea17a0..ff403ba0dd 100644 --- a/internal/gotiny/encEngine.go +++ b/internal/gotiny/encEngine.go @@ -187,7 +187,7 @@ func buildEncEngine(rt reflect.Type, engPtr *encEng) { } } case reflect.Chan, reflect.Func: - panic("not support " + rt.String() + " type") + //panic("not support " + rt.String() + " type") default: engine = encEngines[kind] } diff --git a/middleware/cache/cache.go b/middleware/cache/cache.go index 6d7afb150f..6dfe3d3567 100644 --- a/middleware/cache/cache.go +++ b/middleware/cache/cache.go @@ -87,7 +87,9 @@ func New(config ...Config) fiber.Handler { c.Response().SetBodyRaw(e.body) c.Response().SetStatusCode(e.status) c.Response().Header.SetContentTypeBytes(e.ctype) - + if len(e.cencoding) > 0 { + c.Response().Header.SetBytesV(fiber.HeaderContentEncoding, e.cencoding) + } // Set Cache-Control header if enabled if cfg.CacheControl { maxAge := strconv.FormatUint(e.exp-ts, 10) @@ -107,6 +109,7 @@ func New(config ...Config) fiber.Handler { e.body = utils.SafeBytes(c.Response().Body()) e.status = c.Response().StatusCode() e.ctype = utils.SafeBytes(c.Response().Header.ContentType()) + e.cencoding = utils.SafeBytes(c.Response().Header.Peek(fiber.HeaderContentEncoding)) // For external Storage we store raw body seperated if cfg.Storage != nil { diff --git a/middleware/cache/manager.go b/middleware/cache/manager.go index 5a30dce0b1..dbeccf7076 100644 --- a/middleware/cache/manager.go +++ b/middleware/cache/manager.go @@ -13,10 +13,11 @@ import ( // don't forget to replace the msgp import path to: // "github.com/gofiber/fiber/v2/internal/msgp" type item struct { - body []byte - ctype []byte - status int - exp uint64 + body []byte + ctype []byte + cencoding []byte + status int + exp uint64 } //msgp:ignore manager diff --git a/middleware/cache/manager_msgp.go b/middleware/cache/manager_msgp.go index 898aace91e..b664caf8b9 100644 --- a/middleware/cache/manager_msgp.go +++ b/middleware/cache/manager_msgp.go @@ -36,6 +36,12 @@ func (z *item) DecodeMsg(dc *msgp.Reader) (err error) { err = msgp.WrapError(err, "ctype") return } + case "cencoding": + z.cencoding, err = dc.ReadBytes(z.cencoding) + if err != nil { + err = msgp.WrapError(err, "cencoding") + return + } case "status": z.status, err = dc.ReadInt() if err != nil { @@ -61,9 +67,9 @@ func (z *item) DecodeMsg(dc *msgp.Reader) (err error) { // EncodeMsg implements msgp.Encodable func (z *item) EncodeMsg(en *msgp.Writer) (err error) { - // map header, size 4 + // map header, size 5 // write "body" - err = en.Append(0x84, 0xa4, 0x62, 0x6f, 0x64, 0x79) + err = en.Append(0x85, 0xa4, 0x62, 0x6f, 0x64, 0x79) if err != nil { return } @@ -82,6 +88,16 @@ func (z *item) EncodeMsg(en *msgp.Writer) (err error) { err = msgp.WrapError(err, "ctype") return } + // write "cencoding" + err = en.Append(0xa9, 0x63, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67) + if err != nil { + return + } + err = en.WriteBytes(z.cencoding) + if err != nil { + err = msgp.WrapError(err, "cencoding") + return + } // write "status" err = en.Append(0xa6, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73) if err != nil { @@ -108,13 +124,16 @@ func (z *item) EncodeMsg(en *msgp.Writer) (err error) { // MarshalMsg implements msgp.Marshaler func (z *item) MarshalMsg(b []byte) (o []byte, err error) { o = msgp.Require(b, z.Msgsize()) - // map header, size 4 + // map header, size 5 // string "body" - o = append(o, 0x84, 0xa4, 0x62, 0x6f, 0x64, 0x79) + o = append(o, 0x85, 0xa4, 0x62, 0x6f, 0x64, 0x79) o = msgp.AppendBytes(o, z.body) // string "ctype" o = append(o, 0xa5, 0x63, 0x74, 0x79, 0x70, 0x65) o = msgp.AppendBytes(o, z.ctype) + // string "cencoding" + o = append(o, 0xa9, 0x63, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67) + o = msgp.AppendBytes(o, z.cencoding) // string "status" o = append(o, 0xa6, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73) o = msgp.AppendInt(o, z.status) @@ -154,6 +173,12 @@ func (z *item) UnmarshalMsg(bts []byte) (o []byte, err error) { err = msgp.WrapError(err, "ctype") return } + case "cencoding": + z.cencoding, bts, err = msgp.ReadBytesBytes(bts, z.cencoding) + if err != nil { + err = msgp.WrapError(err, "cencoding") + return + } case "status": z.status, bts, err = msgp.ReadIntBytes(bts) if err != nil { @@ -180,6 +205,6 @@ func (z *item) UnmarshalMsg(bts []byte) (o []byte, err error) { // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message func (z *item) Msgsize() (s int) { - s = 1 + 5 + msgp.BytesPrefixSize + len(z.body) + 6 + msgp.BytesPrefixSize + len(z.ctype) + 7 + msgp.IntSize + 4 + msgp.Uint64Size + s = 1 + 5 + msgp.BytesPrefixSize + len(z.body) + 6 + msgp.BytesPrefixSize + len(z.ctype) + 10 + msgp.BytesPrefixSize + len(z.cencoding) + 7 + msgp.IntSize + 4 + msgp.Uint64Size return } diff --git a/middleware/session/README.md b/middleware/session/README.md index 525541f348..f56a40f71f 100644 --- a/middleware/session/README.md +++ b/middleware/session/README.md @@ -11,6 +11,7 @@ Session middleware for [Fiber](https://github.com/gofiber/fiber) ### Signatures ```go func New(config ...Config) *Store +func (s *Store) RegisterType(i interface{}) func (s *Store) Get(c *fiber.Ctx) (*Session, error) func (s *Store) Reset() error diff --git a/middleware/session/config.go b/middleware/session/config.go index f9d3a54483..1f31ca28b3 100644 --- a/middleware/session/config.go +++ b/middleware/session/config.go @@ -44,6 +44,11 @@ type Config struct { // KeyGenerator generates the session key. // Optional. Default value utils.UUIDv4 KeyGenerator func() string + + // CustomType allows you to store custom type/struct + // in any Storage provider. Only use this option if you + // are using custom a custom type/struct used as value. + CustomType interface{} } // ConfigDefault is the default config diff --git a/middleware/session/store.go b/middleware/session/store.go index 1f6c438ec1..d3260f5108 100644 --- a/middleware/session/store.go +++ b/middleware/session/store.go @@ -14,9 +14,6 @@ type Store struct { var mux sync.Mutex -// Storage ErrNotExist -var errNotExist = "key does not exist" - func New(config ...Config) *Store { // Set default config cfg := configDefault(config...) @@ -30,6 +27,13 @@ func New(config ...Config) *Store { } } +// RegisterType will allow you to encode/decode custom types +// into any Storage provider +func (s *Store) RegisterType(i interface{}) { + gotiny.Register(i) +} + +// Get will get/create a session func (s *Store) Get(c *fiber.Ctx) (*Session, error) { var fresh bool