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 6 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Simplify cookie setting code in ext/session
Use the modern SAPI header ops API, including the remove prefix op we
just added.
  • Loading branch information
NattyNarwhal committed Jun 13, 2025
commit 0fe9c0d11d6f8b01f1194d2e59baa204f203a84b
41 changes: 9 additions & 32 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1341,45 +1341,22 @@ static int php_session_cache_limiter(void)
* removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
*/
static void php_session_remove_cookie(void) {
sapi_header_struct *header;
zend_llist *l = &SG(sapi_headers).headers;
zend_llist_element *next;
zend_llist_element *current;
char *session_cookie;
size_t session_cookie_len;
size_t len = sizeof("Set-Cookie")-1;
sapi_header_line header_line = {0};

ZEND_ASSERT(strpbrk(PS(session_name), SESSION_FORBIDDEN_CHARS) == NULL);
spprintf(&session_cookie, 0, "Set-Cookie: %s=", PS(session_name));

session_cookie_len = strlen(session_cookie);
current = l->head;
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?

Copy link
Member

Choose a reason for hiding this comment

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

No it's not because it doesn't get the "Set-Cookie" len - it just know the total prefix len so it cannot check for it.

Copy link
Member

Choose a reason for hiding this comment

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

And it also doesn't check the separator because that's done only for SAPI_HEADER_DELETE variant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I see what you mean. I think I can restore this optimization.

&& !strncmp(header->header, session_cookie, session_cookie_len)) {
if (current->prev) {
current->prev->next = next;
} else {
l->head = next;
}
if (next) {
next->prev = current->prev;
} else {
l->tail = current->prev;
}
sapi_free_header(header);
efree(current);
--l->count;
}
current = next;
}
header_line.line = session_cookie;
header_line.line_len = strlen(session_cookie);
sapi_header_op(SAPI_HEADER_DELETE_PREFIX, &header_line);

efree(session_cookie);
}

static zend_result php_session_send_cookie(void)
{
sapi_header_line header_line = {0};
smart_str ncookie = {0};
zend_string *date_fmt = NULL;
zend_string *e_id;
Expand Down Expand Up @@ -1445,9 +1422,9 @@ static zend_result php_session_send_cookie(void)
smart_str_0(&ncookie);

php_session_remove_cookie(); /* remove already sent session ID cookie */
/* 'replace' must be 0 here, else a previous Set-Cookie
header, probably sent with setcookie() will be replaced! */
sapi_add_header_ex(estrndup(ZSTR_VAL(ncookie.s), ZSTR_LEN(ncookie.s)), ZSTR_LEN(ncookie.s), 0, 0);
header_line.line = ZSTR_VAL(ncookie.s);
header_line.line_len = ZSTR_LEN(ncookie.s);
sapi_header_op(SAPI_HEADER_ADD, &header_line);
smart_str_free(&ncookie);

return SUCCESS;
Expand Down