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

Properly remove cookie header #166

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ public PushBuilderImpl(HttpServletRequestImpl servletRequest) {
if(cookie.getValue().getMaxAge() != null && cookie.getValue().getMaxAge() <= 0) {
//remove cookie
List<String> existing = this.headers.getAll(HttpHeaderNames.COOKIE);
if(existing != null) {
Iterator<String> it = existing.iterator();
while (it.hasNext()) {
String val = it.next();
if(val.startsWith(cookie.getKey() + "=")) {
it.remove();
}
List<String> newValues = new ArrayList<>(existing.size());
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure existing cannot be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the docs, it can never be null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also checked all the implementation and saw that they always return an empty list if the name does not exist

Copy link
Member

Choose a reason for hiding this comment

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

The old code was more defensive.

for (String value : existing) {
if(value.startsWith(cookie.getKey() + "=")) {
continue;
}
newValues.add(value);
}
this.headers.set(HttpHeaderNames.COOKIE, newValues);
Copy link
Member

Choose a reason for hiding this comment

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

Does it erase the older value or suffix it (headers are multimap), I believe set erase, so it would work, but it needs to be checked.

Copy link
Contributor Author

@geoand geoand Oct 1, 2024

Choose a reason for hiding this comment

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

Sets a header with the specified name and values. If there is an existing header with the same name, it is removed. This getMethod can be represented approximately as the following code:
  
  headers.remove(name);
  for (Object v: values) {
      if (v == null) {
          break;
      }
      headers. add(name, v);
  }

} else if(!cookie.getKey().equals(servletRequest.getServletContext().getSessionCookieConfig().getName())){
this.headers.add(HttpHeaderNames.COOKIE, cookie.getKey() + "=" + cookie.getValue().getValue());
}
Expand Down
Loading