9dc93cba by slene

skip cookie args when value is nil

1 parent 7f5fb871
...@@ -98,38 +98,56 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface ...@@ -98,38 +98,56 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface
98 } 98 }
99 } 99 }
100 } 100 }
101
102 // the settings below
103 // Path, Domain, Secure, HttpOnly
104 // can use nil skip set
105
106 // default "/"
101 if len(others) > 1 { 107 if len(others) > 1 {
102 if v, ok := others[1].(string); ok && len(v) > 0 { 108 if v, ok := others[1].(string); ok && len(v) > 0 {
103 fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v)) 109 fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v))
104 } else {
105 fmt.Fprintf(&b, "; Path=%s", '/')
106 } 110 }
111 } else {
112 fmt.Fprintf(&b, "; Path=%s", "/")
107 } 113 }
114
115 // default empty
108 if len(others) > 2 { 116 if len(others) > 2 {
109 if v, ok := others[2].(string); ok && len(v) > 0 { 117 if v, ok := others[2].(string); ok && len(v) > 0 {
110 fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v)) 118 fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v))
111 } 119 }
112 } 120 }
121
122 // default empty
113 if len(others) > 3 { 123 if len(others) > 3 {
114 var secure bool 124 var secure bool
115 switch v := others[3].(type) { 125 switch v := others[3].(type) {
116 case bool: 126 case bool:
117 secure = v 127 secure = v
118 default: 128 default:
119 secure = true 129 if others[3] != nil {
130 secure = true
131 }
120 } 132 }
121 if secure { 133 if secure {
122 fmt.Fprintf(&b, "; Secure") 134 fmt.Fprintf(&b, "; Secure")
123 } 135 }
124 } 136 }
137
138 // default true
139 httponly := true
125 if len(others) > 4 { 140 if len(others) > 4 {
126 if v, ok := others[4].(bool); ok && !v { 141 if v, ok := others[4].(bool); ok && !v || others[4] == nil {
127 // HttpOnly = false 142 // HttpOnly = false
128 } else { 143 httponly = false
129 fmt.Fprintf(&b, "; HttpOnly")
130 } 144 }
131 } 145 }
132 146
147 if httponly {
148 fmt.Fprintf(&b, "; HttpOnly")
149 }
150
133 output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String()) 151 output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String())
134 } 152 }
135 153
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!