-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http/cookiejar: add way to access all cookies in CookieJar #17587
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
Comments
What will be a good way to implement such a function? I thought about a generator-style function that returns a channel to a custom type containing the key, subkey and the entry, so you could call something like: I'd gladly do it, if you guys think my solution is good. By the way, what is an actual use case for this function? |
The use case in general (from looking around on the web and even a couple github projects) is to persist the cookiejar for later reuse. Some people (like me) want to restart their services while keeping state. I am currently manually keeping up a second copy of everything in the cookiejar (by checking the http client response each time), but since cookiejar is already doing this it is a waste of resources. The same-origin policy was added because of the need for sandboxing Javsacript in the browser and requests to different, third-party servers. Unknown/unauthorized code isn't really a problem in most Go applications. |
Is an opposite function, that accepts all the cookies and puts them in the jar also needed? |
I'd be happy to give this a shot. |
While nice to have, a function to set all the cookies back on the cookie jar isn't required since you can just The main thing is simply getting a JSON/map/struct dump of the cookies inside the cookiejar so it can be persisted. |
I would like this in the stdlib, too. For now, since CookieJar is an interface, I just wrapped it and keep track of stuff.
|
The function should return an array instead of a channel. Channels are far to heavy of an iterable, and I cannot see any need for a channel specifically. |
@dprime A couple questions on that proposed ExportableCookieJar:
From what I can tell rummaging around the code, it'd be ideal if there were a way to serialize the type entries map[string]map[string]entry
// entry is the internal representation of a cookie.
//
// This struct type is not used outside of this package per se, but the exported
// fields are those of RFC 6265.
type entry struct {
Name string
Value string
Domain string
Path string
Secure bool
HttpOnly bool
Persistent bool
HostOnly bool
Expires time.Time
Creation time.Time
LastAccess time.Time
// seqNum is a sequence number so that Cookies returns cookies in a
// deterministic order, even for cookies that have equal Path length and
// equal Creation time. This simplifies testing.
seqNum uint64
} As this data-structure is what preserves and provides the proper lookup of Cookies associated with a URL request in an efficient manner. In that case maybe the |
This stuff might be a bit more tricky than one would expect. The issue is named "add way to access all cookies in CookieJar" but accessing the content is only the first half of what a new API would need to provide: After accessing all stored cookies one may serialise them to disk and probably wants to recreate a Jar from such a serialisation. So we not only need a way to access all cookies (including all attributes) but also a way to refill the Jar. This cannot be done through So we need at least the following API:
This allows accessing all stored cookies and re-populating A Jar from that information. Deleting cookies from the jar can be simulated by specially crafted Jar.SetCookies calls. This is sufficient for the following use case:
During the original discussion about a persistent cookiejar (starting points for more might be https://groups.google.com/forum/#!topic/golang-dev/7Jcbgrbah2s/discussion) we found out that such a minimal API might not be suitable: Think about a browser in a mobile device which wants to persist cookies while not generating garbage (to keep GC low) and minimizing disk access. In such a use case reading
This starts to become a pretty large API change. I think it will be hard to find the right tradeoff between an API which is convenient to use in the simple use case (dump everything at program exit, reload all at next startup) while enabling to partially act on changes to the Jar content.
The reason for the coarse notification and subset is as follows:
Question to @bradfitz and @nigeltao : Are such large API changes okay? Should I prepare a design document? Or is the whole issue something which can be delegated to a 3rd party package? |
Large API changes are OK, but it is indeed tricky, as we've discussed before, and unfortunately, I don't have a lot of spare bandwidth to think about cookie jars at the moment. I think the way to start is to work in a 3rd party package. |
Does this really need to reside in the stdlib? For example, implementing a cookie jar that immediately persists changes in a database is easiest if you know the database you're talking to. You might even use a per-use wrapper that communicates an open SQL transaction to the thing implementing http.CookieJar. I think instead of wanting "100% all of the features cookiejar implementation" in stdlib, might it not be more useful to focus on this: Why is implementing your own CookieJar difficult? Is it because marshaling a http.Cookie usefully is hard? Is it because of innate complexity of HTTP cookie behavior? Can stdlib provide helpers for these tasks, without having to find a one-size-fits-everyone CookieJar implementation? |
This. But I agree: There is not much need for this in the stdlib. Taking the stdlib Jar implementation and adding a few functions/methods is not much work. net/http/cookiejar does not see much commits, actually just a handful during the last 5 years. So forking and maintaining the fork is pretty simple. Adding helper functions to the stdlib is also complicated: http.Cookie is not really suitable to store all information needed in a cookiejar, e.g. how to distinguish domain-cookies from host-cookies? |
Because: golang/go#17587
Because: golang/go#17587
I fully support the idea, but is there any feedback from contributors? |
For me, only the same feedback that I gave in 2017. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.6.3
What operating system and processor architecture are you using (
go env
)?darwin / amd64
What did you do?
Tried to access all cookies stored in a http.CookieJar.
What did you expect to see?
A Function for accessing the
[]*http.Cookie
data. The cookiejar struct is missing a way to access all the cookie structs saved in it. You have to know all the matching urls (as url.URL structs) in order to pull cookies out of it.What did you see instead?
Only a method for reading cookies from pre-known url.URL structs.
The text was updated successfully, but these errors were encountered: