Skip to content

Add SAPI_HEADER_DELETE_PREFIX, make ext/session use it #18678

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

NattyNarwhal
Copy link
Member

@NattyNarwhal NattyNarwhal commented May 27, 2025

Refactoring this as part of triaging GH-18601 - I don't think it'll fix the issue, but it's a good idea to clean anyways. ext/session had its own copy of the remove header code, because the SAPI header op mandated it was just a name, and would remove all headers for that name. ext/session couldn't rely on this because it only wanted to remove a specific cookie, not all of them.

To simplify this, add a version of SAPI_HEADER_DELETE that is more flexible and allows for passing a prefix to check for, not just a name. This should work for removing a specific cookie from the headers. (Also fixes some whitespace too.)

The session ext currently munges into the linked list of headers
itself, because the delete header API is given the key for headers to
delete. The session ext wants to use a prefix past the colon separator,
for i.e. "Set-Cookie: PHPSESSID=", to eliminate only the specific cookie
rather than all cookies.

This changes the SAPI code to add a new header op to take a prefix
instead. Call sites are yet unchanged. Also fix some whitespace.
Use the modern SAPI header ops API, including the remove prefix op we
just added.
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

Happy with the ext/session changes, makes sense to delegate this to the SAPI layer.

@NattyNarwhal NattyNarwhal changed the title Add SAPI_HEADER_REMOVE_PREFIX, make ext/session use it Add SAPI_HEADER_DELETE_PREFIX, make ext/session use it May 28, 2025
The purpose of this is clear, and after refactoring, the special case is
no longer there, so it has no value.
Copy link
Member

@bukka bukka left a comment

Choose a reason for hiding this comment

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

I agree that this logic should be in SAPI.c but it might be better to have more specialized variants (even specifically for Set-Cookie wrapped in SAPI functions.

I would prefer not use header_line but just wrapping functions instead though.

while (current) {
header = (sapi_header_struct *)(current->data);
next = current->next;
if (header->header_len > len && header->header[len] == ':'
Copy link
Member

Choose a reason for hiding this comment

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

Think this is meant to be a micro optimization that it checks that the header is size of Set-Cookie header which might skip some strncmp calls...

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 think this optimization should still be preserved in sapi_remove_header though?

Comment on lines +1420 to +1422
header_line.line = ZSTR_VAL(ncookie.s);
header_line.line_len = ZSTR_LEN(ncookie.s);
sapi_header_op(SAPI_HEADER_ADD, &header_line);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's much point in this and previous version is probably better for outside SAPI usage.

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason why I did this was because sapi_add_header_ex was marked as deprecated in the header and to use sapi_header_op instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants