mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 16:50:54 +00:00
skip cookie args when value is nil
This commit is contained in:
parent
d8b9db8d3e
commit
ee889e9975
@ -98,38 +98,56 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the settings below
|
||||||
|
// Path, Domain, Secure, HttpOnly
|
||||||
|
// can use nil skip set
|
||||||
|
|
||||||
|
// default "/"
|
||||||
if len(others) > 1 {
|
if len(others) > 1 {
|
||||||
if v, ok := others[1].(string); ok && len(v) > 0 {
|
if v, ok := others[1].(string); ok && len(v) > 0 {
|
||||||
fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v))
|
fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v))
|
||||||
} else {
|
|
||||||
fmt.Fprintf(&b, "; Path=%s", '/')
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&b, "; Path=%s", "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default empty
|
||||||
if len(others) > 2 {
|
if len(others) > 2 {
|
||||||
if v, ok := others[2].(string); ok && len(v) > 0 {
|
if v, ok := others[2].(string); ok && len(v) > 0 {
|
||||||
fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v))
|
fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default empty
|
||||||
if len(others) > 3 {
|
if len(others) > 3 {
|
||||||
var secure bool
|
var secure bool
|
||||||
switch v := others[3].(type) {
|
switch v := others[3].(type) {
|
||||||
case bool:
|
case bool:
|
||||||
secure = v
|
secure = v
|
||||||
default:
|
default:
|
||||||
secure = true
|
if others[3] != nil {
|
||||||
|
secure = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if secure {
|
if secure {
|
||||||
fmt.Fprintf(&b, "; Secure")
|
fmt.Fprintf(&b, "; Secure")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default true
|
||||||
|
httponly := true
|
||||||
if len(others) > 4 {
|
if len(others) > 4 {
|
||||||
if v, ok := others[4].(bool); ok && !v {
|
if v, ok := others[4].(bool); ok && !v || others[4] == nil {
|
||||||
// HttpOnly = false
|
// HttpOnly = false
|
||||||
} else {
|
httponly = false
|
||||||
fmt.Fprintf(&b, "; HttpOnly")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if httponly {
|
||||||
|
fmt.Fprintf(&b, "; HttpOnly")
|
||||||
|
}
|
||||||
|
|
||||||
output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String())
|
output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user