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 5 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
39 changes: 5 additions & 34 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1335,46 +1335,17 @@ 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?

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);
}

Expand Down
19 changes: 14 additions & 5 deletions main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,23 @@ 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)
{
sapi_header_struct *header;
zend_llist_element *next;
zend_llist_element *current=l->head;

size_t header_len = len;
const char *colon = strchr(name, ':');
if (colon) {
header_len = (size_t)(colon - name);
}

while (current) {
header = (sapi_header_struct *)(current->data);
next = current->next;
if (header->header_len > len && header->header[len] == ':'
if (header->header_len > header_len
&& (header->header[header_len] == ':' || len > header_len)
&& !strncasecmp(header->header, name, len)) {
if (current->prev) {
current->prev->next = next;
Expand Down Expand Up @@ -653,7 +661,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));
*colon_offset = sav;
}
}
Expand Down Expand Up @@ -691,6 +699,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 +731,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 Down
2 changes: 1 addition & 1 deletion main/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ 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;

BEGIN_EXTERN_C()
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg);

/* Deprecated functions. Use sapi_header_op instead. */
SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace);
#define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1)

Expand Down