Add SAPI_HEADER_DELETE_PREFIX, make ext/session use it (#18678)

* Add SAPI_HEADER_DELETE_PREFIX operation

The session ext currently munges into the linked list of headers
itself, because the delete header API is given the key for headers to
delete. The session ext wants to use a prefix past the colon separator,
for i.e. "Set-Cookie: PHPSESSID=", to eliminate only the specific cookie
rather than all cookies.

This changes the SAPI code to add a new header op to take a prefix
instead. Call sites are yet unchanged. Also fix some whitespace.

* Simplify cookie setting code in ext/session

Use the modern SAPI header ops API, including the remove prefix op we
just added.

* [ci skip] Remove redundant and unnecessary comment

The purpose of this is clear, and after refactoring, the special case is
no longer there, so it has no value.

* Un-deprecate simple add/replace header API, use it

Suggestion from Jakub.

* Restore the optimization removing session cookies had

I don't think this needs to be special cased with the parameter.

* Move setting header length to caller

Suggestion from Jakub.

* [ci skip] adjust tab count

It may be better to use spaces in here instead.

* Use session_cookie_len rather than calling strlen
This commit is contained in:
Calvin Buckley 2025-07-31 19:52:04 -03:00 committed by GitHub
parent a262419398
commit 18dee43e02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 42 deletions

View file

@ -185,13 +185,17 @@ 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: */
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;
@ -199,7 +203,6 @@ typedef enum { /* Parameter: */
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)