Skip to content

Commit 83c21f4

Browse files
committed
feat: setcookie supports CHIPS
1 parent 677a1f8 commit 83c21f4

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/head.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PHPAPI bool php_header(void)
7979
#define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", or \"\\014\""
8080
PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
8181
zend_string *path, zend_string *domain, bool secure, bool httponly,
82-
zend_string *samesite, bool url_encode)
82+
zend_string *samesite, bool partitioned, bool url_encode)
8383
{
8484
zend_string *dt;
8585
sapi_header_line ctr = {0};
@@ -182,6 +182,9 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
182182
smart_str_appends(&buf, COOKIE_SAMESITE);
183183
smart_str_append(&buf, samesite);
184184
}
185+
if (partitioned) {
186+
smart_str_appends(&buf, COOKIE_PARTITIONED);
187+
}
185188

186189
ctr.line = ZSTR_VAL(buf.s);
187190
ctr.line_len = (uint32_t) ZSTR_LEN(buf.s);
@@ -192,7 +195,7 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
192195
}
193196

194197
static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_long *expires, zend_string **path,
195-
zend_string **domain, bool *secure, bool *httponly, zend_string **samesite)
198+
zend_string **domain, bool *secure, bool *httponly, zend_string **samesite, bool *partitioned)
196199
{
197200
zend_string *key;
198201
zval *value;
@@ -212,6 +215,8 @@ static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_
212215
*secure = zval_is_true(value);
213216
} else if (zend_string_equals_literal_ci(key, "httponly")) {
214217
*httponly = zval_is_true(value);
218+
} else if (zend_string_equals_literal_ci(key, "partitioned")) {
219+
*partitioned = zval_is_true(value);
215220
} else if (zend_string_equals_literal_ci(key, "samesite")) {
216221
*samesite = zval_get_string(value);
217222
} else {
@@ -227,9 +232,9 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
227232
HashTable *options = NULL;
228233
zend_long expires = 0;
229234
zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
230-
bool secure = 0, httponly = 0;
235+
bool secure = 0, httponly = 0, partitioned = 0;
231236

232-
ZEND_PARSE_PARAMETERS_START(1, 7)
237+
ZEND_PARSE_PARAMETERS_START(1, 8)
233238
Z_PARAM_STR(name)
234239
Z_PARAM_OPTIONAL
235240
Z_PARAM_STR(value)
@@ -238,6 +243,7 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
238243
Z_PARAM_STR(domain)
239244
Z_PARAM_BOOL(secure)
240245
Z_PARAM_BOOL(httponly)
246+
Z_PARAM_BOOL(partitioned)
241247
ZEND_PARSE_PARAMETERS_END();
242248

243249
if (options) {
@@ -248,13 +254,13 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
248254
}
249255

250256
if (FAILURE == php_head_parse_cookie_options_array(options, &expires, &path,
251-
&domain, &secure, &httponly, &samesite)
257+
&domain, &secure, &httponly, &samesite, &partitioned)
252258
) {
253259
goto cleanup;
254260
}
255261
}
256262

257-
if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, !is_raw) == SUCCESS) {
263+
if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, partitioned, !is_raw) == SUCCESS) {
258264
RETVAL_TRUE;
259265
} else {
260266
RETVAL_FALSE;

ext/standard/head.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
#define COOKIE_SECURE "; secure"
2525
#define COOKIE_HTTPONLY "; HttpOnly"
2626
#define COOKIE_SAMESITE "; SameSite="
27+
#define COOKIE_PARTITIONED "; Partitioned"
2728

2829
extern PHP_RINIT_FUNCTION(head);
2930

3031
PHPAPI bool php_header(void);
3132
PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
3233
zend_string *path, zend_string *domain, bool secure, bool httponly,
33-
zend_string *samesite, bool url_encode);
34+
zend_string *samesite, bool url_encode, bool partitioned);
3435

3536
#endif

ext/standard/tests/network/setcookie.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ setcookie('name', 'value', 0, '/path/');
1616
setcookie('name', 'value', 0, '', 'domain.tld');
1717
setcookie('name', 'value', 0, '', '', TRUE);
1818
setcookie('name', 'value', 0, '', '', FALSE, TRUE);
19+
setcookie('name', 'value', 0, '', '', FALSE, FALSE, TRUE);
1920

2021
setcookie('name', 'value', ['expires' => $tsp]);
2122
setcookie('name', 'value', ['expires' => $tsn, 'path' => '/path/', 'domain' => 'domain.tld', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict']);
2223

24+
setcookie('name', 'value', ['partitioned' => 1]);
25+
2326
$expected = array(
2427
'Set-Cookie: name=deleted; expires='.date('D, d M Y H:i:s', 1).' GMT; Max-Age=0',
2528
'Set-Cookie: name=deleted; expires='.date('D, d M Y H:i:s', 1).' GMT; Max-Age=0',
@@ -33,8 +36,10 @@ $expected = array(
3336
'Set-Cookie: name=value; domain=domain.tld',
3437
'Set-Cookie: name=value; secure',
3538
'Set-Cookie: name=value; HttpOnly',
39+
'Set-Cookie: name=value; Partitioned',
3640
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsp).' GMT; Max-Age=5',
37-
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsn).' GMT; Max-Age=0; path=/path/; domain=domain.tld; secure; HttpOnly; SameSite=Strict'
41+
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsn).' GMT; Max-Age=0; path=/path/; domain=domain.tld; secure; HttpOnly; SameSite=Strict',
42+
'Set-Cookie: name=value; Partitioned',
3843
);
3944

4045
$headers = headers_list();

0 commit comments

Comments
 (0)