Skip to content

Commit afc1c24

Browse files
committed
Simplify cookie setting code in ext/session
Use the modern SAPI header ops API, including the remove prefix op we just added.
1 parent e7c3bf7 commit afc1c24

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

ext/session/session.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,44 +1341,22 @@ static int php_session_cache_limiter(void)
13411341
* removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
13421342
*/
13431343
static void php_session_remove_cookie(void) {
1344-
sapi_header_struct *header;
1345-
zend_llist *l = &SG(sapi_headers).headers;
1346-
zend_llist_element *next;
1347-
zend_llist_element *current;
13481344
char *session_cookie;
1349-
size_t session_cookie_len;
1350-
size_t len = sizeof("Set-Cookie")-1;
1345+
sapi_header_line header_line = {0};
13511346

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

1355-
current = l->head;
1356-
while (current) {
1357-
header = (sapi_header_struct *)(current->data);
1358-
next = current->next;
1359-
if (header->header_len > len && header->header[len] == ':'
1360-
&& !strncmp(header->header, session_cookie, session_cookie_len)) {
1361-
if (current->prev) {
1362-
current->prev->next = next;
1363-
} else {
1364-
l->head = next;
1365-
}
1366-
if (next) {
1367-
next->prev = current->prev;
1368-
} else {
1369-
l->tail = current->prev;
1370-
}
1371-
sapi_free_header(header);
1372-
efree(current);
1373-
--l->count;
1374-
}
1375-
current = next;
1376-
}
1350+
header_line.line = session_cookie;
1351+
header_line.line_len = strlen(session_cookie);
1352+
sapi_header_op(SAPI_HEADER_DELETE_PREFIX, &header_line);
1353+
13771354
efree(session_cookie);
13781355
}
13791356

13801357
static zend_result php_session_send_cookie(void)
13811358
{
1359+
sapi_header_line header_line = {0};
13821360
smart_str ncookie = {0};
13831361
zend_string *date_fmt = NULL;
13841362
zend_string *e_id;
@@ -1444,9 +1422,9 @@ static zend_result php_session_send_cookie(void)
14441422
smart_str_0(&ncookie);
14451423

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

14521430
return SUCCESS;

0 commit comments

Comments
 (0)