-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add HTTP::Cookie#destroy
#14819
base: master
Are you sure you want to change the base?
Add HTTP::Cookie#destroy
#14819
Conversation
Since the intent is to expire the cookie, then I'd also put an expiration date far further than 5 minutes ago (clocks can get unsynced) and use a fixed date, like the UNIX timestamp (1970-01-01 00:00:00 UTC). |
@ysbaddaden Hey julian! This little function is a convenience for destroying cookies (which usually means setting their expiry to some date in the past and setting their value to blank) I just thought others might want a convenient function to destroy cookies. Although as you pointed out, it is superflous. I just PR'd it to promote developer happiness :3 |
It would be much appreciated to follow the guidelines for feature discussions and open an issue for a new proposal first, to keep the general discussion independent of a PR with a concrete implementation. |
Actually, setting So I think the functionality should be implemented as |
Sorry about the typo. I can't run the test suite locally so I'm relying on the CI. |
src/http/cookie.cr
Outdated
# tell the browser to delete the cookie. | ||
# Set its value to an empty string | ||
# and set its expiration time to the past | ||
# Browsers delete cookies that have expired. | ||
def destroy | ||
self.value = "" | ||
self.expires = Time::UNIX_EPOCH | ||
self.max_age = Time::Span.zero | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# tell the browser to delete the cookie. | |
# Set its value to an empty string | |
# and set its expiration time to the past | |
# Browsers delete cookies that have expired. | |
def destroy | |
self.value = "" | |
self.expires = Time::UNIX_EPOCH | |
self.max_age = Time::Span.zero | |
end | |
# Tells the browser to expire the cookie by setting its | |
# max age time to zero and its expiration time to the past. | |
def expire | |
self.expires = Time::UNIX_EPOCH | |
self.max_age = Time::Span.zero | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on why you suggest that we remove setting the value to a blank string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, my reasoning was twofold:
expire
method name IMO implies only cookie expiration, not emptying its value- Spec doesn't mention (or I've not seen it does) clearing out the value as the requirement for a cookie expiration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe destroy will better represent what the function does. And there are many methods with a name similar to #expire
I think calling it #destroy
would be more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing the value is not a requirement for expiration. But I think it makes a lot of sense not to send the value you want gone to the client again.
HTTP::Cookie#destroy
IMO the name |
# Destroys the cookie. | ||
# This is done by causing the cookie to expire. Also clears its value. | ||
def destroy | ||
self.value = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still do think that's a bit unexpected to clear the value in here. Just sayin'
Reasoning
I tried to use
cookies.delete("...")
today and it didn't cause the cookie to expire from the browser. It sent it again on the next request.This method sets the cookie value to an empty string and seets the expiry date to 5 minutes ago. Browsers automatically delete expired cookies.
My aim is to provide a convenience to anyone who happens to need to use
HTTP::Cookie