update output.Cookie
Showing
1 changed file
with
35 additions
and
19 deletions
| ... | @@ -77,43 +77,59 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface | ... | @@ -77,43 +77,59 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface |
| 77 | var b bytes.Buffer | 77 | var b bytes.Buffer |
| 78 | fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) | 78 | fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) |
| 79 | if len(others) > 0 { | 79 | if len(others) > 0 { |
| 80 | switch others[0].(type) { | 80 | switch v := others[0].(type) { |
| 81 | case int: | 81 | case int: |
| 82 | if others[0].(int) > 0 { | 82 | if v > 0 { |
| 83 | fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int)) | 83 | fmt.Fprintf(&b, "; Max-Age=%d", v) |
| 84 | } else if others[0].(int) < 0 { | 84 | } else if v < 0 { |
| 85 | fmt.Fprintf(&b, "; Max-Age=0") | 85 | fmt.Fprintf(&b, "; Max-Age=0") |
| 86 | } | 86 | } |
| 87 | case int64: | 87 | case int64: |
| 88 | if others[0].(int64) > 0 { | 88 | if v > 0 { |
| 89 | fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int64)) | 89 | fmt.Fprintf(&b, "; Max-Age=%d", v) |
| 90 | } else if others[0].(int64) < 0 { | 90 | } else if v < 0 { |
| 91 | fmt.Fprintf(&b, "; Max-Age=0") | 91 | fmt.Fprintf(&b, "; Max-Age=0") |
| 92 | } | 92 | } |
| 93 | case int32: | 93 | case int32: |
| 94 | if others[0].(int32) > 0 { | 94 | if v > 0 { |
| 95 | fmt.Fprintf(&b, "; Max-Age=%d", others[0].(int32)) | 95 | fmt.Fprintf(&b, "; Max-Age=%d", v) |
| 96 | } else if others[0].(int32) < 0 { | 96 | } else if v < 0 { |
| 97 | fmt.Fprintf(&b, "; Max-Age=0") | 97 | fmt.Fprintf(&b, "; Max-Age=0") |
| 98 | } | 98 | } |
| 99 | } | 99 | } |
| 100 | } | 100 | } |
| 101 | if len(others) > 1 { | 101 | if len(others) > 1 { |
| 102 | if len(others[1].(string)) == 0 { | 102 | if v, ok := others[1].(string); ok && len(v) > 0 { |
| 103 | fmt.Fprintf(&b, "; Path=%s", '/') | 103 | fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v)) |
| 104 | } else { | 104 | } else { |
| 105 | fmt.Fprintf(&b, "; Path=%s", sanitizeValue(others[1].(string))) | 105 | fmt.Fprintf(&b, "; Path=%s", '/') |
| 106 | } | 106 | } |
| 107 | } | 107 | } |
| 108 | if len(others) > 2 && len(others[2].(string)) > 0 { | 108 | if len(others) > 2 { |
| 109 | fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(others[2].(string))) | 109 | if v, ok := others[2].(string); ok && len(v) > 0 { |
| 110 | fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v)) | ||
| 111 | } | ||
| 110 | } | 112 | } |
| 111 | if len(others) > 3 && others[3].(bool) { | 113 | if len(others) > 3 { |
| 112 | fmt.Fprintf(&b, "; Secure") | 114 | var secure bool |
| 115 | switch v := others[3].(type) { | ||
| 116 | case bool: | ||
| 117 | secure = v | ||
| 118 | default: | ||
| 119 | secure = true | ||
| 120 | } | ||
| 121 | if secure { | ||
| 122 | fmt.Fprintf(&b, "; Secure") | ||
| 123 | } | ||
| 113 | } | 124 | } |
| 114 | if !(len(others) > 4 && others[4].(bool) == false) { | 125 | if len(others) > 4 { |
| 115 | fmt.Fprintf(&b, "; HttpOnly") | 126 | if v, ok := others[4].(bool); ok && !v { |
| 127 | // HttpOnly = false | ||
| 128 | } else { | ||
| 129 | fmt.Fprintf(&b, "; HttpOnly") | ||
| 130 | } | ||
| 116 | } | 131 | } |
| 132 | |||
| 117 | output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String()) | 133 | output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String()) |
| 118 | } | 134 | } |
| 119 | 135 | ... | ... |
-
Please register or sign in to post a comment