Skip to content

Commit 5568707

Browse files
committed
new cookie
1 parent 936402e commit 5568707

26 files changed

+817
-175
lines changed

ext-src/php_swoole.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ PHP_MINIT_FUNCTION(swoole) {
745745
php_swoole_server_port_minit(module_number);
746746
php_swoole_http_request_minit(module_number);
747747
php_swoole_http_response_minit(module_number);
748+
php_swoole_http_cookie_minit(module_number);
748749
php_swoole_http_server_minit(module_number);
749750
php_swoole_http_server_coro_minit(module_number);
750751
php_swoole_websocket_server_minit(module_number);

ext-src/php_swoole_http.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,112 @@ struct Context {
213213
void free();
214214
};
215215

216+
struct Cookie {
217+
zend_string *name = nullptr;
218+
zend_string *value = nullptr;
219+
zend_string *path = nullptr;
220+
zend_string *domain = nullptr;
221+
zend_string *sameSite = nullptr;
222+
zend_string *priority = nullptr;
223+
zend_long expires = 0;
224+
zend_bool secure = false;
225+
zend_bool httpOnly = false;
226+
zend_bool partitioned = false;
227+
smart_str buffer = {0};
228+
229+
zend_string *create() {
230+
zend_string *date = nullptr;
231+
if (!value) {
232+
smart_str_append(&buffer, name);
233+
smart_str_appends(&buffer, "=deleted; expires=");
234+
235+
date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), 1, 0);
236+
smart_str_append(&buffer, date);
237+
smart_str_appends(&buffer, "; Max-Age=0");
238+
zend_string_free(date);
239+
240+
smart_str_0(&buffer);
241+
return buffer.s;
242+
}
243+
244+
smart_str_append(&buffer, name);
245+
smart_str_appendc(&buffer, '=');
246+
smart_str_append(&buffer, value);
247+
248+
if (expires > 0) {
249+
smart_str_appends(&buffer, "; expires=");
250+
date = php_format_date((char *) ZEND_STRL("D, d-M-Y H:i:s T"), expires, 0);
251+
smart_str_append(&buffer, date);
252+
smart_str_appends(&buffer, "; Max-Age=");
253+
254+
double diff = difftime(expires, php_time());
255+
smart_str_append_long(&buffer, (zend_long) (diff >= 0 ? diff : 0));
256+
zend_string_free(date);
257+
}
258+
259+
if (path && ZSTR_LEN(path) > 0) {
260+
smart_str_appends(&buffer, "; path=");
261+
smart_str_append(&buffer, path);
262+
}
263+
264+
if (domain && ZSTR_LEN(domain) > 0) {
265+
smart_str_appends(&buffer, "; domain=");
266+
smart_str_append(&buffer, domain);
267+
}
268+
269+
if (secure) {
270+
smart_str_appends(&buffer, "; secure");
271+
}
272+
273+
if (httpOnly) {
274+
smart_str_appends(&buffer, "; HttpOnly");
275+
}
276+
277+
if (sameSite && ZSTR_LEN(sameSite) > 0) {
278+
smart_str_appends(&buffer, "; SameSite=");
279+
smart_str_append(&buffer, sameSite);
280+
}
281+
282+
if (priority && ZSTR_LEN(priority) > 0) {
283+
smart_str_appends(&buffer, "; Priority=");
284+
smart_str_append(&buffer, priority);
285+
}
286+
287+
if (partitioned) {
288+
smart_str_appends(&buffer, "; Partitioned");
289+
}
290+
291+
smart_str_0(&buffer);
292+
return buffer.s;
293+
}
294+
295+
~Cookie() {
296+
if (name) {
297+
zend_string_release(name);
298+
}
299+
300+
if (value) {
301+
zend_string_release(value);
302+
}
303+
304+
if (path) {
305+
zend_string_release(path);
306+
}
307+
308+
if (domain) {
309+
zend_string_release(domain);
310+
}
311+
312+
if (sameSite) {
313+
zend_string_release(sameSite);
314+
}
315+
316+
if (priority) {
317+
zend_string_release(priority);
318+
}
319+
}
320+
};
321+
216322
} // namespace http
217323

218324
namespace http2 {
@@ -270,10 +376,12 @@ class Session {
270376
extern zend_class_entry *swoole_http_server_ce;
271377
extern zend_class_entry *swoole_http_request_ce;
272378
extern zend_class_entry *swoole_http_response_ce;
379+
extern zend_class_entry *swoole_http_cookie_ce;
273380

274381
swoole::http::Context *swoole_http_context_new(swoole::SessionId fd);
275382
swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobject);
276383
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);
384+
swoole::http::Cookie *php_swoole_http_response_get_and_check_cookie(zval *zobject);
277385

278386
/**
279387
* These class properties cannot be modified by the user before assignment, such as Swoole\\Http\\Request.

ext-src/php_swoole_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ void php_swoole_server_minit(int module_number);
262262
void php_swoole_server_port_minit(int module_number);
263263
void php_swoole_http_request_minit(int module_number);
264264
void php_swoole_http_response_minit(int module_number);
265+
void php_swoole_http_cookie_minit(int module_number);
265266
void php_swoole_http_server_minit(int module_number);
266267
void php_swoole_http_server_coro_minit(int module_number);
267268
void php_swoole_websocket_server_minit(int module_number);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace Swoole\Http {
3+
class Cookie {
4+
public function __construct() {}
5+
public function setName(string $name): void {}
6+
public function setValue(string $value = ''): void {}
7+
public function setExpires(int $expires = 0): void {}
8+
public function setPath(string $path = '/'): void {}
9+
public function setDomain(string $domain = ''): void {}
10+
public function setSecure(bool $secure = false): void {}
11+
public function setHttpOnly(bool $httpOnly = false): void {}
12+
public function setSameSite(string $sameSite = ''): void {}
13+
public function setPriority(string $priority = ''): void {}
14+
public function setPartitioned(bool $partitioned = false): void {}
15+
public function getCookie(): array {}
16+
public function reset(): bool {}
17+
}
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* This is a generated file, edit the .stub.php file instead.
2+
* Stub hash: a14fec26e023c94118c7074dec7f462f8b3a84f0 */
3+
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
5+
ZEND_END_ARG_INFO()
6+
7+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setName, 0, 1, IS_VOID, 0)
8+
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
9+
ZEND_END_ARG_INFO()
10+
11+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setValue, 0, 0, IS_VOID, 0)
12+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
13+
ZEND_END_ARG_INFO()
14+
15+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setExpires, 0, 0, IS_VOID, 0)
16+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
17+
ZEND_END_ARG_INFO()
18+
19+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPath, 0, 0, IS_VOID, 0)
20+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
21+
ZEND_END_ARG_INFO()
22+
23+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setDomain, 0, 0, IS_VOID, 0)
24+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
25+
ZEND_END_ARG_INFO()
26+
27+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSecure, 0, 0, IS_VOID, 0)
28+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
29+
ZEND_END_ARG_INFO()
30+
31+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setHttpOnly, 0, 0, IS_VOID, 0)
32+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httpOnly, _IS_BOOL, 0, "false")
33+
ZEND_END_ARG_INFO()
34+
35+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setSameSite, 0, 0, IS_VOID, 0)
36+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sameSite, IS_STRING, 0, "\'\'")
37+
ZEND_END_ARG_INFO()
38+
39+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPriority, 0, 0, IS_VOID, 0)
40+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
41+
ZEND_END_ARG_INFO()
42+
43+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_setPartitioned, 0, 0, IS_VOID, 0)
44+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
45+
ZEND_END_ARG_INFO()
46+
47+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_getCookie, 0, 0, IS_ARRAY, 0)
48+
ZEND_END_ARG_INFO()
49+
50+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, _IS_BOOL, 0)
51+
ZEND_END_ARG_INFO()

ext-src/stubs/php_swoole_http_response.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ public function write(string $content): bool {}
55
public function end(?string $content = null): bool {}
66
public function sendfile(string $filename, int $offset = 0, int $length = 0): bool {}
77
public function redirect(string $location, int $http_code = 302): bool {}
8-
public function cookie(string $name, string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = ''): bool {}
9-
public function rawcookie(string $name, string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = ''): bool {}
8+
public function cookie(\Swoole\Http\Cookie $cookie): bool {}
9+
public function rawcookie(\Swoole\Http\Cookie $cookie): bool {}
1010
public function header(string $key, string|array $value, bool $format = true): bool {}
1111
public function initHeader(): bool {}
1212
public function isWritable(): bool {}

ext-src/stubs/php_swoole_http_response_arginfo.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: f233694bac2a3ab5469d8ffd95d4d44f5ce9c340 */
2+
* Stub hash: 8d9d9ceaf03c44dc001ced33ab773ca091ec3996 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_write, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
@@ -21,15 +21,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_redir
2121
ZEND_END_ARG_INFO()
2222

2323
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cookie, 0, 1, _IS_BOOL, 0)
24-
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
25-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
26-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
27-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
28-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
29-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
30-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httponly, _IS_BOOL, 0, "false")
31-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, samesite, IS_STRING, 0, "\'\'")
32-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
24+
ZEND_ARG_OBJ_INFO(0, cookie, Swoole\\Http\\Cookie, 0)
3325
ZEND_END_ARG_INFO()
3426

3527
#define arginfo_class_Swoole_Http_Response_rawcookie arginfo_class_Swoole_Http_Response_cookie

0 commit comments

Comments
 (0)