Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookiejar implementation. #526

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ type Client struct {
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// CookieJar stores cookies allowing user to handle cookies easily.
//
// If CookieJar is nil no cookie will be collected.
CookieJar *CookieJar

// Callback for establishing new connections to hosts.
//
// Default Dial is used if not set.
Expand Down Expand Up @@ -410,6 +415,7 @@ func (c *Client) Do(req *Request, resp *Response) error {
Name: c.Name,
NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader,
Dial: c.Dial,
CookieJar: c.CookieJar,
DialDualStack: c.DialDualStack,
IsTLS: isTLS,
TLSConfig: c.TLSConfig,
Expand Down Expand Up @@ -525,6 +531,10 @@ type HostClient struct {
// Default Dial is used if not set.
Dial DialFunc

// CookieJar stores cookies. If CookieJar is nil
// no cookie will be collected.
CookieJar *CookieJar

// Attempt to connect to both ipv4 and ipv6 host addresses
// if set to true.
//
Expand Down Expand Up @@ -1101,8 +1111,16 @@ func (c *HostClient) do(req *Request, resp *Response) (bool, error) {
resp = AcquireResponse()
}

if c.CookieJar != nil {
c.CookieJar.dumpTo(req)
}

ok, err := c.doNonNilReqResp(req, resp)

if c.CookieJar != nil {
c.CookieJar.getFrom(req.Host(), req.URI().Path(), resp)
}

if nilResp {
ReleaseResponse(resp)
}
Expand Down
63 changes: 63 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,69 @@ func TestClientPostArgs(t *testing.T) {
}
}

func TestClientCookieJar(t *testing.T) {
ln := fasthttputil.NewInmemoryListener()
key, value := "cometo", "thefasthttpcon"
key2, value2 := "hello", "world"
key3, value3 := "coo", "kie"
s := &Server{
Handler: func(ctx *RequestCtx) {
cookie := AcquireCookie()
cookie.SetKey(key)
cookie.SetValue(value)
ctx.Response.Header.SetCookie(cookie)

cookie2 := AcquireCookie()
cookie2.SetKey(key2)
cookie2.SetValue(value2)
cookie2.SetExpire(CookieExpireDelete)
ctx.Response.Header.SetCookie(cookie2)

cookie3:= AcquireCookie()
cookie3.SetKey(key3)
cookie3.SetValue(value3)
cookie3.SetPath("/hello/")
ctx.Response.Header.SetCookie(cookie3)
},
}
go s.Serve(ln)
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
},
CookieJar: &CookieJar{},
}
req := AcquireRequest()
res := AcquireResponse()
req.SetRequestURI("http://fasthttp.con/hello/world")
err := c.Do(req, res)
if err != nil {
t.Fatal(err)
}

uri := AcquireURI()
uri.SetHost("fasthttp.con")

cs := c.CookieJar.Get(uri)
if len(cs) != 2 {
t.Fatalf("Unexpected len: %d. Expected: %d", len(cs), 2)
}

if k := string(cs[0].Key()); k != key {
t.Fatalf("Unexpected key: %s <> %s", k, key)
}
if v := string(cs[0].Value()); v != value {
t.Fatalf("Unexpected value: %s <> %s", v, value)
}

if k := string(cs[1].Key()); k != key3 {
t.Fatalf("Unexpected key: %s <> %s", k, key3)
}
if v := string(cs[1].Value()); v != value3 {
t.Fatalf("Unexpected value: %s <> %s", v, value3)
}
}

func TestClientRedirectSameSchema(t *testing.T) {

listenHTTPS1 := testClientRedirectListener(t, true)
Expand Down
1 change: 1 addition & 0 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
// CookieSameSite is an enum for the mode in which the SameSite flag should be set for the given cookie.
// See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 for details.
type CookieSameSite int

const (
// CookieSameSiteDisabled removes the SameSite flag
CookieSameSiteDisabled CookieSameSite = iota
Expand Down
204 changes: 204 additions & 0 deletions cookiejar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package fasthttp

import (
"bytes"
"net"
"sync"
"time"
)

// CookieJar manages cookie storage
type CookieJar struct {
m sync.Mutex
hostCookies map[string][]*Cookie
dgrr marked this conversation as resolved.
Show resolved Hide resolved
}

// Get returns the cookies stored from a specific domain.
//
// If there were no cookies related with host returned slice will be nil.
//
// CookieJar keeps a copy of the cookies, so the returned cookies can be released safely.
func (cj *CookieJar) Get(uri *URI) (cookies []*Cookie) {
if uri != nil {
cookies = cj.get(uri.Host(), uri.Path())
}
return
}

func (cj *CookieJar) get(host, path []byte) (rcs []*Cookie) {
if cj.hostCookies == nil {
return
}

var (
err error
cookies []*Cookie
hostStr = b2s(host)
)
// port must not be included.
hostStr, _, err = net.SplitHostPort(hostStr)
if err != nil {
hostStr = b2s(host)
}
// get cookies deleting expired ones
cookies = cj.getCookies(hostStr)

rcs = make([]*Cookie, 0, len(cookies))
for i := 0; i < len(cookies); i++ {
cookie := cookies[i]
if len(path) > 1 && len(cookie.path) > 1 && !bytes.HasPrefix(cookie.Path(), path) {
continue
}
rcs = append(rcs, cookie)
}

return
}

// getCookies returns a cookie slice releasing expired cookies
func (cj *CookieJar) getCookies(hostStr string) (cookies []*Cookie) {
cj.m.Lock()
defer cj.m.Unlock()

cookies = cj.hostCookies[hostStr]
var (
t = time.Now()
n = len(cookies)
)
for i := 0; i < len(cookies); i++ {
c := cookies[i]
if !c.Expire().Equal(CookieExpireUnlimited) && c.Expire().Before(t) { // cookie expired
cookies = append(cookies[:i], cookies[i+1:]...)
ReleaseCookie(c)
i--
}
}
// has any cookie been deleted?
if n > len(cookies) {
cj.hostCookies[hostStr] = cookies
}
return
}

// Set sets cookies for a specific host.
//
// The host is get from uri.Host().
//
// If the cookie key already exists it will be replaced by the new cookie value.
dgrr marked this conversation as resolved.
Show resolved Hide resolved
//
// CookieJar keeps a copy of the cookies, so the parsed cookies can be released safely.
func (cj *CookieJar) Set(uri *URI, cookies ...*Cookie) {
if uri != nil {
cj.set(uri.Host(), cookies...)
}
}

// SetByHost sets cookies for a specific host.
//
// If the cookie key already exists it will be replaced by the new cookie value.
dgrr marked this conversation as resolved.
Show resolved Hide resolved
//
// CookieJar keeps a copy of the cookies, so the parsed cookies can be released safely.
func (cj *CookieJar) SetByHost(host []byte, cookies ...*Cookie) {
cj.set(host, cookies...)
}

func (cj *CookieJar) set(host []byte, cookies ...*Cookie) {
hostStr := b2s(host)

cj.m.Lock()
defer cj.m.Unlock()

if cj.hostCookies == nil {
cj.hostCookies = make(map[string][]*Cookie)
}
hcs, ok := cj.hostCookies[hostStr]
if !ok {
// If the key does not exists in the map then
// we must make a copy for the key.
hostStr = string(host)
}
for _, cookie := range cookies {
c := searchCookieByKeyAndPath(cookie.Key(), cookie.Path(), hcs)
if c == nil {
c = AcquireCookie()
hcs = append(hcs, c)
}
c.CopyTo(cookie)
}
cj.hostCookies[hostStr] = hcs
}

// SetKeyValue sets a cookie by key and value for a specific host.
//
// This function prevents extra allocations by making repeated cookies
// not being duplicated.
func (cj *CookieJar) SetKeyValue(host, key, value string) {
cj.SetKeyValueBytes(host, s2b(key), s2b(value))
}

// SetKeyValueBytes sets a cookie by key and value for a specific host.
//
// This function prevents extra allocations by making repeated cookies
// not being duplicated.
dgrr marked this conversation as resolved.
Show resolved Hide resolved
func (cj *CookieJar) SetKeyValueBytes(host string, key, value []byte) {
cj.setKeyValue(host, key, value)
}

func (cj *CookieJar) setKeyValue(host string, key, value []byte) {
c := AcquireCookie()
c.SetKeyBytes(key)
c.SetValueBytes(value)
cj.set(s2b(host), c)
}

func (cj *CookieJar) dumpTo(req *Request) {
uri := req.URI()
cookies := cj.get(uri.Host(), uri.Path())
for _, cookie := range cookies {
req.Header.SetCookieBytesKV(cookie.Key(), cookie.Value())
}
}

func (cj *CookieJar) getFrom(host, path []byte, res *Response) {
hostStr := b2s(host)

cj.m.Lock()
defer cj.m.Unlock()

if cj.hostCookies == nil {
cj.hostCookies = make(map[string][]*Cookie)
}
cookies, ok := cj.hostCookies[hostStr]
dgrr marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
// If the key does not exists in the map then
// we must make a copy for the key.
hostStr = string(host)
}
t := time.Now()
res.Header.VisitAllCookie(func(key, value []byte) {
created := false
c := searchCookieByKeyAndPath(key, path, cookies)
if c == nil {
c, created = AcquireCookie(), true
}
c.ParseBytes(value)
dgrr marked this conversation as resolved.
Show resolved Hide resolved
if c.Expire().Equal(CookieExpireUnlimited) || c.Expire().After(t) {
cookies = append(cookies, c)
} else if created {
ReleaseCookie(c)
}
})
cj.hostCookies[hostStr] = cookies
}

func searchCookieByKeyAndPath(key, path []byte, cookies []*Cookie) (cookie *Cookie) {
for _, c := range cookies {
if bytes.Equal(key, c.Key()) {
if len(path) <= 1 || bytes.HasPrefix(c.Path(), path) {
cookie = c
break
dgrr marked this conversation as resolved.
Show resolved Hide resolved
}
dgrr marked this conversation as resolved.
Show resolved Hide resolved
}
}
return
}
Loading