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
Move setting header length to caller
Suggestion from Jakub.
  • Loading branch information
NattyNarwhal committed Jul 2, 2025
commit 8cd5992c99316de88c244041e226df7c004f1043
1 change: 1 addition & 0 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ static void php_session_remove_cookie(void) {

header_line.line = session_cookie;
header_line.line_len = strlen(session_cookie);
header_line.header_len = sizeof("Set-Cookie") - 1;
sapi_header_op(SAPI_HEADER_DELETE_PREFIX, &header_line);

efree(session_cookie);
Expand Down
22 changes: 11 additions & 11 deletions main/SAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,12 @@ 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, size_t header_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;
Expand Down Expand Up @@ -661,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), 0);
*colon_offset = sav;
}
}
Expand All @@ -676,7 +670,7 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
sapi_header_struct sapi_header;
char *colon_offset;
char *header_line;
size_t header_line_len;
size_t header_line_len, header_len;
int http_response_code;

if (SG(headers_sent) && !SG(request_info).no_headers) {
Expand Down Expand Up @@ -708,7 +702,13 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
}
header_line = estrndup(p->line, p->line_len);
header_line_len = p->line_len;
http_response_code = p->response_code;
if (op == SAPI_HEADER_DELETE_PREFIX) {
header_len = p->header_len;
http_response_code = 0;
} else {
header_len = 0;
http_response_code = p->response_code;
}
break;
}

Expand Down Expand Up @@ -742,7 +742,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, header_len);
efree(header_line);
return SUCCESS;
} else {
Expand Down
5 changes: 4 additions & 1 deletion main/SAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ END_EXTERN_C()
typedef struct {
const char *line; /* If you allocated this, you need to free it yourself */
size_t line_len;
zend_long response_code; /* long due to zend_parse_parameters compatibility */
union {
zend_long response_code; /* long due to zend_parse_parameters compatibility */
size_t header_len; /* the "Key" in "Key: Value", for optimization */
};
} sapi_header_line;

typedef enum { /* Parameter: */
Expand Down