github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/cookie.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package transports
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  )
    24  
    25  const (
    26  	CookieSameSiteDisabled CookieSameSite = iota
    27  	CookieSameSiteDefaultMode
    28  	CookieSameSiteLaxMode
    29  	CookieSameSiteStrictMode
    30  	CookieSameSiteNoneMode
    31  )
    32  
    33  type CookieSameSite int
    34  
    35  type nocopy struct{}
    36  
    37  func (*nocopy) Lock()   {}
    38  func (*nocopy) Unlock() {}
    39  
    40  type Cookie struct {
    41  	nocopy   nocopy
    42  	key      []byte
    43  	value    []byte
    44  	expire   time.Time
    45  	maxAge   int
    46  	domain   []byte
    47  	path     []byte
    48  	httpOnly bool
    49  	secure   bool
    50  	sameSite CookieSameSite
    51  }
    52  
    53  func (c *Cookie) HTTPOnly() bool {
    54  	return c.httpOnly
    55  }
    56  
    57  func (c *Cookie) SetHTTPOnly(httpOnly bool) {
    58  	c.httpOnly = httpOnly
    59  }
    60  
    61  func (c *Cookie) Secure() bool {
    62  	return c.secure
    63  }
    64  
    65  func (c *Cookie) SetSecure(secure bool) {
    66  	c.secure = secure
    67  }
    68  
    69  func (c *Cookie) SameSite() CookieSameSite {
    70  	return c.sameSite
    71  }
    72  
    73  func (c *Cookie) SetSameSite(mode CookieSameSite) {
    74  	c.sameSite = mode
    75  	if mode == CookieSameSiteNoneMode {
    76  		c.SetSecure(true)
    77  	}
    78  }
    79  
    80  func (c *Cookie) Path() []byte {
    81  	return c.path
    82  }
    83  
    84  func (c *Cookie) SetPath(path []byte) {
    85  	c.path = path
    86  }
    87  
    88  func (c *Cookie) Domain() []byte {
    89  	return c.domain
    90  }
    91  
    92  func (c *Cookie) SetDomain(domain []byte) {
    93  	c.domain = append(c.domain[:0], domain...)
    94  }
    95  
    96  func (c *Cookie) MaxAge() int {
    97  	return c.maxAge
    98  }
    99  
   100  func (c *Cookie) SetMaxAge(seconds int) {
   101  	c.maxAge = seconds
   102  }
   103  
   104  func (c *Cookie) Expire() time.Time {
   105  	return c.expire
   106  }
   107  
   108  func (c *Cookie) SetExpire(expire time.Time) {
   109  	c.expire = expire
   110  }
   111  
   112  func (c *Cookie) Value() []byte {
   113  	return c.value
   114  }
   115  
   116  func (c *Cookie) SetValue(value []byte) {
   117  	c.value = append(c.value[:0], value...)
   118  }
   119  
   120  func (c *Cookie) Key() []byte {
   121  	return c.key
   122  }
   123  
   124  func (c *Cookie) SetKey(key []byte) {
   125  	c.key = append(c.key[:0], key...)
   126  }
   127  
   128  func (c *Cookie) Reset() {
   129  	c.key = c.key[:0]
   130  	c.value = c.value[:0]
   131  	c.expire = time.Time{}
   132  	c.maxAge = 0
   133  	c.domain = c.domain[:0]
   134  	c.path = c.path[:0]
   135  	c.httpOnly = false
   136  	c.secure = false
   137  	c.sameSite = CookieSameSiteDisabled
   138  }
   139  
   140  var cookiePool = &sync.Pool{
   141  	New: func() interface{} {
   142  		return &Cookie{}
   143  	},
   144  }
   145  
   146  func AcquireCookie() *Cookie {
   147  	return cookiePool.Get().(*Cookie)
   148  }
   149  
   150  func ReleaseCookie(c *Cookie) {
   151  	c.Reset()
   152  	cookiePool.Put(c)
   153  }