mirror of https://github.com/gofiber/fiber.git
updated case to handle binary data
parent
71c75ae998
commit
0d06e7ea90
46
ctx.go
46
ctx.go
|
@ -22,6 +22,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/gofiber/utils/v2"
|
"github.com/gofiber/utils/v2"
|
||||||
"github.com/valyala/bytebufferpool"
|
"github.com/valyala/bytebufferpool"
|
||||||
|
@ -451,6 +452,11 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
|
||||||
// Make copies or use the Immutable setting to use the value outside the Handler.
|
// Make copies or use the Immutable setting to use the value outside the Handler.
|
||||||
func (c *DefaultCtx) Cookies(key string, defaultValue ...string) string {
|
func (c *DefaultCtx) Cookies(key string, defaultValue ...string) string {
|
||||||
value := c.app.getString(c.fasthttp.Request.Header.Cookie(key))
|
value := c.app.getString(c.fasthttp.Request.Header.Cookie(key))
|
||||||
|
// If the value looks like binary data, return it as-is
|
||||||
|
if len(value) > 0 && !utf8.ValidString(value) {
|
||||||
|
fmt.Println("Detected non-UTF8 cookie value, returning raw bytes")
|
||||||
|
return value
|
||||||
|
}
|
||||||
return defaultString(c.sanitizeCookieValue(value), defaultValue)
|
return defaultString(c.sanitizeCookieValue(value), defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -458,27 +464,27 @@ func (c *DefaultCtx) Cookies(key string, defaultValue ...string) string {
|
||||||
// It removes invalid characters from the cookie value, similar to how
|
// It removes invalid characters from the cookie value, similar to how
|
||||||
// Go's standard library handles cookie values.
|
// Go's standard library handles cookie values.
|
||||||
func (c *DefaultCtx) sanitizeCookieValue(v string) string {
|
func (c *DefaultCtx) sanitizeCookieValue(v string) string {
|
||||||
// First, check if all characters are valid.
|
// First, check if all characters are valid.
|
||||||
valid := true
|
valid := true
|
||||||
for i := 0; i < len(v); i++ {
|
for i := 0; i < len(v); i++ {
|
||||||
if !c.validCookieValueByte(v[i]) {
|
if !c.validCookieValueByte(v[i]) {
|
||||||
valid = false
|
valid = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If all characters are valid, return the original string.
|
// If all characters are valid, return the original string.
|
||||||
if valid {
|
if valid {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, build a sanitized string in a byte slice.
|
// Otherwise, build a sanitized string in a byte slice.
|
||||||
buf := make([]byte, 0, len(v))
|
buf := make([]byte, 0, len(v))
|
||||||
for i := 0; i < len(v); i++ {
|
for i := 0; i < len(v); i++ {
|
||||||
if c.validCookieValueByte(v[i]) {
|
if c.validCookieValueByte(v[i]) {
|
||||||
buf = append(buf, v[i])
|
buf = append(buf, v[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return string(buf)
|
return string(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validCookieValueByte reports whether b is a valid byte in a cookie value.
|
// validCookieValueByte reports whether b is a valid byte in a cookie value.
|
||||||
|
|
Loading…
Reference in New Issue