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
Open
Show file tree
Hide file tree
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
46 changes: 9 additions & 37 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1335,51 +1335,23 @@ static int php_session_cache_limiter(void)
* Cookie Management *
********************* */

/*
* Remove already sent session ID cookie.
* It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
* 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?

&& !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 +1417,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);
Comment on lines +1420 to +1422
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.

smart_str_free(&ncookie);

return SUCCESS;
Expand Down
15 changes: 9 additions & 6 deletions main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,17 @@ static void sapi_update_response_code(int ncode)
* since zend_llist_del_element only removes one matched item once,
* we should remove them manually
*/
static void sapi_remove_header(zend_llist *l, char *name, size_t len) {
static void sapi_remove_header(zend_llist *l, char *name, size_t len, bool check_separator)
{
sapi_header_struct *header;
zend_llist_element *next;
zend_llist_element *current=l->head;

while (current) {
header = (sapi_header_struct *)(current->data);
next = current->next;
if (header->header_len > len && header->header[len] == ':'
if (header->header_len > len
&& (header->header[len] == ':' || !check_separator)
&& !strncasecmp(header->header, name, len)) {
if (current->prev) {
current->prev->next = next;
Expand Down Expand Up @@ -653,7 +655,7 @@ static void sapi_header_add_op(sapi_header_op_enum op, sapi_header_struct *sapi_
char sav = *colon_offset;

*colon_offset = 0;
sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header));
sapi_remove_header(&SG(sapi_headers).headers, sapi_header->header, strlen(sapi_header->header), true);
*colon_offset = sav;
}
}
Expand Down Expand Up @@ -691,6 +693,7 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)

case SAPI_HEADER_ADD:
case SAPI_HEADER_REPLACE:
case SAPI_HEADER_DELETE_PREFIX:
case SAPI_HEADER_DELETE: {
sapi_header_line *p = arg;

Expand Down Expand Up @@ -722,8 +725,8 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
header_line[header_line_len]='\0';
}

if (op == SAPI_HEADER_DELETE) {
if (strchr(header_line, ':')) {
if (op == SAPI_HEADER_DELETE || op == SAPI_HEADER_DELETE_PREFIX) {
if (op == SAPI_HEADER_DELETE && strchr(header_line, ':')) {
efree(header_line);
sapi_module.sapi_error(E_WARNING, "Header to delete may not contain colon.");
return FAILURE;
Expand All @@ -733,7 +736,7 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
sapi_header.header_len = header_line_len;
sapi_module.header_handler(&sapi_header, op, &SG(sapi_headers));
}
sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len);
sapi_remove_header(&SG(sapi_headers).headers, header_line, header_line_len, op == SAPI_HEADER_DELETE);
efree(header_line);
return SUCCESS;
} else {
Expand Down
1 change: 1 addition & 0 deletions main/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ typedef enum { /* Parameter: */
SAPI_HEADER_REPLACE, /* sapi_header_line* */
SAPI_HEADER_ADD, /* sapi_header_line* */
SAPI_HEADER_DELETE, /* sapi_header_line* */
SAPI_HEADER_DELETE_PREFIX, /* sapi_header_line* */
SAPI_HEADER_DELETE_ALL, /* void */
SAPI_HEADER_SET_STATUS /* int */
} sapi_header_op_enum;
Expand Down