aboutsummaryrefslogtreecommitdiff
path: root/cookie.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-03-29 16:12:59 +0300
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-03-29 16:12:59 +0300
commit1498d8174e70a1b9a90faffbdaa529869633b3c1 (patch)
treef39bfa3da54b795483db2ae1cf28aafb831477b2 /cookie.go
parentserver: sleep for a while after reaching the concurrency limit, so other conc... (diff)
downloadfasthttp-1498d8174e70a1b9a90faffbdaa529869633b3c1.tar.gz
fasthttp-1498d8174e70a1b9a90faffbdaa529869633b3c1.tar.bz2
fasthttp-1498d8174e70a1b9a90faffbdaa529869633b3c1.zip
Issue #73: added 'HttpOnly' and 'secure' flags support to Cookie
Diffstat (limited to 'cookie.go')
-rw-r--r--cookie.go57
1 files changed, 50 insertions, 7 deletions
diff --git a/cookie.go b/cookie.go
index 90f481d..2ff8a4c 100644
--- a/cookie.go
+++ b/cookie.go
@@ -55,6 +55,9 @@ type Cookie struct {
domain []byte
path []byte
+ httpOnly bool
+ secure bool
+
bufKV argsKV
buf []byte
}
@@ -67,6 +70,28 @@ func (c *Cookie) CopyTo(src *Cookie) {
c.expire = src.expire
c.domain = append(c.domain[:0], src.domain...)
c.path = append(c.path[:0], src.path...)
+ c.httpOnly = src.httpOnly
+ c.secure = src.secure
+}
+
+// HTTPOnly returns true if the cookie is http only.
+func (c *Cookie) HTTPOnly() bool {
+ return c.httpOnly
+}
+
+// SetHTTPOnly sets cookie's httpOnly flag to the given value.
+func (c *Cookie) SetHTTPOnly(httpOnly bool) {
+ c.httpOnly = httpOnly
+}
+
+// Secure returns true if the cookie is secure.
+func (c *Cookie) Secure() bool {
+ return c.secure
+}
+
+// SetSecure sets cookie's secure flag to the given value.
+func (c *Cookie) SetSecure(secure bool) {
+ c.secure = secure
}
// Path returns cookie path.
@@ -165,6 +190,8 @@ func (c *Cookie) Reset() {
c.expire = zeroTime
c.domain = c.domain[:0]
c.path = c.path[:0]
+ c.httpOnly = false
+ c.secure = false
}
// AppendBytes appends cookie representation to dst and returns
@@ -189,6 +216,14 @@ func (c *Cookie) AppendBytes(dst []byte) []byte {
if len(c.path) > 0 {
dst = appendCookiePart(dst, strCookiePath, c.path)
}
+ if c.httpOnly {
+ dst = append(dst, ';', ' ')
+ dst = append(dst, strCookieHTTPOnly...)
+ }
+ if c.secure {
+ dst = append(dst, ';', ' ')
+ dst = append(dst, strCookieSecure...)
+ }
return dst
}
@@ -252,6 +287,13 @@ func (c *Cookie) ParseBytes(src []byte) error {
c.domain = append(c.domain[:0], kv.value...)
case "path":
c.path = append(c.path[:0], kv.value...)
+ case "":
+ switch string(kv.value) {
+ case "HttpOnly":
+ c.httpOnly = true
+ case "secure":
+ c.secure = true
+ }
}
}
return nil
@@ -305,26 +347,27 @@ type cookieScanner struct {
}
func (s *cookieScanner) next(kv *argsKV, decode bool) bool {
- if len(s.b) == 0 {
+ b := s.b
+ if len(b) == 0 {
return false
}
isKey := true
k := 0
- for i, c := range s.b {
+ for i, c := range b {
switch c {
case '=':
if isKey {
isKey = false
- kv.key = decodeCookieArg(kv.key, s.b[:i], decode)
+ kv.key = decodeCookieArg(kv.key, b[:i], decode)
k = i + 1
}
case ';':
if isKey {
kv.key = kv.key[:0]
}
- kv.value = decodeCookieArg(kv.value, s.b[k:i], decode)
- s.b = s.b[i+1:]
+ kv.value = decodeCookieArg(kv.value, b[k:i], decode)
+ s.b = b[i+1:]
return true
}
}
@@ -332,8 +375,8 @@ func (s *cookieScanner) next(kv *argsKV, decode bool) bool {
if isKey {
kv.key = kv.key[:0]
}
- kv.value = decodeCookieArg(kv.value, s.b[k:], decode)
- s.b = s.b[len(s.b):]
+ kv.value = decodeCookieArg(kv.value, b[k:], decode)
+ s.b = b[len(b):]
return true
}