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

Add Cookie#setAttribute #399

Closed
wants to merge 4 commits into from
Closed

Add Cookie#setAttribute #399

wants to merge 4 commits into from

Conversation

BalusC
Copy link
Member

@BalusC BalusC commented Apr 26, 2021

For discussion, see #175 and #271

While at it I also took opportunity to:

  • cleanup isToken helper method whose comment and methodname were completely out of sync with actual implementation
  • fix "rfc2019" typo in comments to "rfc2109"
  • add missing check for "HttpOnly" in cookie name
  • removed outdated javadoc wrt version 1 being "experimental"

@BalusC
Copy link
Member Author

BalusC commented Apr 26, 2021

Some thoughts:

  • perhaps add removeAttribute() (this is likely to lead to confusing expected behavior, much better is to just not set attribute in frist place)
  • perhaps detail in javadoc how exactly empty strings and nulls should be interpreted (natural expectation: empty string represents a boolean attribute such as Secure and null indicates that any predefined attribute should not be set)
  • perhaps rename setAttribute() / getAttributes() to overrideAttribute() / getOverriddenAttributes() as that more clearly describes intended behavior

@joakime
Copy link

joakime commented Apr 26, 2021

Since you are touching the RFC / version details in the javadoc, do we want to mention that RFC2109 is obsolete and replaced with RFC2965 which is also now obsolete and replaced with RFC6265.
The use of "Version" in the Cookie is also increasingly unsupported in modern browser updates (most browsers will either ignore the version attribute, or the entire cookie if it contains a version attribute).

@BalusC
Copy link
Member Author

BalusC commented Apr 26, 2021

Candidate for a new ticket/PR indeed but not applicable in current context.

@pzygielo
Copy link
Contributor

It looks that cloned Cookie shares *attributes with original. Is that as intended?

* @since Servlet 5.1
*/
public void setAttribute(String name, String value) {
if (name == null || name.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation here needs to enforce the apidoc above that says:

This should override any predefined attribute for which already a getter/setter pair exist in the current version.

This can be done be removing all the fields and using only the map to store the attributes (and then updating the getters/setters accordingly). Alternately, known fields can be intercepted here and call the setters directly.

Eitherway, we need for the known fields to always appear in the map or to never appear in the map, and never have different values if they appear in both.

Copy link
Member Author

@BalusC BalusC May 1, 2021

Choose a reason for hiding this comment

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

Alternately, known fields can be intercepted here and call the setters directly.

That won't work in long term. An good example is the earlier proposal to make SameSite an enum. We want the API to behave in best possible compatible way when HTTP guys at some point decide to add a new value to the enumeration.

This can be done be removing all the fields and using only the map to store the attributes (and then updating the getters/setters accordingly).

It could only work if we redocument non-String getter methods to throw some runtime exception when an "incompatible" value was set via setAttribute().

Copy link
Member Author

Choose a reason for hiding this comment

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

I've prepped another PR for this: #400

@BalusC
Copy link
Member Author

BalusC commented May 4, 2021

Closing off. This is superseded by #400

@BalusC BalusC closed this May 4, 2021
gregw added a commit that referenced this pull request May 17, 2021
* Add Cookie#setAttribute
#175
#271

* Skip null value from check for reserved token

* Alternative approach to cookie attribute handling
github.com//pull/399#discussion_r624276777

* Make sure path is referenced via constant

* In hindsight, token check doesn't need to be performed on value

* Match casing with spec; mapping is case insensitive anyway

* Optimized Cookie to lazily create attribute map

* Added junit test

* Updates from review

* Fixed quote

* surefire plugin

* Updates from review

* Updates from review

Co-authored-by: Bauke Scholtz <balusc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants