-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
net/http: add Request.CookiesNamed #61472
Comments
ReadCookies()
method for http.Request
ReadCookies(name)
method for http.Request
This commit implements the new API proposed with golang#61472 This change set implements the ReadCookies function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Closes golang#61472
This commit implements the new API proposed with golang#61472 This change set implements the ReadCookies function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Closes golang#61472
If we add this, perhaps call it This doesn't seem unreasonable; it's currently possible to get this information by using |
This commit implements the new API proposed with golang#61472 This change set implements the CookiesNamed function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Fixes golang#61472
ReadCookies(name)
method for http.Request
Sounds good. I've renamed it in the proposal and in #61473 |
We recently wrote a routine to remove a cookie by name zalando/skipper#2410 func removeCookie(request *http.Request, name string) bool {
cookies := request.Cookies()
hasCookie := false
for _, c := range cookies {
if c.Name == name {
hasCookie = true
break
}
}
if hasCookie {
request.Header.Del("Cookie")
for _, c := range cookies {
if c.Name != name {
request.AddCookie(c)
}
}
}
return hasCookie
} This would be a bit simpler if we could get all cookies except cookie we'd like to remove (OTOH we would like to preserve hasCookie check to avoid touching headers if there is no match). With that in mind and considering that api for trivial case to get single cookie is there I am wondering if it would make sense to support predicate instead of exact match: func (r *Request) CookiesByName(filter func(string) bool) []*Cookie though it is not a common pattern, I could only find https://pkg.go.dev/reflect#Value.FieldByNameFunc and https://pkg.go.dev/go/ast#Filter |
@AlexanderYastrebov I totally see your point. However, I'm wondering where to draw the line between what the library should provide in terms of filter functionality and what it shouldn't. For example, if a function like the one you provided would exist, why isn't there one where I could filter for the cookie value? (EDIT: okay, maybe because all cookies would need to be parsed anyways for that and you could just use I think that the proposed Actually, IMHO, the existing |
The A proliferation of cookie-fetching-and-filtering methods doesn't seem like a good idea. As @timofurrer says, where do we draw the line? I think the argument in favor of |
Another thought: should we wait to see if the iterator proposal lands and add a cookie iterator instead? |
@carlmjohnson I generally like the idea. However, I still think that the proposed IMHO, the cookie iterator could be another proposal to solve problems like the one @AlexanderYastrebov described in #61472 (comment). WDYT? |
@neild has there been any progress on the proposal status? Is there anything I can assist with to get this over the line? |
Change https://go.dev/cl/511516 mentions this issue: |
This commit implements the new API proposed with golang#61472 This change set implements the CookiesNamed function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Fixes golang#61472
Since this one missed Go 1.12 I've retargeted the PR to Go 1.22. Any chance of getting this in? |
This commit implements the new API proposed with golang#61472 This change set implements the CookiesNamed function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Fixes golang#61472
Based on the discussion above, this proposal seems like a likely accept. In net/http, add a new Request method CookiesNamed, so that the full set of cookie methods is:
|
No change in consensus, so accepted. 🎉 In net/http, add a new Request method CookiesNamed, so that the full set of cookie methods is:
|
This commit implements the new API proposed with golang#61472 This change set implements the CookiesNamed function on the net/http Request so that it's conveniently possible to retrieve all cookies from the Cookie header that match a given name. It does so by just wrapping the already existing readCookies function. Fixes golang#61472
@timofurrer thanks for the patience and for the hard work! I've just added some comments to your change and after addressing I shall review again and then if gucci, I shall approve; we are almost there and thank you! |
This commit implements the new API proposed with golang#61472 Iimplements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes golang#61472
This commit implements the new API proposed with golang#61472 Iimplements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes golang#61472
This commit implements the new API proposed with golang#61472 Implements a new method http.Request.CookiesName, that allows retrieving all cookies that match the given name. Fixes golang#61472
Reverted in CL https://go-review.googlesource.com/c/go/+/571277. @timofurrer once the revert lands, please prepare your CL again rebased from the latest master. |
Change https://go.dev/cl/571278 mentions this issue: |
CL 511516 added the method but didn't include a release note for it because it was authored and tested before the new release note flow. For #61472. Change-Id: I38f73e97093a2badaea658ed430e174b73e35b3a Reviewed-on: https://go-review.googlesource.com/c/go/+/571278 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Michael Knyszek <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]>
I'd like to propose to expose (by wrapping) the
readCookies
method onhttp.Request
asCookiesNamed
that takes a single argument which is thefilter
for the cookie name.The function signature would be:
... and the implementation would be as simple as something like this:
Problem statement
Modern browsers may send multiple cookies with the same name if they are present in the cookie storage and are allowed to send. See RFC 6265.
However, there is no convenient way so far to get a slice of Cookies that matches a specific name.
Today, there is only
r.Cookies()
to get all cookies from theCookie
header andr.Cookie(name)
to get the "first" cookie from theCookie
header that matches the givenname
.The proposed
r.CookiesNamed(name)
would solve that by providing a simple way to just read all cookies with a given name.Proposed Implementation
I have a draft PR ready here, which I'd like to contribute for this proposal: #61473
Edits
ReadCookies
toCookiesNamed
.The text was updated successfully, but these errors were encountered: