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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Applied review changes. Deleted init and copyCookies functions.
  • Loading branch information
dgrr committed Feb 18, 2019
commit 9fad3e054768e74e12732d7b7bb310b96603d1a8
96 changes: 40 additions & 56 deletions cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,11 @@ type CookieJar struct {
hostCookies map[string][]*Cookie
dgrr marked this conversation as resolved.
Show resolved Hide resolved
}

func (cj *CookieJar) init() {
cj.m.Lock()
{
if cj.hostCookies == nil {
cj.hostCookies = make(map[string][]*Cookie)
}
}
cj.m.Unlock()
}

func copyCookies(cookies []*Cookie) (cs []*Cookie) {
// TODO: Try to delete the allocations
cs = make([]*Cookie, 0, len(cookies))
for _, cookie := range cookies {
c := AcquireCookie()
c.CopyTo(cookie)
cs = append(cs, c)
}
return
}

// Get returns the cookies stored from a specific domain.
//
// If there were no cookies related with host returned slice will be nil.
//
// returned cookies can be released safely.
// 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())
Expand All @@ -47,7 +26,9 @@ func (cj *CookieJar) Get(uri *URI) (cookies []*Cookie) {
}

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

var (
err error
Expand All @@ -59,12 +40,8 @@ func (cj *CookieJar) get(host, path []byte) (rcs []*Cookie) {
if err != nil {
hostStr = b2s(host)
}
cj.m.Lock()
{
// get cookies deleting expired ones
cookies = cj.getCookies(hostStr)
}
cj.m.Unlock()
// get cookies deleting expired ones
cookies = cj.getCookies(hostStr)

rcs = make([]*Cookie, 0, len(cookies))
for i := 0; i < len(cookies); i++ {
Expand All @@ -79,32 +56,38 @@ func (cj *CookieJar) get(host, path []byte) (rcs []*Cookie) {
}

// getCookies returns a cookie slice releasing expired cookies
func (cj *CookieJar) getCookies(hostStr string) []*Cookie {
var (
func (cj *CookieJar) getCookies(hostStr string) (cookies []*Cookie) {
cj.m.Lock()
{
cookies = cj.hostCookies[hostStr]
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--
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
}
}
// has any cookie been deleted?
if n > len(cookies) {
cj.hostCookies[hostStr] = cookies
}
return cookies
cj.m.Unlock()
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...)
Expand All @@ -114,15 +97,18 @@ func (cj *CookieJar) Set(uri *URI, cookies ...*Cookie) {
// 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) {
cj.init()

cj.m.Lock()
{
if cj.hostCookies == nil {
cj.hostCookies = make(map[string][]*Cookie)
dgrr marked this conversation as resolved.
Show resolved Hide resolved
}
hostStr := b2s(host)
hcs, ok := cj.hostCookies[hostStr]
if !ok {
Expand All @@ -132,13 +118,11 @@ func (cj *CookieJar) set(host []byte, cookies ...*Cookie) {
}
for _, cookie := range cookies {
c := searchCookieByKeyAndPath(cookie.Key(), cookie.Path(), hcs)
if c != nil {
c.ParseBytes(cookie.Cookie())
} else {
if c == nil {
c = AcquireCookie()
c.CopyTo(cookie)
hcs = append(hcs, c)
}
c.CopyTo(cookie)
}
cj.hostCookies[hostStr] = hcs
}
Expand Down Expand Up @@ -177,11 +161,12 @@ func (cj *CookieJar) dumpTo(req *Request) {
}

func (cj *CookieJar) getFrom(host, path []byte, res *Response) {
cj.init()

hostStr := b2s(host)
cj.m.Lock()
{
if cj.hostCookies == nil {
cj.hostCookies = make(map[string][]*Cookie)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can just do return. No need to allocate anything.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make sure you do defer cj.m.Unlock() instead of doing this at the end of the function.

I myself always prefer to defer Unlocks as its much easier to follow the code. Also defer is basically free these days so no worry about performance.

}
cookies, ok := cj.hostCookies[hostStr]
if !ok {
// If the key does not exists in the map then
Expand All @@ -193,11 +178,10 @@ func (cj *CookieJar) getFrom(host, path []byte, res *Response) {
created := false
c := searchCookieByKeyAndPath(key, path, cookies)
if c == nil {
c = AcquireCookie()
created = true
c, created = AcquireCookie(), true
}
c.ParseBytes(value)
if c.Expire().IsZero() || c.Expire().After(t) { // cookie expired
if c.Expire().Equal(CookieExpireUnlimited) || c.Expire().After(t) {
cookies = append(cookies, c)
} else if created {
ReleaseCookie(c)
Expand Down