Skip to content

Commit b21b8d9

Browse files
New cookie API (#5420)
* new cookie * optimize set cookie method * fix error * optimize code * optimize code * refactor * optimize code * optimize code [2] --------- Co-authored-by: NathanFreeman <[email protected]> Co-authored-by: MARiA so cute <[email protected]>
1 parent 5ba0700 commit b21b8d9

21 files changed

+924
-174
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,43 @@ struct Context {
213213
void free();
214214
};
215215

216+
class Cookie {
217+
private:
218+
bool encode_;
219+
smart_str buffer_ = {0};
220+
221+
protected:
222+
zend_string *name = nullptr;
223+
zend_string *value = nullptr;
224+
zend_string *path = nullptr;
225+
zend_string *domain = nullptr;
226+
zend_string *sameSite = nullptr;
227+
zend_string *priority = nullptr;
228+
zend_long expires = 0;
229+
zend_bool secure = false;
230+
zend_bool httpOnly = false;
231+
zend_bool partitioned = false;
232+
233+
public:
234+
Cookie(bool _encode = true) {
235+
encode_ = _encode;
236+
}
237+
Cookie *withName(zend_string *);
238+
Cookie *withExpires(zend_long);
239+
Cookie *withSecure(zend_bool);
240+
Cookie *withHttpOnly(zend_bool);
241+
Cookie *withPartitioned(zend_bool);
242+
Cookie *withValue(zend_string *);
243+
Cookie *withPath(zend_string *);
244+
Cookie *withDomain(zend_string *);
245+
Cookie *withSameSite(zend_string *);
246+
Cookie *withPriority(zend_string *);
247+
void reset();
248+
void toArray(zval *return_value);
249+
zend_string *toString();
250+
~Cookie();
251+
};
252+
216253
} // namespace http
217254

218255
namespace http2 {
@@ -270,10 +307,12 @@ class Session {
270307
extern zend_class_entry *swoole_http_server_ce;
271308
extern zend_class_entry *swoole_http_request_ce;
272309
extern zend_class_entry *swoole_http_response_ce;
310+
extern zend_class_entry *swoole_http_cookie_ce;
273311

274312
swoole::http::Context *swoole_http_context_new(swoole::SessionId fd);
275313
swoole::http::Context *php_swoole_http_request_get_and_check_context(zval *zobject);
276314
swoole::http::Context *php_swoole_http_response_get_and_check_context(zval *zobject);
315+
swoole::http::Cookie *php_swoole_http_get_cooke_safety(zval *zobject);
277316

278317
/**
279318
* 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Swoole\Http {
3+
class Cookie {
4+
public function __construct(bool $encode = true) {}
5+
public function withName(string $name): \Swoole\Http\Cookie {}
6+
public function withValue(string $value = ''): \Swoole\Http\Cookie {}
7+
public function withExpires(int $expires = 0): \Swoole\Http\Cookie {}
8+
public function withPath(string $path = '/'): \Swoole\Http\Cookie {}
9+
public function withDomain(string $domain = ''): \Swoole\Http\Cookie {}
10+
public function withSecure(bool $secure = false): \Swoole\Http\Cookie {}
11+
public function withHttpOnly(bool $httpOnly = false): \Swoole\Http\Cookie {}
12+
public function withSameSite(string $sameSite = ''): \Swoole\Http\Cookie {}
13+
public function withPriority(string $priority = ''): \Swoole\Http\Cookie {}
14+
public function withPartitioned(bool $partitioned = false): \Swoole\Http\Cookie {}
15+
public function toArray(): array {}
16+
public function toString(): string | false {}
17+
public function reset(): void {}
18+
}
19+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* This is a generated file, edit the .stub.php file instead.
2+
* Stub hash: a11e74ea8b1a4c77a3c10fbfbd94775c504ba812 */
3+
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Http_Cookie___construct, 0, 0, 0)
5+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, encode, _IS_BOOL, 0, "true")
6+
ZEND_END_ARG_INFO()
7+
8+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withName, 0, 1, Swoole\\Http\\Cookie, 0)
9+
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
10+
ZEND_END_ARG_INFO()
11+
12+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withValue, 0, 0, Swoole\\Http\\Cookie, 0)
13+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
14+
ZEND_END_ARG_INFO()
15+
16+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withExpires, 0, 0, Swoole\\Http\\Cookie, 0)
17+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
18+
ZEND_END_ARG_INFO()
19+
20+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPath, 0, 0, Swoole\\Http\\Cookie, 0)
21+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
22+
ZEND_END_ARG_INFO()
23+
24+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withDomain, 0, 0, Swoole\\Http\\Cookie, 0)
25+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
26+
ZEND_END_ARG_INFO()
27+
28+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withSecure, 0, 0, Swoole\\Http\\Cookie, 0)
29+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
30+
ZEND_END_ARG_INFO()
31+
32+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withHttpOnly, 0, 0, Swoole\\Http\\Cookie, 0)
33+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httpOnly, _IS_BOOL, 0, "false")
34+
ZEND_END_ARG_INFO()
35+
36+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withSameSite, 0, 0, Swoole\\Http\\Cookie, 0)
37+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sameSite, IS_STRING, 0, "\'\'")
38+
ZEND_END_ARG_INFO()
39+
40+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPriority, 0, 0, Swoole\\Http\\Cookie, 0)
41+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
42+
ZEND_END_ARG_INFO()
43+
44+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Http_Cookie_withPartitioned, 0, 0, Swoole\\Http\\Cookie, 0)
45+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
46+
ZEND_END_ARG_INFO()
47+
48+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_toArray, 0, 0, IS_ARRAY, 0)
49+
ZEND_END_ARG_INFO()
50+
51+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Http_Cookie_toString, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
52+
ZEND_END_ARG_INFO()
53+
54+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Cookie_reset, 0, 0, IS_VOID, 0)
55+
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|string $name_or_object , string $value = '', int $expires = 0 , string $path = '/', string $domain = '', bool $secure = false , bool $httponly = false, string $samesite = '', string $priority = '', bool $partitioned = false): 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 $partitioned = false): 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: 15 additions & 3 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: 8797137fe315a9dec64e349bad8bd72529895bf9 */
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,7 +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)
24+
ZEND_ARG_OBJ_TYPE_MASK(0, name_or_object, Swoole\\Http\\Cookie, MAY_BE_STRING, NULL)
2525
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
2626
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
2727
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
@@ -30,9 +30,21 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_cooki
3030
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httponly, _IS_BOOL, 0, "false")
3131
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, samesite, IS_STRING, 0, "\'\'")
3232
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
33+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
3334
ZEND_END_ARG_INFO()
3435

35-
#define arginfo_class_Swoole_Http_Response_rawcookie arginfo_class_Swoole_Http_Response_cookie
36+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_rawcookie, 0, 1, _IS_BOOL, 0)
37+
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
38+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_STRING, 0, "\'\'")
39+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, expires, IS_LONG, 0, "0")
40+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, path, IS_STRING, 0, "\'/\'")
41+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, domain, IS_STRING, 0, "\'\'")
42+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, secure, _IS_BOOL, 0, "false")
43+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, httponly, _IS_BOOL, 0, "false")
44+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, samesite, IS_STRING, 0, "\'\'")
45+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, priority, IS_STRING, 0, "\'\'")
46+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, partitioned, _IS_BOOL, 0, "false")
47+
ZEND_END_ARG_INFO()
3648

3749
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Http_Response_header, 0, 2, _IS_BOOL, 0)
3850
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)

0 commit comments

Comments
 (0)